• 使用ES-Hadoop 6.5.4编写MR将数据索引到ES


    1. 开发环境

    • Elasticsearch 6.5.4
    • ES-Hadoop 6.5.4
    • Hadoop 2.0.0

    2. 下载地址

    ES-Hadoop下载地址如下:

    官网地址:https://www.elastic.co/downloads/past-releases

    3. 使用示例

    ES-Hadoop插件使用非常简单,只要在作业中导入jar包,在作业描述类中设置一些属性,就可以了,其他部分操作和一般的MR作业并没有太大差别。

    3.1 导入jar包

    下载好插件解压后,可以看见其中包含对应许多hadoop组件的jar包(hive、pig等),只需要将自己需要的jar包添加项目中,因为这里我只是将hbase里的数据索引到ES中,所以只需要添加elasticsearch-hadoop-mr-6.5.4.jar这个jar包。

    :还需要将所用jar包添加到hadoop的classPath中,否则运行作业时会报找不到类的错误。

    3.2 编写描述类

    只需要添加如下设置:

    
    //禁止speculative机制,该机制会启动多个相同task,使数据重复索引
    conf.setBoolean("mapred.map.tasks.speculative.execution", false);  
    conf.setBoolean("mapred.reduce.tasks.speculative.execution", false);
    //设置ES集群中任意节点的IP地址和端口号
    conf.set("es.nodes", "http://节点IP:9200");
    //设置要索引的index/type
    conf.set("es.resource", "mytest/rec");
    //设置输入的数据格式为json
    conf.set("es.input.json", "yes");
    //设置json中文档id对应的字段名
    conf.set("es.mapping.id", "id");
    
    //设置输出格式为EsOutputFormat类
    job.setOutputFormatClass(EsOutputFormat.class);
    //不需要reduce,map也不需要key,所以将map类的key设置为NullWritable
    job.setMapOutputKeyClass(NullWritable.class);
    //将map的value类型设置为Text
    job.setMapOutputValueClass(Text.class);
    
    

    3.3 编写Mapper类

    
    
    public class MixRecMapper extends Mapper<LongWritable,Text,NullWritable,Text>{
    
    		
    	@Override
    	protected void map(LongWritable offset, Text userId,org.apache.hadoop.mapreduce.Mapper.Context context)throws IOException, InterruptedException {
    		
    		//前面为省略的业务逻辑代码
    		
    		//jsonDoc为自行拼接的json字符串
    		String jsonDoc = "{"id":"" + userId.toString() + "","mix_rec":" + mixList.toString() + "}";
    //				logger.info(jsonDoc+" 入ES的json===========================");
    				context.write(NullWritable.get(),new Text(jsonDoc));
    	}
    	
    }
    
    

    4. 参考文献

    官方文档:https://www.elastic.co/guide/en/elasticsearch/hadoop/current/mapreduce.html

  • 相关阅读:
    xshell入门及Linux常用命令
    C++之vector
    c++ 之 string
    引用 与 指针
    关于时间复杂度的计算以及相关概念
    位运算
    thymeleafDemo
    面试总结
    关于mvvm原理实现,模拟vue(3)-----发布订阅
    关于mvvm原理实现,模拟vue(2)-----模板编译
  • 原文地址:https://www.cnblogs.com/liminghuang/p/10314182.html
Copyright © 2020-2023  润新知