环境

elasticsearch和logstash和kibana(可视化工具)的版本要一致,我用的是6.0.0,操作上linux和windows差不多,我这是windows。重点在logstash的配置文件上。

一、安装部署

自行搜一下elk(elasticsearch+logstash+kibana)的安装,这里不是重点不赘述了。

二、启动

  1. 首先启动elasticsearch
    找到es安装目录的bin目录elasticsearch-6.0.0\bin
  2. es数据迁移使用 Logstash后结构不一致_大数据

  3. windows直接点击elasticsearch.bat来启动,linuxs则进入elasticsearch安装目录输入./bin/elasticsearch来启动。windows环境会弹出来一个cmd的命令框不要关闭,这是es程序在运行。
  4. 使用浏览器(我用的是火狐)输入http://localhost:9200/得到如下界面则表示启动成功。

es数据迁移使用 Logstash后结构不一致_elasticsearch_02

  1. 把kibana也先启动起来同样到kibana的安装目录的bin目录下点kibana.bat来启动
    D:\kibana-6.0.0-windows-x86_64\bin
    同样会弹出来一个cmd框
  2. es数据迁移使用 Logstash后结构不一致_elasticsearch_03

  3. 用浏览器访问http://localhost:5601

es数据迁移使用 Logstash后结构不一致_es_04


点小扳手(7.0以后的界面可能不一样但是同样是点小扳手)

es数据迁移使用 Logstash后结构不一致_mysql_05

到此证明es和kibana安装并启动成功

二、logstash配置文件

  1. 在logstash安装目录下新建文件夹config_mysql
    D:\logstash-6.0.0\config-mysql
  2. 创建load_data.conf用来写配置,内容如下(这个配置文件是关键,建议弄清楚每个字段的意思)
input {
    jdbc {
	  # mysql jdbc connection string to our backup databse  
	  jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/petshop?characterEncoding=utf8&useSSL=false"
	  # the user we wish to excute our statement as
	  jdbc_user => "root"
	  jdbc_password => "123456"
	  # the path to our downloaded jdbc driver
	  jdbc_driver_library => "D:/logstash-6.0.0/config-mysql/mysql-connector-java-5.1.43.jar"
	  # the name of the driver class for mysql
	  jdbc_driver_class => "com.mysql.jdbc.Driver"
	  jdbc_paging_enabled => "true"
	  jdbc_page_size => "50000"
	  #注意
	  #1.这里不要少了唯一标识id,或者其他主键字段
	  #2.把含有'type'的字段as为其他名字,否则会覆盖logstash的type
	  #3.注意数据库的时区是否相同
	  statement => "SELECT u.id,u.email,u.account,c.good_name,c.count,c.price
						FROM users u
						LEFT JOIN car c
						ON u.id = c.user_id 
						where u.modified_at > :sql_last_value
						OR c.update_time > :sql_last_value"
	  
	#定时字段,一分钟更新一次,其他需求自行搜索
		schedule => "* * * * *"
	#指定路径,car文件会自动生成,用来存放sql_last_value的值,一个jdbc对应一个
		last_run_metadata_path => "D:/logstash-6.0.0/config-mysql/run/car"   
	#设定ES索引类型
	type => "car"
  }
    jdbc {
	  # mysql jdbc connection string to our backup databse  
	  jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/petshop?characterEncoding=utf8&useSSL=false"
	  # the user we wish to excute our statement as
	  jdbc_user => "root"
	  jdbc_password => "123456"
	  # the path to our downloaded jdbc driver
	  jdbc_driver_library => "D:/logstash-6.0.0/config-mysql/mysql-connector-java-5.1.43.jar"
	  # the name of the driver class for mysql
	  jdbc_driver_class => "com.mysql.jdbc.Driver"
	  jdbc_paging_enabled => "true"
	  jdbc_page_size => "50000"
	  #注意
	  #1.这里不要少了唯一标识id,或者其他主键字段
	  #2.把含有'type'的字段as为其他名字,否则会覆盖logstash的type
	  #3.注意数据库的时区是否相同
	  statement => "SELECT * FROM goods"
	  
	#定时字段,一分钟更新一次,其他需求自行搜索
		schedule => "* * * * *"
	#指定路径,car文件会自动生成,用来存放sql_last_value的值,一个jdbc对应一个
		last_run_metadata_path => "D:/logstash-6.0.0/config-mysql/run/goods"   
	#设定ES索引类型
	type => "goods"
  }
}

filter {
  json {
  source => "message"
  remove_field => ["message"]
  }
}

output {

	if[type] == "car"{
		elasticsearch {
			hosts => ["127.0.0.1:9200"]
			index => "car_index"
			document_type => "jdbc"
			#此处建议为主键字段id
			document_id => "%{id}"
		}
    }
		if[type] == "goods"{
		elasticsearch {
			hosts => ["127.0.0.1:9200"]
			index => "goods_index"
			document_type => "jdbc"
			#此处建议为主键字段id
			document_id => "%{id}"
		}
    }
	


  stdout {
#以JSON格式输出
  codec => json_lines
  }
}
  1. 运行logstash命令如下,我这使用的gitbash
$ nohup.exe ./bin/logstash.bat -f config-mysql/load_data.conf &

  1. 注:nohup.exe和&都表示后台运行,可以不加,它俩之间的区别自行搜索。
    nohup会把日志输出nohup.out
    输出日志在这。

三、验证是否上传成功

查看所有索引

GET _cat/indices

es数据迁移使用 Logstash后结构不一致_bc_06

查看索引的数据

es数据迁移使用 Logstash后结构不一致_elasticsearch_07

至此导入数据成功。若碰到问题可以在评论里问。