2017-03-28 47 views
0

所以即时通讯设法从logstash中使用JDBC导入程序并导出到elasticsearch时,将头围绕在document_type和document_id之间。Logstash/Elasticsearch JDBC document_id vs document_type?

我终于把我的头包裹在索引上。但让我们假装从传感器数据表(如温度/湿度/等等)中提取传感器ID的......温度/湿度(天气相关数据)与时间记录。 (所以这是一张大桌子)

而且我希望每隔X次频繁轮询数据库。

什么是document_type vs document_id在这种情况下,这将被存储(或任何你想调用它)对1索引。

document_type vs document_id使我感到困惑,特别是关于JDBC导入器。

如果我设置document_id来表示我的主键,每次都不会被覆盖吗?所以我每次只有一个数据文件? (这似乎毫无意义)

回答

1

jdbc plugin将为每列创建一个字段的JSON文档。因此,要保持符合你的例子,如果你有这样的数据将被导入为一个文件,看起来像这样:

{ 
    "sensor_id": 567, 
    "temp": 90, 
    "humidity": 6, 
    "timestamp": "{time}", 
    "@timestamp": "{time}" // auto-created field, the time Logstash received the document 
} 

当你说你是对的,如果你设置document_id你的主键,它会被覆盖。您可以忽略document_id,除非您想更新Elasticsearch中的现有文档,我不认为您想要处理这种类型的数据。让Elasticsearch为您生成文档ID。

现在让我们来谈谈document_type。如果要设置文档类型,则需要将Logstash中的type字段设置为某个值(该值将传播到Elasticsearch中)。因此Elasticsearch中的type field用于对相似的文档进行分组。如果一切都在你的表,你与JDBC插件导入的文件是同一类型的(他们应该!),你可以在这样的jdbc输入设置type ...

input { 
    jdbc { 
    jdbc_driver_library => "mysql-connector-java-5.1.36-bin.jar" 
    jdbc_driver_class => "com.mysql.jdbc.Driver" 
    jdbc_connection_string => "jdbc:mysql://localhost:3306/mydb" 
    jdbc_user => "mysql" 
    parameters => { "favorite_artist" => "Beethoven" } 
    schedule => "* * * * *" 
    statement => "SELECT * from songs where artist = :favorite_artist" 
    ... 
    type => "weather" 
    } 
} 

现在在Elasticsearch中,您可以通过为该类型设置mapping来利用type字段。例如,你可能想要:

PUT my_index 
{ 
    "mappings": { 
    "weather": { 
     "_all":  { "enabled": false }, 
     "properties": { 
     "sensor_id":  { "type": "integer" }, 
     "temp":   { "type": "integer" }, 
     "humidity":  { "type": "integer" }, 
     "timestamp":  { "type": "date" } 
     } 
    } 
    } 
} 

希望这有助于! :)

+0

哇谢谢你,这可能是我见过的最好的解释! – msmith1114

+0

对不起,这可能是一个后期问题:但我是否需要设置映射?我想我不确定他们的确切需要? 如果我不需要设置映射,那么这种类型真的需要吗? – msmith1114