2017-05-17 28 views
0

我正在使用this插件作爲我的logstash日誌的輸出。使用UPSERT功能的jdbc輸出插件logstash

我需要使用upsert函數來檢查一行是否存在然後更新,如果它不存在,那麼只需添加。

我使用PostgreSQL作爲分貝,它支持使用UPSERT,很好描述here。作爲輸入,日誌來自elasticsearch。

我的配置問題是我正確地在我的表中添加新行,但無法更新現有的行。

這裏是我的配置:

jdbc { 
     driver_jar_path => '/home/vittorio/Downloads/postgresql-42.1.1.jre6.jar' 
     connection_test => false 
     connection_string => 'jdbc:postgresql://127.0.0.1:5432/postgres' 
statement => [" 

     INSERT INTO userstate VALUES(?,?,?,?,?) on conflict (username) 
     do update set (business_name, iban, status, timestamp) = ('%{[resource][response_attributes][business_name]}','%{[resource][response_attributes][iban]}','%{[resource][response_attributes][status]}','%{@timestamp}') 
     where userstate.username = '%{[request][username]}';", "%{[request][username]}","%{[resource][response_attributes][business_name]}","%{[resource][response_attributes][iban]}","%{[resource][response_attributes][status]}","%{@timestamp}" 
     ] 
username => "myuser" 
password => "mypass" 
} 

我做錯什麼了嗎? 感謝

回答

0

我manged使其通過自己的工作,這是我到目前爲止已經完成:

jdbc { 
      driver_jar_path => '/home/vittorio/Downloads/postgresql-42.1.1.jre6.jar' 
      connection_test => false 
      connection_string => 'jdbc:postgresql://127.0.0.1:5432/postgres' 
      statement => [" 

      INSERT INTO userstate VALUES(?,?,?,?,?) 
      on conflict (username) 
      do update set (business_name, iban, status, timestamp) = (?,?,?,?) 
      where userstate.username = ?" 
      , "%{[request][username]}","%{[resource][response_attributes][business_name]}","%{[resource][response_attributes][iban]}","%{[resource][response_attributes][status]}","%{@timestamp}","%{[resource][response_attributes][business_name]}","%{[resource][response_attributes][iban]}","%{[resource][response_attributes][status]}","%{@timestamp}","%{[request][username]}" 

      ] 
      username => "myusername" 
      password => "mypass" 

     } 

基本上,我已經改變了使用?代替%{[request][username]}where語句,然後每個映射?與來自日誌的相應值。我知道,這是昏迷後的很長時間,但這是我發現的唯一方法。如果有人知道更好的方法,請讓我知道。

謝謝