最近在项目上应用到了ElasticSearch和Logstash,在此主要记录了Logstash-input-jdbc同步Oracle数据库到ElasticSearch的主要步骤,本文是对环境进行简单的配置,如需在实际环境中运行还需要进一步调整。
- 首先要配置服务器环境
- 安装Java
- 安装ElasticSearch(我用的版本是2.4.0)
- 安装head插件(用于在浏览器查看状态和数据,非必须)
- 安装Logstash
- 安装logstash-input-jdbc:在ElasticSearch安装目录下运行命令:bin/plugin install logstash-input-jdbc,成功会有提示。
- 安装 elasticsearch-jdbc-2.3.4.1:运行命令:wget http://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/2.3.4.1/elasticsearch-jdbc-2.3.4.1-dist.zip
- 配置同步数据
- 将oracle安装目录下的ojdbc6.jar拷贝到elasticsearch-jdbc-2.3.4.1的lib目录下(此包是同步oracle数据库的驱动程序)
- 配置同步文件:在logstash/bin目录下新建jdbc_oracle.conf文件,配置内容如下:
- input{
stdin{}jdbc{jdbc_connection_string => "jdbc:oracle:thin:@//192.168.1.16:1521/db"jdbc_user => "systemadmin"jdbc_password => "systemadmin"jdbc_driver_library => "/home/oracle/elasticsearch-jdbc-2.3.4.1/lib/ojdbc6.jar"jdbc_driver_class => "Java::oracle.jdbc.driver.OracleDriver"record_last_run => "true"use_column_value => "false"tracking_column => "id"last_run_metadata_path => "/home/oracle/es/info"clean_run => "false"jdbc_paging_enabled => "true"jdbc_page_size => "50000"statement_filepath =>"/home/oracle/es/jdbc_oracle.sql"schedule => "* * * * *"type => "tstype"}}output{elasticsearch{hosts => "localhost:9200"index => "tsuser"document_id => "%{id}"}} - 创建sql脚本对应配置脚本中的statement_filepath:jdbc_oracle.sql,脚本中写sql语句即可,本例中写的是:select * from users
- 在logstash/bin下启动程序: ./logstash -f jdbc_oracle.conf
- 使用head查看数据已经同步成功。