0

我正在使用 logstash 创建从 elasticsearch 到 crate.io 的管道。下面是配置。

input{
 elasticsearch {
   hosts => "<host_name>:9200"
   index => "index1"
   query => '{ "size":10,"query": {"match_all": {} } }'
   }
}
filter{
    if!([bench_pose][M_Body_t]) {
            mutate{
                    add_field => {"[bench_pose][M_Body_t]" => null}
            }
    }
    if!([bench_pose][M_Jaw_t]) {
            mutate{
                    add_field => {"[bench_pose][M_Jaw_t]" => null}
            }
    }
}
output{
 jdbc {
       driver_class => "io.crate.client.jdbc.CrateDriver"
       driver_auto_commit => false
       driver_jar_path => "/etc/crate/crate-jdbc-standalone-1.12.3.jar"
       connection_string => "crate://<host_ip>:4300"
       statement => ["INSERT INTO table_name(path,bench_pose_m_Body_t,bench_pose_m_jaw_t) VALUES(?,?,?)",'path','%{[bench_pose][M_Body_t]}','%{[bench_pose][M_Jaw_t]}']
   }
}

源有一个字段“bench_pose”,它是 OBJECT 数据类型,而 bench_pose 下的字段 M_Body_t 和 M_Jaw_t 是 DOUBLE 类型。这些字段在源和目标中都可以为空。此字段在源中的少数文档中具有空值。即使目标表具有 NULLable 字段,当 insert 语句遇到具有 null 值的文档时,也会引发以下错误。

JDBC - Exception. Not retrying. {:exception=>java.sql.SQLException: Validation failed for bench_pose_m_body_t: 'null' cannot be cast to type double  

如果我删除过滤器插件,它会引发如下错误。

JDBC - Exception. Not retrying. {:exception=>java.sql.SQLException: Validation failed for bench_pose_m_body_t: '%{[bench_pose][M_Body_t]}' cannot be cast to type double

如果我在插入中硬编码空值并在 crate sql 上运行查询,它会正确执行。如何在 logstash 中解析这些字段中的空值?

4

1 回答 1

2

您需要将 INSERT 语句更改为:

   statement => ["INSERT INTO table_name(path,benc‌​h_pose_m_Body_t,bench‌​_pose_m_jaw_t) VALUES(?,TRY_CAST(? as double),TRY_CAST(? as double))",'path','%{[bench_pose][M_Body_t]}','%{[bench_pose][M_Jaw_t]}']

TRY_CAST如果类型转换不兼容,将返回 null 而不是抛出错误。

于 2016-08-17T03:18:10.157 回答