# Windows环境安装Logstash和Logstash-input-jdbc

author: diboot team , publish date: 2019.07.22

本文为Windows环境下的相关安装示例,适用于需要用Logstash和Logstash-input-jdbc将数据从数据库导入到Elasticsearch中的场景。

# 安装步骤

# 0. 安装JDK 8及Elasticsearch

安装JDK8并配置JAVA_HOME环境变量; 安装Elasticsearch并启动;

# 1. 安装Logstash

# 1.1 下载与你的Elasticsearch版本对应的Logstash版本

Logstash官方下载地址 (opens new window)

# 1.2 解压到本地路径

如: “D:\Server\logstash-6.8.0”

# 1.3 启动与验证

打开CMD命令行,切换到logstash安装路径下的bin目录,执行:

logstash -e "input { stdin { } } output { stdout {} }"
1

正常可以看到如图打印启动成功的信息。

启动成功的信息

可以输入测试内容,如“hello world” 进一步验证。

# 2. 安装Logstash-input-jdbc插件

CMD命令行切换到bin目录,执行以下命令安装Logstash-input-jdbc插件:

logstash-plugin install logstash-input-jdbc
1

安装Logstash-input-jdbc插件 安装正常将打印成功信息。

# 3. 验证Logstash-input-jdbc

# 3.1 以MySQL为例,下载驱动jar到本地目录(此处以logstash-6.8.0/test路径为例)

# 3.2 写测试的conf文件(如 test.conf)

input {
  stdin {
  }
  jdbc {
  jdbc_connection_string => "jdbc:mysql://localhost:3306/example"
  jdbc_user => "dbuser"
  jdbc_password => "123456"
  jdbc_driver_library => "../test/mysql-connector-java-5.1.47.jar"
  jdbc_driver_class => "com.mysql.jdbc.Driver"
  statement => "SELECT * FROM user"
  type => "test"
  }
}
output {
  elasticsearch {
	hosts => "localhost:9200"
	index => "world"
	document_id => "%{id}"
  }
  stdout {
	codec => json_lines
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 3.3 运行测试conf

logstash -f ../test/test.conf
1

你将会看到JDBC的查询结果信息 JDBC的查询结果信息


diboot 简单高效的轻代码开发框架 (opens new window)