本文将介绍如何通过Hive来读取ElasticSearch中的数据,然后我们可以像操作其他正常Hive表一样,使用Hive来直接操作ElasticSearch中的数据,将极大的方便开发人员。本文使用的各组件版本分别为 Hive0.12、Hadoop-2.2.0、ElasticSearch 2.3.4。

  我们先来看看ElasticSearch中相关表的mapping:

{
    "user": {
        "properties": {
            "regtime": {
                "index": "not_analyzed",
                "type": "string"
            },
            "uid": {
                "type": "integer"
            },
            "mobile": {
                "index": "not_analyzed",
                "type": "string"
            },
            "username": {
                "index": "not_analyzed",
                "type": "string"
            }
        }
    }
}

ElasticSearch中的index名为iteblog,type为user;user有regtime、uid、mobile以及username四个属性。现在我们在Hive端进行操作。

  要让Hive能够操作ElasticSearch中的数据我们需要对Hive进行一些设置。值得高兴的是,ElasticSearch官方为我们提供了一些类库可以实现这些要求。我们需要引入相应的elasticsearch-hadoop-xxx.jar包,因为我们得ElasticSearch版本是2.x的,所以我们最少需要使用ES-Hadoop 2.2.x,本文使用的是elasticsearch-hadoop-2.3.4.jar,这个可以到Maven中央仓库下载。要让Hive能够加载elasticsearch-hadoop-2.3.4.jar文件有好几种方式:

1、直接通过add命令加载,如下:

hive > ADD JAR /home/iteblog/elasticsearch-hadoop-2.3.4.jar;
Added [/home/iteblog/elasticsearch-hadoop-2.3.4.jar] to class path
Added resources: [/home/iteblog/elasticsearch-hadoop-2.3.4.jar]

2、我们还可以在启动Hive的时候进行设置,如下:

$ bin/hive --auxpath=/home/iteblog/elasticsearch-hadoop-2.3.4.jar

3、我们还可以通过设置hive.aux.jars.path属性来实现:

$ bin/hive -hiveconf hive.aux.jars.path=/home/iteblog/elasticsearch-hadoop-2.3.4.jar

或者我们把这个设置直接写到hive-site.xml中,以便后面方便:

<property>
  <name>hive.aux.jars.path</name>
  <value>/home/iteblog/elasticsearch-hadoop-2.3.4.jar</value>
  <description>A comma separated list (with no spaces) of the jar files</description>
</property>

大家可以根据自己实际情况选择设置。设置好ElasticSearch相关类库之后,我们就可以到Hive中创建表了。为了方便,我们直接将Hive中各个字段以及类型设置成和ElasticSearch中一样:

hive (iteblog)> create EXTERNAL  table `user`(
                    >   regtime string,
                    >   uid int,
                    >   mobile string,
                    >   username string
                    > )
                    > STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
                    > TBLPROPERTIES('es.resource' = 'iteblog/user', 'es.nodes'='www.iteblog.com', 'es.port'='9200', 'es.nodes.wan.only'='true');

到这里,我们已经已经可以在Hive里面查询ElasticSearch中的数据了:

hive (iteblog)> select * from  `user` limit 10;
OK
2016-10-24 13:08:16 1   13112121212 Tom
2016-10-24 14:08:16 2   13112121212 Join
2016-10-25 14:23:16 3   13112121212 iteblog
2016-10-25 13:08:16 4   NULL        weixin
2016-10-25 19:08:16 5   13112121212 bbs
2016-10-25 13:14:04 6   NULL        zhangshan
2016-10-25 13:08:16 7   13112121212 wangwu
2016-10-25 14:56:16 8   13112121212 Joan
2016-10-25 15:25:16 9   13112121212 White
2016-10-25 17:24:16 0   NULL        lihhh
Time taken: 0.072 seconds, Fetched: 10 row(s)

如上所述,我们已经成功通过Hive查询到ElasticSearch中的数据了。如果你在通过Hive查询ElasticSearch中的数据遇到如下异常:

Failed with exception java.io.IOException:org.elasticsearch.hadoop.EsHadoopIllegalArgumentException: Cannot detect ES version - typically this happens if the network/Elasticsearch cluster is not accessible or when targeting a WAN/Cloud instance without the proper setting 'es.nodes.wan.only'

这个很可能是因为你配置错了 es.nodes 或者 es.port 属性了。

  在上面的例子中,我们为了方便将Hive中的字段设置成和ElasticSearch中一样;但实际情况下,我们可能无法将Hive中的字段和ElasticSearch保持一致,这时候我们需要在创建Hive表的时候做一些设置,否则将会出现错误。我们可以通过 es.mapping.names 参数实现,如下:

hive (iteblog)> create EXTERNAL  table `user`(
                    >   register_time string,
                    >   user_id int,
                    >   mobile string,
                    >   username string
                    > )
                    > STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
                    > TBLPROPERTIES('es.resource' = 'iteblog/user', 'es.nodes'='www.iteblog.com', 'es.port'='9200', 'es.nodes.wan.only'='true','es.mapping.names'='register_time:regtime,user_id:uid');

然后我们就可以将Hive中的 register_time 映射到ElasticSearch中的 regtime 字段; user_id 映射到ElasticSearch中的 uid 字段。

  在创建Hive表的时候,我们还可以通过制定 es.query 来限制需要查询的数据,如下:

hive (iteblog)> create EXTERNAL  table `user`(
                    >   regtime string,
                    >   uid int,
                    >   mobile string,
                    >   username string
                    > )
                    > STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
                    > TBLPROPERTIES('es.resource' = 'iteblog/user', 'es.nodes'='www.iteblog.com', 'es.port'='9200', 'es.nodes.wan.only'='true','es.query' = '?q=uid:2');

上面的查询仅返回uid为2的数据(关于查询条件设置可以参见《23种非常有用的ElasticSearch查询例子(1)》),然后我们可以看效果:

hive (iteblog)> select * from  `user` limit 10;
OK
2016-10-24 14:08:16 2   13112121212 Join
Time taken: 0.023 seconds, Fetched: 1 row(s)

我们可以看到,uid为2的数据才返回了,其他的数据被过滤了。

  在一些需要启动MapReduce任务来完成的SQL,Hive启动的Map个数和ElasticSearch中的分片个数一致,也就是每个分片使用一个Map任务来处

本文转载自过往记忆(https://www.iteblog.com/)

使用Hive读取ElasticSearch中的数据的更多相关文章

  1. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作(二)

    CSSDesk body { background-color: #2574b0; } /*! zybuluo */ article,aside,details,figcaption,figure,f ...

  2. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作

    http://www.cnblogs.com/wgp13x/p/4934521.html 内容一样,样式好的版本. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据 ...

  3. Spark读取Hbase中的数据

    大家可能都知道很熟悉Spark的两种常见的数据读取方式(存放到RDD中):(1).调用parallelize函数直接从集合中获取数据,并存入RDD中:Java版本如下: JavaRDD<Inte ...

  4. Python中如何读取xls中的数据

    要想读取EXCEL中的数据,首先得下载xlrd包,地址:https://pypi.python.org/pypi/xlrd  安装方法:下载解压后,利用windows  dos命令进入解压目录eg,c ...

  5. 编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时会产生Additional information: 阅读器关闭时尝试调用 Read 无效问题,解决方法与解释

    在自学杨中科老师的视频教学时,拓展编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时 会产生Additional information: 阅读器关闭时尝试调用 ...

  6. 读取redis中的数据时出现:MISCONF Redis is configured to save RDB snapshots

    读取redis中的数据时出现:MISCONF Redis is configured to save RDB snapshots   以下为异常详细信息: Exception in thread &q ...

  7. sql 读取excel中的数据

    select 列名 as 字段名 from openBowSet('MSDASQL.1','driver=Microsoft Excel Driver(*.xls);dbq=文件存放地址','sele ...

  8. java读取请求中body数据

    java读取请求中body数据 /** * 获取request中body数据 * * @author lifq * * 2017年2月24日 下午2:29:06 * @throws IOExcepti ...

  9. SpringBoot(十三)_springboot上传Excel并读取excel中的数据

    今天工作中,发现同事在整理数据,通过excel上传到数据库.所以现在写了篇利用springboot读取excel中的数据的demo.至于数据的进一步处理,大家肯定有不同的应用场景,自行修改 pom文件 ...

随机推荐

  1. 命令行安装kvm虚拟机、桥接网络、用virt-manager管理

    宿主机CentOS Linux release 7.2.1511 (Core),内核3.10.0-327.el7.x86_64 1.配置宿主机网络桥接 想让虚拟机有自己的ip且外网可访问,需要在安装虚 ...

  2. js:return [ expression ],return false,return true,return的区别

    1.return [ expression ] return返回值实际上是对window.event.returnvalue进行设置. 2.return false,阻止默认的行为, ① 当给a标签绑 ...

  3. JavaWeb学习总结——文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  4. Centos6.5+Redmine

    花了两天时间,基于centos6.5操作系统,搭建了redmine环境,在这里记录下过程中遇到的问题以及搭建流程. centos6.5; redmine2.5.0; Ruby1.9.3; step 1 ...

  5. 006-docker-安装-nginx

    1.搜索镜像 docker search nginx 2.拉取合适镜像 docker pull nginx docker images 3.使用镜像 docker run -p 8080:80 --n ...

  6. C++读取dll文件所在目录

    ////保证config.txt从本DLL目录位置读取 //获取DLL自身所在路径(此处包括DLL文件名) }; GetModuleFileNameA((HINSTANCE)&__ImageB ...

  7. 58A

    #include <stdio.h> #include <string.h> int main() { char hel[6]="hello"; char ...

  8. Linux下samba服务搭建

    参考: https://www.cnblogs.com/lxyqwer/p/7271369.html https://www.cnblogs.com/liulipeng/p/3406352.html ...

  9. "pip3 install requests"

    后续设置参考 “selenium python3” https://www.cnblogs.com/jpr-ok/p/10108231.html

  10. async await的用法

    const fs = require('fs'); const readFile = function (fileName) { return new Promise(function (resolv ...