一个带Reducer的hive2es的一个例子

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 {

    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 {

    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和上篇一样

 

或者可以参考:https://blog.csdn.net/xsdxs/article/details/53749059?locationNum=5&fps=1

 

 

 

你可能感兴趣的:(ElasticSearch)