本文介绍elasticsearch的从mysql导入数据
0. 数据准备
create database shopdb
CREATE TABLE `goods` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '0',
`name` varchar(255) NOT NULL DEFAULT '' COMMENT '商品名称',
`shop_name` varchar(255) NOT NULL DEFAULT '' COMMENT '店铺名称',
`price` int(20) NOT NULL DEFAULT '0' COMMENT '商品价格',
`created_at` datetime NOT NULL COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=50562 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='商品表';
数据准备:
INSERT INTO ``.`` (`id`, `name`, `shop_name`, `price`, `created_at`) VALUES ('1', '卫浴', '万元有限公司', '3000', '2018-05-04 16:09:16');
INSERT INTO ``.`` (`id`, `name`, `shop_name`, `price`, `created_at`) VALUES ('2', '[白金宫殿] 新古典 盛世臻品 高档头层牛皮沙发 奢华双面雕花沙发套装(1+2+3)', '万都国际家居', '9900', '2017-07-08 14:51:16');
INSERT INTO ``.`` (`id`, `name`, `shop_name`, `price`, `created_at`) VALUES ('3', ' 陶瓷加厚蹲便器防臭蹲厕 送冲水箱 前出水', '中山之巅', '45900', '2017-07-21 16:22:21');
INSERT INTO ``.`` (`id`, `name`, `shop_name`, `price`, `created_at`) VALUES ('4', '屏风', '中式之风', '1100', '2018-02-09 12:55:20');
INSERT INTO ``.`` (`id`, `name`, `shop_name`, `price`, `created_at`) VALUES ('5', '木柜', '中策建材名品汇', '200000', '2018-03-15 14:22:37');
INSERT INTO ``.`` (`id`, `name`, `shop_name`, `price`, `created_at`) VALUES ('6', '欧式套装门', '中龙', '9900', '2017-07-09 17:46:27');
1. 下载并安装logstash
1.1 下载地址
https://www.elastic.co/downloads/logstash#ga-release
我用的环境是windows,选择的是zip下载包
1.2 安装
直接解压即可
2. 配置文件
2.1 下载jdbc包
使用mysql需要下载相应的jdbc包,
下载地址可以参考:https://dev.mysql.com/downloads/connector/j/
下拉选择框中选择平台独立:Platform Independent
然后选择对应的版本下载
2.2 更改配置文件
logstash-6.6.0/config/
目录下默认提供了一个参考配置文件logstash-sample.conf
我们复制并修改为logstash-mysql.conf
配置内容如下:
input {
jdbc {
jdbc_connection_string => "jdbc:mysql://localhost:3306/shopdb"
# 数据库连接用户名 和 密码
jdbc_user => "root"
jdbc_password => ""
# jdbc jar包下载地址
jdbc_driver_library => "C:/Users/Administrator/Downloads/logstash-6.6.0/bin/mysql-connector-java-5.1.47.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
# 我们需要导入数据使用的查询语句
statement => "SELECT * FROM goods"
}
}
output {
# 标准输出行存储
stdout { codec => json_lines }
elasticsearch {
# es主机
"hosts" => "localhost:9200"
# 数据库index
"index" => "shopdb"
# 文档类型(表)
"document_type" => "goods"
# 文档id 注意变量使用 %{var} 其中var小写
"document_id" => "%{id}"
}
}
更多配置扩展可参看这篇文章:https://qbox.io/blog/migrating-mysql-data-into-elasticsearch-using-logstash
3. 启动
logstash-6.6.0\bin>logstash -f …\config\logstash-mysql.conf
启动时可能报错(该报错不影响正常数据导入):
启动logstash报错:Unable to get Charset sun.stdout.encoding using default UTF-8
参考这篇文章解决:https://blog.csdn.net/u014078154/article/details/79199215
5. 查询
浏览器输入:localhost:9200/shopdb/goods/_search
即可查看所有记录