一个带Reducer的hive2es的一个例子
程序员文章站
2022-06-05 09:01:03
...
package com.peidw.ch1;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
import java.util.StringTokenizer;
public class WordsMapper extends Mapper<Object, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
Text word = new Text();
word.set(itr.nextToken());
context.write(word, one);
}
}
}
reducer
package com.peidw.ch1;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
import java.util.StringTokenizer;
public class WordsMapper extends Mapper<Object, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
Text word = new Text();
word.set(itr.nextToken());
context.write(word, one);
}
}
}
drive
package com.peidw.ch1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.elasticsearch.hadoop.mr.EsOutputFormat;
public class Driver {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
// ElasticSearch Server nodes to point to
conf.set("es.nodes", "centos.hadoop:9200");
// ElasticSearch index and type name in {indexName}/{typeName} format
conf.set("es.resource", "eshadoop/wordcount");
conf.setBoolean("es.mapred.map.tasks.speculative.execution", false);
conf.setBoolean("es.mapred.reduce.tasks.speculative.execution", false);
conf.setInt("es.mapred.number_of_shards",2);
conf.setInt("es.mapred.number_of_replicas",0);
// Create Job instance
Job job = new Job(conf, "word count");
// set Driver class
job.setJarByClass(Driver.class);
job.setMapperClass(WordsMapper.class);
job.setReducerClass(WordsReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// set OutputFormat to EsOutputFormat provided by ElasticSearch-Hadoop jar
job.setOutputFormatClass(EsOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
pom.xml和上篇一样
上一篇: 对称加密与非对称加密
下一篇: SHA256Utils