elasticSearch学习笔记

elasticSearch 安装与运行

1
2
3
4
5
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.3.tar.gz
tar -xzvf elasticsearch-5.6.3.tar.gz
cd elasticsearch-5.6.3/bin
./elasticsearch

验证安装是否成功

浏览器或者curl,wget http://localhost:9200/
正常即成功

kibana安装与运行

1
2
3
4
5
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.6.3-linux-x86_64.tar.gz
tar -xzvf kibana-5.6.3-linux-x86_64
cd kibana-5.6.3-linux-x86_64/bin
./kibana

kibana插件sense 主要解决curl 交互不方便
以前的安装方式 ./kibana-plugin install elastic/sense

官网的安装方法已经过时了,在es 2.x是适用的,但是5.x后其实不再需要sense插件,因为kibana 界面有dev tools

kibana 使用

访问端口 5601 http://localhost:5601/
在dev tools 进行交互

elasticSearch 基本操作

{put|get|post|delete} + json

elasticSearch 基本查询

查询全部记录

基本格式: GET /index/docment/id

GET /log/test1/_search

根据某个字段进行查询(单个) match

GET /log/test1/_search?q=name:shuai

Elasticsearch 提供了更加丰富灵活的查询语言,它被称作 Query
DSL,通过它你可以完成更加复杂、强大的搜索任务

1
2
3
4
5
6
7
8
GET /log/test1/_search
{
"query": {
"match": {
"name": "shuai"
}
}
}

根据某个字段范围查询(多个)

1
2
3
4
5
6
7
8
9
10
11
GET /log/weblog/_search
{
"query": {
"range": {
"no": {
"gte": 10,
"lte": 20
}
}
}
}
  1. gt :: 大于
  2. gte:: 大于等于
  3. lt :: 小于
  4. lte:: 小于等于

    elasticSearch 操作符及关键字

    可通过dev tools 的自动提示解决

and 查询

多字段查询

elasticSearch性能

weblog表(102537575行) 时间
hive 47.553 seconds
es 瞬间

es hive 整合

添加jar包

1
2
3
hive> add jar /opt/jars/elasticsearch-hadoop-hive-5.6.3.jar;
# Added [/opt/jars/elasticsearch-hadoop-hive-5.6.3.jar] to class path
# Added resources: [/opt/jars/elasticsearch-hadoop-hive-5.6.3.jar]

创建外部表

1
2
3
hive>CREATE EXTERNAL TABLE es_weblog(no int,ip string, time string)
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES('es.resource' = 'log/weblog','es.index.auto.create' = 'true','es.mapping.names' = 'no:no,ip:ip,time:time');

es.mapping.names 是建立mapping ,前面为内部表的字段,后面为外部表的字段

遇到的问题

es.mapping.names 字段对照错误

导入数据

1
hive> INSERT OVERWRITE TABLE es_weblog select no,ip,time from weblog;

这个过程是比较慢的