hive 操作elasticsearch

一,从hive 表格向elasticsearch 导入数据

1,首先,创建elasticsearch 索引,索引如下

  1. curl -XPUT '10.81.179.209:9200/zebra_info_demo?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "settings": {
  4. "number_of_shards":5,
  5. "number_of_replicas":2
  6. },
  7. "mappings": {
  8. "zebra_info": {
  9. "properties": {
  10. "name" : {"type" : "text"},
  11. "type": {"type": "text"},
  12. "province": {"type": "text"},
  13. "city": {"type": "text"},
  14. "citycode": {"type": "text", "index": "no"},
  15. "district": {"type": "text"},
  16. "adcode": {"type": "text", "index": "no"},
  17. "township": {"type": "text"},
  18. "bausiness_circle": {"type": "text"},
  19. "formatted_address": {"type": "text"},
  20. "location": {"type": "geo_point"},
  21. "extensions": {
  22. "type": "nested",
  23. "properties": {
  24. "map_lat": {"type": "double", "index": "no"},
  25. "map_lng": {"type": "double", "index": "no"},
  26. "avg_price": {"type": "double", "index": "no"},
  27. "shops": {"type":"short", "index": "no"},
  28. "good_comments": {"type":"short", "index": "no"},
  29. "lvl": {"type":"short", "index": "no"},
  30. "leisure_type": {"type": "text", "index": "no"},
  31. "fun_type": {"type": "text", "index": "no"},
  32. "numbers": {"type": "short", "index": "no"}
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }
  39. '

2,查看elasticsearch版本,下载相应的elasticsearch-hive-hadoop jar 包

可以用如下命令查看elastic search 的版本

本文版本5.6.9

到如下maven 官网下载jar 包。

https://repo.maven.apache.org/maven2/org/elasticsearch/elasticsearch-hadoop-hive/

选择正确的版本即可。

3, 把下载下来的jar 包上传到hdfs 路径下。

本文jar 包路径,hdfs:///udf/elasticsearch-hadoop-hive-5.6.9.jar

4,哦了,建表,用起来

  1. DELETE jars;
  2. add jar hdfs:///udf/elasticsearch-hadoop-hive-5.6.9.jar;
  3. drop table zebra_info_demo;
  4. CREATE EXTERNAL TABLE zebra_info_demo(
  5. name string,
  6. `type` string,
  7. province double,
  8. city string,
  9. citycode string,
  10. district string,
  11. adcode string,
  12. township string,
  13. business_circle string,
  14. formatted_address string,
  15. location string,
  16. extensions STRUCT<map_lat:double, map_lng:double, avg_price:double, shops:smallint, good_comments:smallint, lvl:smallint, leisure_type:STRING, fun_type:STRING, numbers:smallint>
  17. )
  18. STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
  19. TBLPROPERTIES('es.nodes' = '10.81.179.209:9200',
  20. 'es.index.auto.create' = 'false',
  21. 'es.resource' = 'zebra_info_demo/zebra_info',
  22. 'es.read.metadata' = 'true',
  23. 'es.mapping.names' = 'name:name, type:type, province:province, city:city, citycode:citycode, district:district, adcode:adcode, township:township, business_circle:business_circle, formatted_address:formatted_address, location:location, extensions:extensions');

5, 往里面填充数据,就O了。

  1. INSERT INTO TABLE zebra_info_demo
  2. SELECT
  3. a.name,
  4. a.brands,
  5. a.province,
  6. a.city,
  7. null as citycode,
  8. null as district,
  9. null as adcode,
  10. null as township,
  11. a.business_circle,
  12. null as formatted_address,
  13. concat(a.map_lat, ', ', a.map_lng) as `location`,
  14. named_struct('map_lat', cast(a.map_lat as double), 'map_lng',cast(a.map_lng as double) ,'avg_price', cast(0 as DOUBLE), 'shops', 0S, 'good_comments', 0S, 'lvl', cast(a.lv1 as SMALLINT), 'leisure_type', '', 'fun_type', '', 'numbers', 0S) as extentions
  15. from medicalsite_childclinic a;

运行结果:

二,已知elasticsearch 索引,然后,建立hive 表格和elasticsearch 进行交互。可以join 哦,一个字,liubi

1,先看一下索引和数据

已知索引如下:

  1. curl -XPUT '10.81.179.209:9200/join_tests?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "cities": {
  5. "properties": {
  6. "province": {
  7. "type": "string"
  8. },
  9. "city": {
  10. "type": "string"
  11. }
  12. }
  13. }
  14. }
  15. }
  16. }
  17. '
  18. curl -XPUT '10.81.179.209:9200/join_tests1?pretty' -H 'Content-Type: application/json' -d'
  19. {
  20. "mappings": {
  21. "shop": {
  22. "properties":{
  23. "name": {
  24. "type": "string"
  25. },
  26. "city": {
  27. "type": "string"
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
  34. '

数据如下:

2,建立表格,写一堆有毒的sql 语句。

  1. DELETE jars;
  2. add jar hdfs:///udf/elasticsearch-hadoop-hive-5.6.9.jar;
  3. create table join_tests(
  4. province string,
  5. city string
  6. )STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
  7. TBLPROPERTIES('es.nodes' = '10.81.179.209:9200',
  8. 'es.index.auto.create' = 'false',
  9. 'es.resource' = 'join_tests/cities',
  10. 'es.read.metadata' = 'true',
  11. 'es.mapping.names' = 'province:province, city:city');
  12. create table join_tests1(
  13. name string,
  14. city string
  15. )STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
  16. TBLPROPERTIES('es.nodes' = '10.81.179.209:9200',
  17. 'es.index.auto.create' = 'false',
  18. 'es.resource' = 'join_tests1/shop',
  19. 'es.read.metadata' = 'true',
  20. 'es.mapping.names' = 'name:name, city:city');
  21. SELECT
  22. a.province,
  23. b.city,
  24. b.name
  25. from join_tests a LEFT JOIN join_tests1 b on a.city = b.city;

3,运行结果

结束语

推荐一个useful 的工具, apache Hue, 可以用来管理hdfs 文件,hive 操作。mysql 操作等。

hive 学习系列五(hive 和elasticsearch 的交互,很详细哦,我又来吹liubi了)的更多相关文章

  1. hive 学习系列六 hive 去重办法的思考

    方法1,建立临时表,利用hive的collect_set 进行去重. create table if not exists tubutest ( name1 string, name2 string ...

  2. hive 学习系列之七 hive 常用数据清洗函数

    1,case when 的利用,清洗诸如评分等的内容,用例如下. case when new.comment_grade = '五星商户' then 50 when new.comment_grade ...

  3. Hive学习 系列博客

    原 Hive作业优化 原 Hive学习六:HIVE日志分析(用户画像) 原 Hive学习五--日志案例分析 原 Hive学习三 原 Hive学习二 原 Hive学习一 博客来源,https://blo ...

  4. scrapy爬虫学习系列五:图片的抓取和下载

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  5. 大数据学习系列之五 ----- Hive整合HBase图文详解

    引言 在上一篇 大数据学习系列之四 ----- Hadoop+Hive环境搭建图文详解(单机) 和之前的大数据学习系列之二 ----- HBase环境搭建(单机) 中成功搭建了Hive和HBase的环 ...

  6. Hadoop Hive概念学习系列之hive三种方式区别和搭建、HiveServer2环境搭建、HWI环境搭建和beeline环境搭建(五)

     说在前面的话 以下三种情况,最好是在3台集群里做,比如,master.slave1.slave2的master和slave1都安装了hive,将master作为服务端,将slave1作为服务端. 以 ...

  7. Hive学习之六 《Hive进阶— —hive jdbc》 详解

    接Hive学习五 http://www.cnblogs.com/invban/p/5331159.html 一.配置环境变量 hive jdbc的开发,在开发环境中,配置Java环境变量 修改/etc ...

  8. 【Hive学习之八】Hive 调优【重要】

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 apache-hive-3.1.1 ...

  9. 【Hive学习之一】Hive简介

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 apache-hive-3.1.1 ...

随机推荐

  1. 【起航计划 036】2015 起航计划 Android APIDemo的魔鬼步伐 35 App->Service->Messenger Service Messenger实现进程间通信

    前面LocalService 主要是提供同一Application中组件来使用,如果希望支持不同应用或进程使用Service.可以通过Messenger.使用Messgener可以用来支持进程间通信而 ...

  2. Asterisk 对wav格式的支持

    经过测试wav格式文件仅支持PCM 8000kHz 16bit 单声道,非常蛋疼的一个原因,排查了好久! 关于C#支持的一些格式(Mono 单声道 .Stereo 立体声道) // Standard ...

  3. JAVA利用poi获取world文件内容

    本文主要简单介绍了利用poi包,读取world文件内容. 这个依然存在版本的问题,只能读取doc结尾的老版本文件. 话不多说,上代码: import java.io.File; import java ...

  4. SpringCloud实战3-Hystrix请求熔断与服务降级

    我们知道大量请求会阻塞在Tomcat服务器上,影响其它整个服务.在复杂的分布式架构的应用程序有很多的依赖,都会不可避免地在某些时候失败.高并发的依赖失败时如果没有隔离措施,当前应用服务就有被拖垮的风险 ...

  5. 写一个简单的shellcode

    0x00 前言 漏洞利用中必不可缺的部分就是shellcode,不会编写shellcode和咸鱼有什么区别,跳出咸鱼第一步. 0x01 系统调用 通过系统调用execve函数返回shell C语言实现 ...

  6. miniMobile(手机)

    官网:http://www.web2014.cn/

  7. 【洛谷P1118】数字三角形

    数字三角形 题目链接 4 16 3 1 2 4 3 1 2 4 (3+1) (1+2) (2+4)(3+1+1+2) (1+2+2+4) (3+1+1+1+2+2+2+4)16=1*3+3*1+3*2 ...

  8. 【题解】洛谷P3205【HNOI2010】合唱队

    洛谷 P3205:https://www.luogu.org/problemnew/show/P3205 复习区间DPing 思路 把理想队列拆分成 第一个和后面几个 划分成求后面几个的理想队列 最后 ...

  9. notepad++括号自动补全插件: XBracket Lite

    1.4.5.1. 通过XBracket Lite实现括号的自动补全 先去打开相应的设置: 再根据自己的需要去设置: 其中解释一下相应的选项的含义: Treat'' as brackets 把单引号', ...

  10. windows mysql密码设置与破解

    1.设置密码 mysqladmin -uroot -p password "1234" 查看当前用户: 2.破解密码 关闭 MySQL 服务 执行 mysqld --skip-gr ...