hadoop mapreduce 평균 기온 계산 코드, 절대 오리지널
1901 46
1902 21
1903 48
1904 33
1905 43
1906 47
1907 31
1908 28
1909 26
1910 35
1911 30
1912 16
1913 29
1914 29
1915 5
1916 21
1917 22
1918 31
1919 27
1920 43
1921 34
1922 27
1923 26
package com.teset;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class AvgTemprature extends Configured implements Tool {
public static class AvgMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
@Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
// map
String str = value.toString();
String year = null;
int temprature = 0;
StringTokenizer tokenstr = new StringTokenizer(str);
int i = 0;
while (tokenstr.hasMoreTokens()) {
String tempstr = tokenstr.nextToken();
i++;
if (i == 1) {
year = tempstr;
continue;
} else if (i == 5 && Integer.parseInt(tempstr) != -9999) {
temprature = Integer.parseInt(tempstr);
context.write(new Text(year), new IntWritable(temprature));
break;
}
}
}
}
public static class AvgReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
int sum=0;
int count=0;
for (IntWritable value : values) {
sum=sum+value.get();
count++;
}
if (count!=0){
context.write(key, new IntWritable(sum/count));
}
}
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new AvgTemprature(), args);
System.exit(res);
}
@Override
public int run(String[] arg0) throws Exception {
Configuration conf = getConf();
Job job = new Job(conf, "AvgTem");//
job.setJarByClass(AvgTemprature.class);// class
//
job.setMapperClass(AvgMapper.class);// map
job.setReducerClass(AvgReducer.class);
FileInputFormat.addInputPath(job, new Path(arg0[0]));
FileOutputFormat.setOutputPath(job, new Path(arg0[1]));
job.setCombinerClass(AvgReducer.class);
job.setOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.waitForCompletion(true);
return job.isSuccessful() ? 0 : 1;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.