背景
最近的项目中用hive作为数仓工具,并用pyspark进行数据处理。
后来需要进行一些前端展示,发现检索数据实在是太慢了,尤其是还要进行一些toPandas()的操作,来把pyspark的dataframe导到pandas(),再返回给前端。
看了下解决方案,有两个方案可以考虑。
第一个是用clickhouse来进行检索,用kettle或者waterdrop这种抽数工具把数据导入到clickhouse种。第二种是用hive关联到ES的数据,打通ES和hadoop平台,第二种貌似更好一些。
hive本身是一个工具,通过表格来映射到外部文件,这个文件可以是HDFS,也可以是ES中的数据。这样当需要计算的时候,就把数据加载到pyspark中计算,需要查询的时候,就直接用ES。可进可退。
操作步骤
下载:http://download.elastic.co/hadoop/elasticsearch-hadoop-5.6.4.zip
解压:
进入hive,加载jar包。
add jar file:///root/hadoop/elasticsearch-hadoop-7.13.2/dist/elasticsearch-hadoop-hive-7.13.2.jar;
ES中的index如下:
在hive里建表:
create database if not exists tmp;
drop table if exists tmp.tmp_es;
create external table tmp.tmp_es (id BIGINT, insur_type varchar(16), name varchar(32))
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES('es.nodes' = '10.154.76.25',
'es.port'='9200',
'es.index.auto.create' = 'False',
'es.resource' ='mz_kc24/_doc',
'es.read.metadata' = 'true',
'es.mapping.names' = 'id:id, insur_type:insur_type, name:name',
'es.nodes.wan.only'='true',
'es.index.read.missing.as.empty'='true',
'es.input.use.sliced.partitions'='false',
'es.input.max.docs.per.partition'='1000');
注意,id必须指定为BIGINT,如果指定为INT,会报错。
确认:
select * from tmp.tmp_es limit 10;