![0ef3f55aed0322a4def1933bd9e91ffd.png](https://i-blog.csdnimg.cn/blog_migrate/0959da1f40e65e458e439fb7cecbb00a.png)
点击上方蓝字关注我们
导语
数仓建设的第一步就是数据获取,目前大多数公司都是使用数据同步神器-Datax源码重构/Kettle/Sqoop/MongoDump等工具将外部数据同步到数仓。
本篇主要讲解的是如何基于Hive直接读取外部数据源另关注公众号,后台可获取更多关于大数据/算法/python/java等学习资料和实际应用场景解决方案
Hive读取ES数据
1.下载jar包并加载
1# 下载地址:https://www.elastic.co/cn/downloads/hadoop
2ADD JAR /home/elasticsearch-hadoop-2.3.4.jar;
2.创建映射表![217edf62f1741673cea42a2c275f28c8.png](https://i-blog.csdnimg.cn/blog_migrate/e009a3566737e7c71bdf30ac94d7b4cb.png)
1create external table wedw_ods.span_other_tmp(
2 es_id string,
3 id string,
4 parent_id string,
5 service_name string,
6 kind string,
7 name string,
8 timestamp bigint,
9 duration bigint,
10 error boolean,
11 ip string,
12 trace_id string,
13 reference string,
14 tags array<struct<key:string,value:string>>,
15 baggages array<struct<key:string,value:string>>
16 )
17 -- ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
18 ROW FORMAT SERDE 'org.elasticsearch.hadoop.hive.EsSerDe'
19 -- WITH SERDEPROPERTIES ("case.insensitive" = "false","mapping.baggages_key"="baggages")
20STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
21TBLPROPERTIES(
22 'es.resource' = 'index:other-2020-04-08/span', --指定索引和type
23 'es.index.auto.create' = 'false', --是否自动创建索引
24 'es.query'='?q=_id:AXFWjbHX9yjdMoK7JOl3', --指定查询读取
25 'es.nodes'='', --指定es节点ip
26 'es.field.read.empty.as.null'='true', --是否读取空字段
27 'es.port' = '9200', --指定es 节点端口
28 'es.nodes.wan.only' = 'true',
29 'es.nodes.discovery' = 'false', --是否开启自动探查
30 'es.index.read.missing.as.empty'='true',
31 'es.read.metadata'='true',
32 'es.batch.size.entries' = '10000', -- 每次读取的批次数
33 'es.scroll.size' = '10000', --每次翻页数
34 -- 'es.output.json' = 'true', --以json格式输出
35 'es.read.field.include' = '_id,id,parentId,serviceName,traceId,kind,name,timestamp,duration,error,ip,trace_id,tags,reference,tags.key,tags.value,baggages.key,baggages.value', --只读取指定字段
36 'es.mapping.names'='es_id:_metadata._id,parent_id:parentId,service_name:serviceName,trace_id:traceId,tags:tags', --映射字段 hive字段:es字段
37 'es.read.field.as.array.include' = 'tags,baggages' --以数组的方式读取指定字段
38);
3.遇到的问题
1.通常在做es映射的时候,需要保持hive的字段类型和es中的mapping类型要一致。
2.es-hadoop.jar包要和es版本和hive版本要兼容。否则会抛出“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'”错误
Hive映射Mongo
1.使用直连的方式
前提需要的jar包: mongo-hadoop-core-2.0.0.jar mongo-hadoop-hive-2.0.0.jar mongo-java-driver-3.4.2.jar
1CREATE TALBE tmp.test
2( id string,
3 name string
4)
5STORED BY 'com.mongodb.hadoop.hive.MongoStorageHandler'
6WITH SERDEPROPERTIES ('mongo.columns.mapping'='{"id":"_id"}')
7TBLPROPERTIES ('mongo.uri' = 'mongodb://user:password@ip:port/db.collection');
2.使用Bson文件的方式
2.1 导出使用mongodump导出Bson文件
1mongodump -u userName -p Password -h ip:port --collection CollectName --db dbName -o target_Path
2.2 将Bson文件导入hdfs
1hdfs dfs -put target_Path hdfs_target_path
2.3 Hive读取Bson文件
1-- 前提需要jar包:
2-- mongo-hadoop-core-2.0.0.jar
3-- mongo-hadoop-hive-2.0.0.jar
4-- mongo-java-driver-3.4.2.jar
5-- 建表语句:
6create table if not exists ${table_name}
7(field type comment "")
8row format serd ‘com.mongodb.hadoop.hive.BSONSerDe’
9with serdeproperties('mongo.columns.mapping'='{hive字段与mongo字段的映射关系(如果hive字段名和mongo中的字段名一致,可省略)}')
10stored as inputformat 'com.mongodb.hadoop.mapred.BSONFileInputFormat'
11outputformat 'com.mongodb.hadoop.hive.output.HiveBSONFileOutputFormat'
12location ‘HDFS的目录’
Hive映射HBase
1create external table if not exists wedw_ods.log_status_from_hbase
2(
3m_id string
4,order_info_id string
5,before_status int
6,after_status int
7,status_type int
8,description string
9,gmt_created string
10,gmt_modified string
11)
12stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
13with serdeproperties ("hbase.columns.mapping" = ":key,f:order_info_id,f:before_status,f:after_status,f:status_type,f:description,f:gmt_created,f:gmt_modified")
14tblproperties ("hbase.table.name" = "log:log_status");
注意:该Hive表一个外部表,所以删除该表并不会删除HBase表中的数据
1、建表或映射表的时候如果没有指定:key则第一个列默认就是行键
2、HBase对应的Hive表中没有时间戳概念,默认返回的就是最新版本的值
3、由于HBase中没有数据类型信息,所以在存储数据的时候都转化为String类型
最后说明
以上方法是直接以hive表的形式读取外部数据,这种方式最大的特点就是简单。
但是也有很大的弊端,那就是在读取或者写入表的时候会对外部存储系统造成负载过高。
通常结合实际的系统负载以及数据量综合评估后,再行选择具体的同步方式。
●2020年大厂面试题-数据仓库篇
●数据同步神器-Datax源码重构
●十分钟搞定分布式一致性算法
●zookeeper源码解读之-DataTree模型构建+Leader选举
●zookeeper源码解读之-服务端启动流程
●一文教你如何玩转zookeeper
●zookeeper应用场景解决方案-Leader选
●实战:如何实时采集上亿级别数据?
●Kafka深度剖析HW以及LEO
●Livy REST 提交Spark作业
●Spark集成ElasticSearch
●Spark数据倾斜之骚操作解决方案
●一道简单的算法面试题
●Impala介绍以及常见问题
●ElasticSearch无感知重构索引●ElasticSearch分页搜索以及deep paging性能揭秘
●ElasticSearch 一个field索引两次解决排序问题
●ElasticSearch Partial Update大揭秘
●Hive常见问题汇总