16

我正在使用jdbc-river目前從Sql Server數據庫/表讀取數據。截至目前,我已經爲我的數據庫中的每個表創建了一個單獨的類型。作爲我的實現中的下一步,我想使用父/子類型,以便我可以翻譯我的SQL表之間的關係並存儲它們。Elasticsearch:使用jdbc-rivers設置父/子

Table1 
Col_id| name| prop1|prop2|prop3 

child_table1 
col_id| table_id| child_prop1|child_prop2|child_prop3 


curl -XPUT 'localhost:9200/_river/parent/_meta' -d '{ 
    "type" : "jdbc", 
    "jdbc" : { 
     "driver" : "com.mysql.jdbc.Driver", 
     "url" : "jdbc:mysql://localhost:3306/test", 
     "user" : "", 
     "password" : "", 
     "sql" : "select * from table1", 
     "index" : "index1", 
     "type" : "parent" 
    } 
}' 

curl -XPUT 'localhost:9200/_river/child/_meta' -d '{ 
    "type" : "jdbc", 
    "jdbc" : { 
     "driver" : "com.mysql.jdbc.Driver", 
     "url" : "jdbc:mysql://localhost:3306/test", 
     "user" : "", 
     "password" : "", 
     "sql" : "select * from child_table1", 
     "index" : "index1", 
     "type" : "child" 
    } 
}' 



curl -XPOST 'localhost:9200/_river/child/_mapping' -d '{ 
    "child":{ 
    "_parent": {"type": "parent"} 
    } 
}' 

我想存儲的格式如下

{ 
    "id": "1", 
    "name": "A leading wordsmith", 
    "prop1": "data", 
    "prop2": "data", 
    "prop3": "data", 

    "child": [ 
    { 
     "child_prop1": "data", 
     "child_prop2": "data", 
     "child_prop3": "data", 
    } 
    { 
     "child_prop1": "data1", 
     "child_prop2": "data1", 
     "child_prop3": "data1", 
    } 
    ] 
} 

我的數據我如何可以使用JDBC-河流到我的數據存儲爲父/子類型,上面的場景任何註釋可以。

UPDATE 基於反饋以下是更新後的映射&元。

curl -XPOST 'http://localhost:9200/library' -d '{ 
    "settings": { 
    "number_of_shards": 1, 
    "number_of_replicas": 0 
    }, 
    "mappings": { 
    "person": { 
     "properties": { 
     "person_id": { 
      "type": "integer" 
     }, 
     "name": { 
      "type": "string" 
     } 
     } 
    }, 
    "work": { 
     "_parent": { 
     "type": "person" 
     }, 
     "properties": { 
     "person_id": { 
      "type": "integer", 
      "index": "not_analyzed" 
     }, 
     "name": { 
      "type": "string" 
     }, 
     "genre": { 
      "type": "string" 
     }, 
     "publisher": { 
      "type": "string" 
     } 
     } 
    } 
    } 
}' 

curl -XPUT localhost:9200/_river/person/_meta -d '{ 
    "type": "jdbc", 
    "jdbc": { 
    "driver": "com.microsoft.sqlserver.jdbc.SQLServerDriver", 
    "url": "jdbc:sqlserver://127.0.0.1:1433;databaseName=blogcontext", 
    "user": "sa", 
    "password": "password", 
    "sql": "select person_id as _id, name from person", 
    "poll": "30s" 
    }, 
    "index": { 
    "index": "library", 
    "type": "person", 
    "bulk_size": 500, 
    "autocommit": true 
    } 
}' 

curl -XPUT localhost:9200/_river/work/_meta -d '{ 
    "type": "jdbc", 
    "jdbc": { 
    "driver": "com.microsoft.sqlserver.jdbc.SQLServerDriver", 
    "url": "jdbc:sqlserver://127.0.0.1:1433;databaseName=blogcontext", 
    "user": "sa", 
    "password": "password", 
    "sql": "select person_id as _parent,name,genre,publisher from work", 
    "poll": "30s" 
    }, 
    "index": { 
    "index": "library", 
    "type": "work", 
    "bulk_size": 500, 
    "autocommit": true 
    } 
}' 

日誌文件

[2014-01-14 07:10:35,488][ERROR][OneShotRiverMouth  ] bulk [1] error 
    org.elasticsearch.ElasticSearchIllegalArgumentException: Can't specify parent if no parent field has been configured 
     at org.elasticsearch.action.index.IndexRequest.process(IndexRequest.java:597) 
     at org.elasticsearch.action.bulk.TransportBulkAction.executeBulk(TransportBulkAction.java:165) 
     at org.elasticsearch.action.bulk.TransportBulkAction.doExecute(TransportBulkAction.java:140) 
     at org.elasticsearch.action.bulk.TransportBulkAction.doExecute(TransportBulkAction.java:63) 
     at org.elasticsearch.action.support.TransportAction.execute(TransportAction.java:63) 
     at org.elasticsearch.client.node.NodeClient.execute(NodeClient.java:92) 
     at org.elasticsearch.client.support.AbstractClient.bulk(AbstractClient.java:149) 
     at org.elasticsearch.action.bulk.BulkProcessor.execute(BulkProcessor.java:283) 
     at org.elasticsearch.action.bulk.BulkProcessor.access$400(BulkProcessor.java:46) 
     at org.elasticsearch.action.bulk.BulkProcessor$Flush.run(BulkProcessor.java:336) 
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 
     at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351) 
     at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
     at java.lang.Thread.run(Thread.java:724) 

感謝

+0

這對我也很有用。 – Jay

回答

9

假設你的表是這樣的:

table1 
table_id| name| prop1|prop2|prop3 

child_table1 
child_id| table_id| child_prop1|child_prop2|child_prop3 

您需要選擇主行ID並把它命名爲「_id 「,您的父母身份並將其命名爲」_parent「

curl -XPUT 'localhost:9200/_river/parent/_meta' -d '{ 
    "type" : "jdbc", 
    "jdbc" : { 
     "driver" : "com.mysql.jdbc.Driver", 
     "url" : "jdbc:mysql://localhost:3306/test", 
     "user" : "", 
     "password" : "", 
     "sql" : "select table_id as _id, name, prop1, prop2, prop3 from table1", 
     "index" : "index1", 
     "type" : "parent" 
    } 
}' 

curl -XPUT 'localhost:9200/_river/child/_meta' -d '{ 
    "type" : "jdbc", 
    "jdbc" : { 
     "driver" : "com.mysql.jdbc.Driver", 
     "url" : "jdbc:mysql://localhost:3306/test", 
     "user" : "", 
     "password" : "", 
     "sql" : "select child_id as _id, table_id as _parent, child_prop1, child_prop2, child_prop3 from child_table1", 
     "index" : "index1", 
     "type" : "child" 
    } 
}' 

並像你一樣定義映射父/子,然後完成。您可以使用父/子查詢來立即查詢父/子數據。

UPDATE: 我已經使用您的最新映射並創建一個示例數據庫來導入數據。一切工作正常,我可以索引父/孩沒有任何錯誤。 我使用ES 0.9.5,jdbc-river 2.2.2。

+1

我曾試過,但得到以下錯誤[2014-01-10 07:52:18,112] [錯誤] [SimpleRiverMouth]批量[5037]錯誤 org.elasticsearch.ElasticSearchIllegalArgumentException:如果沒有父字段有已配置 – jsp

+0

我認爲你的父母/孩子映射中缺少一些東西。你能發表你的貼圖嗎? –

+0

https://gist.github.com/jpoth/8406466 – jsp

相關問題