首页 技术 正文
技术 2022年11月16日
0 收藏 862 点赞 4,621 浏览 1632 个字

目录

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848