Nodejs和postgresql中的Elasticsearch jdbc-importer

时间:2017-05-21 10:35:03

标签: node.js postgresql elasticsearch jdbc

我想在我的项目中使用Elasticsearch。我正在使用Nodejs和postgresql。

我想将postgresql与elasticsearch连接,因为我正在使用jdbc-importer。我按照他们的文档中的步骤连接postgresql,但是我通过命令行成功了。

我想通过nodejs

在我的项目中使用jdbc-importer

运行jdbc importer的commondline代码:

bin=/Users/mac/Documents/elasticsearch-jdbc-2.3.4.1/bin
lib=/Users/mac/Documents/elasticsearch-jdbc-2.3.4.1/lib
echo '{
     "type" : "jdbc",
     "jdbc" : {
         "url" : 
        "jdbc:postgresql://localhost:5432/development",
        "sql" : "select * from \"Products\"",
         "index" : "product",
         "type" : "product",
    "elasticsearch" : {
    "cluster" : "elasticsearch",
    "host" : "localhost",
    "port" : 9300
}
     }
 }' | java \
        -cp "${lib}/*" \
        org.xbib.tools.Runner \
        org.xbib.tools.JDBCImporter

上面的命令在elasticsearch中创建了索引产品,它也有来自postgresql的Products表的数据。

现在,我想通过nodejs使用那个jdbc导入器,如果有人知道在elasticsearch中管理我的postgresql数据的其他有效方法,也欢迎他们回答。

1 个答案:

答案 0 :(得分:0)

您可以使用Logstash: https://www.elastic.co/blog/logstash-jdbc-input-plugin

有了它,您可以将数据从Postgres传输到ElasticSearch。这是我的配置文件:

input { 
  jdbc {
    # Postgres jdbc connection string to our database, mydb
    jdbc_connection_string => "jdbc:postgresql://localhost:5432/MyDB"
    # The user we wish to execute our statement as
    jdbc_user => "postgres"
    jdbc_password => ""
    # The path to our downloaded jdbc driver
    jdbc_driver_library => "postgresql-42.1.1.jar"
    # The name of the driver class for Postgresql
    jdbc_driver_class => "org.postgresql.Driver"
    # our query
    statement => "SELECT * from contacts"
  }
}
output {
  elasticsearch {
    hosts => ["localhost:9200"]
    index => "contacts"
    document_type => "contact"
    document_id => "%{uid}"
  }
}