八、使用elasticsearch + kibana + logstash 导入mysql数据和创建索引实
一、mysql 数据准备
因为我的 Mysql 是在windows 上的,所以在windows中要设置 mysql 容许远程访问。关于具体设置参见 mysql如何修改开启允许远程连接 (windows)。
本次实践用表:
二、kibana中创建索引
PUT booklist
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"doc" : {
"dynamic" : "false",
"properties": {
"id":{
"type": "integer"
},
"title":{
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"content":{
"type": "text",
"store": false,
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"createDate":{
"type": "date",
"index": false,
"store": false,
"format": ["yyyy-MM-dd HH:mm:ss"]
}
}
}
}
}
备注:
"mappings": {...}
下配置的是索引的type
,这里使用的是doc
,是因为elasticsearch
自动给导入的数据的type
设置为doc
,而es6.x
不再像以前一样容许存在多个type
,所以,这里只能配置为doc
。
可以使用 GET /_cat/indices?v 测试索引是否创建成功。
三、logstash中导入数据
1、在 logstash 的安装目录下的bin文件夹下,新建config-mysql目录
2、新建三个文件:booklist.sql、mysql.conf、station_parameter.txt
- booklist.sql内容:
select * from booklist
- station_parameter.txt:用于增量导入,写入每次导入数据的最后一个id
- mysql.conf:导入数据的配置文件
3、配置mysql.conf
input {
stdin{
}
jdbc {
// mysql 数据库链接,test为数据库名
jdbc_connection_string => "jdbc:mysql://192.168.10.1:3306/test"
// 用户名和密码
jdbc_user => "root"
jdbc_password => "root"
// 驱动(需单独下载)
jdbc_driver_library => "/usr/local/mysql-connector-java-5.1.47-bin.jar"
// 驱动类名
jdbc_driver_class => "com.mysql.jdbc.Driver"
//处理中文乱码问题
codec => plain {charset => "UTF-8"}
//使用其它字段追踪,而不是用时间(这里是用来实现增量更新的)
use_column_value => true
//追踪的字段
tracking_column => id
//是否记录上次执行结果, 如果为真,将会把上次执行到的 tracking_column 字段的值记录下来,保存到 last_run_metadata_path 指定的文件中
record_last_run => true
//上一个sql_last_value值的存放文件路径, 必须要在文件中指定字段的初始值
last_run_metadata_path => "/usr/local/logstash-6.2.2/bin/config-mysql/station_parameter.txt"
//开启分页查询
jdbc_paging_enabled => true
jdbc_page_size => 300
//执行的sql 文件路径+名称(这个sql文件里需要写如下语句:select * from booklist),也可以直接替换成 statement => "select * from booklist"
statement_filepath => "/usr/local/logstash-6.2.2/bin/config-mysql/booklist.sql"
//设置监听间隔 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新
schedule => "* * * * *"
//索引类型(参见上面解释)
type => "doc"
}
}
filter{
//下面:当使用Logstash自动生成的mapping模板时过滤掉@timestamp和@version字段
mutate { remove_field => ["@timestamp","@version"] }
}
output {
elasticsearch {
//hosts:一般是localhost:9200
hosts => ["localhost:9200"]
//索引名
index => "booklist"
//表示按照id同步mysql数据
document_id => "%{id}"
}
stdout {
codec => json_lines
}
}
4、启动 logstash:./logstash -f config-mysql/mysql.conf
四、kibana查询数据
1、查询 content 字段中有 “套装”的记录
GET /booklist/_search
{
"query": {
"term": {
"title": {
"value": "套装"
}
}
},
"from": 0,
"size": 3
}
2、查询结果
{
"took": 16,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.7697402,
"hits": [
{
"_index": "booklist",
"_type": "doc",
"_id": "11",
"_score": 1.7697402,
"_source": {
"id": 11,
"imgurl": "http://img3.doubanio.com/lpic/s26722258.jpg",
"author": 2260,
"@version": "1",
"title": "爱不释手的套装书系列(历史篇)",
"status": "enable",
"createdate": "2016-11-11T04:20:36.000Z",
"content": """
岁月如歌,悠悠岁月,几多愁?
一本书,一段尘封已久的历史。
究竟有多少的历史谜团,为后人所迷惑?
以史为鉴,只有熟知历史,牢记历史,方能立!
""",
"type": "doc",
"@timestamp": "2019-02-11T12:18:00.288Z"
}
},
{
"_index": "booklist",
"_type": "doc",
"_id": "12",
"_score": 1.6377631,
"_source": {
"id": 12,
"imgurl": "https://img1.doubanio.com/lpic/s27023579.jpg",
"author": 2260,
"@version": "1",
"title": "爱不释手的套装书系列(文学篇)",
"status": "enable",
"createdate": "2016-11-16T14:09:08.000Z",
"content": """
饱含情感的文字,
深情款款的期盼。
人非草木,孰能无情?
那段逝去的故事,孰知?
""",
"type": "doc",
"@timestamp": "2019-02-11T12:18:00.288Z"
}
}
]
}
}