【问题标题】:how to connect elasticsearch with postgres using logstash?如何使用logstash将elasticsearch与postgres连接起来?
【发布时间】:2019-10-09 08:19:34
【问题描述】:

我在使用 logstash 将数据从 postgres 传输到 elastic 时遇到问题。我收到错误“converge_state 中的阻塞”。产生的错误是:

Failed to execute action {:action=>LogStash::PipelineAction::Create/pipeline_id:main, :exception=>"LogStash::ConfigurationError", :message=>"Expected one of \r, \n at line 36, column 4 (byte 583) after # }", :backtrace=>["/usr/local/Cellar/logstash/7.3.2/libexec/logstash-core/lib/logstash/compiler.rb:41:in `compile_imperative'", "/usr/local/Cellar/logstash/7.3.2/libexec/logstash-core/lib/logstash/compiler.rb:49:in `compile_graph'", "/usr/local/Cellar/logstash/7.3.2/libexec/logstash-core/lib/logstash/compiler.rb:11:in `block in compile_sources'", "org/jruby/RubyArray.java:2577:in `map'", "/usr/local/Cellar/logstash/7.3.2/libexec/logstash-core/lib/logstash/compiler.rb:10:in `compile_sources'", "org/logstash/execution/AbstractPipelineExt.java:151:in `initialize'", "org/logstash/execution/JavaBasePipelineExt.java:47:in `initialize'", "/usr/local/Cellar/logstash/7.3.2/libexec/logstash-core/lib/logstash/java_pipeline.rb:24:in `initialize'", "/usr/local/Cellar/logstash/7.3.2/libexec/logstash-core/lib/logstash/pipeline_action/create.rb:36:in `execute'", "/usr/local/Cellar/logstash/7.3.2/libexec/logstash-core/lib/logstash/agent.rb:325:in `block in converge_state'"]}

我的配置文件如下所示:

input {
    jdbc {

        jdbc_connection_string => "jdbc:postgresql://localhost:5432/school"


        jdbc_user => "postgres"
        jdbc_password => "postgres"


        jdbc_driver_library => "/Users/karangupta/Downloads/postgresql-42.2.8.jar"


        jdbc_driver_class => "org.postgresql.Driver"

        statement => "select sch.udise_sch_code,
    sch.school_name,
    e.edu_block_name,
    d.district_name,
    s.state_name,

    sch.school_address,
    sch.pincode,
    case when sch.sch_loc_r_u = 1 then 'rural'
        when sch.sch_loc_r_u = 2 then 'urban' end as "sch_location_type",
    sch.assembly_const,
    sch.status,

    schd.academic_year,
    schd.sch_category,
    schd.class_frm,
    schd.class_to,

    case when schd.sch_type = 1 then 'boys' 
          when schd.sch_type = 2 then 'girls' 
          else 'coed' end as "school_type",

    case when sde.is_cce_ele = 1 then 'yes'
        when sde.is_cce_ele = 2 then 'no' 
        else 'NA' end as "cce_implemented_ele",
    case when sde.is_pcr_maintained = 1 then 'yes'
        when sde.is_pcr_maintained = 2 then 'no' 
        else 'NA' end as "cce_implemented_ele",
    case when sde.is_pcr_shared_parents = 1 then 'yes'
        when sde.is_pcr_shared_parents = 2 then 'no' 
        else 'NA' end as "is_pcr_shared_parents",
    case when sde.is_cce_sec = 1 then 'yes'
        when sde.is_cce_sec = 2 then 'no' 
        else 'NA' end as "cce_implemented_sec",
    case when sde.is_cce_hrsec = 1 then 'yes'
        when sde.is_cce_hrsec = 2 then 'no' 
        else 'NA' end as "cce_implemented_hrsec",
    sde.rte_25p_applied,
    sde.rte_25p_enrolled_previous_yr,
    sde.is_smc,
    sde.smc_tot_m,
    sde.smc_tot_f,
    sde.sms_parents_m,
    sde.sms_parents_f,
    sde.smc_lgb_m
from mst_school_2017_18 sch 
inner join mst_edu_block_2017_18 e on sch.udise_edu_block_code = e.udise_edu_block_code
inner join mst_district_2017_18 d on e.udise_dist_code = d.udise_district_code
inner join mst_state_2017_18 s on d.udise_state_code = s.udise_state_code
inner join school_details_2017_18 schd on sch.udise_sch_code = schd.udise_sch_code
inner join school_details_extended_2017_18 sde on sch.udise_sch_code = sde.udise_sch_code"
    }
}


output {
    stdout { codec => json_lines }
}

# output {
#   elasticsearch {
#       index => "schoolDetails"
#       hosts => "http://localhost:9200"
#   }
# }

我不知道我在这里做错了什么。

【问题讨论】:

    标签: elasticsearch jdbc logstash logstash-jdbc


    【解决方案1】:

    我建议您将多行 SQL 语句存储在一个文件中,然后 reference that file in your configuration

    所以不是

    statement => "select ..."
    

    使用这个:

    statement_filepath => "/absolute/path/to/statement.sql" 
    

    【讨论】:

    • @KaranGupta 以编程方式保持配置文件精简。但是 Val 的回答向您展示了另一种方法,而我实际上向您展示了 如何解决错误... 不过,你们两个都没有冒犯
    • @apt-get_install_skill 没有冒犯,但是对于这么大的查询,将 SQL 查询放在单独的文件而不是单行。我挑战任何人尝试在一行上编写多连接 SQL 查询。
    • @Val 我完全同意,但它并没有以您指出配置有什么问题的方式解决核心问题/错误。不过答案很好。
    • 确实如此,因为换行符是罪魁祸首 ;-) 无论如何,人们现在有两种选择是件好事
    • 当然,尽管最终,每个人​​都想要的是不仅有效而且更易于长期维护的东西。
    【解决方案2】:

    JDBC 输入插件需要 SQL 语句没有任何换行符。因此,用空格替换所有换行符应该没问题。

    我是通过 Notepad++ 为您完成的。语句设置应如下所示:

    statement => "select sch.udise_sch_code, sch.school_name, e.edu_block_name, d.district_name, s.state_name, sch.school_address, sch.pincode, case when sch.sch_loc_r_u = 1 then 'rural' when sch.sch_loc_r_u = 2 then 'urban' end as "sch_location_type", sch.assembly_const, sch.status, schd.academic_year, schd.sch_category, schd.class_frm, schd.class_to, case when schd.sch_type = 1 then 'boys' when schd.sch_type = 2 then 'girls' else 'coed' end as "school_type", case when sde.is_cce_ele = 1 then 'yes' when sde.is_cce_ele = 2 then 'no' else 'NA' end as "cce_implemented_ele", case when sde.is_pcr_maintained = 1 then 'yes' when sde.is_pcr_maintained = 2 then 'no' else 'NA' end as "cce_implemented_ele", case when sde.is_pcr_shared_parents = 1 then 'yes' when sde.is_pcr_shared_parents = 2 then 'no' else 'NA' end as "is_pcr_shared_parents", case when sde.is_cce_sec = 1 then 'yes' when sde.is_cce_sec = 2 then 'no' else 'NA' end as "cce_implemented_sec", case when sde.is_cce_hrsec = 1 then 'yes' when sde.is_cce_hrsec = 2 then 'no' else 'NA' end as "cce_implemented_hrsec", sde.rte_25p_applied, sde.rte_25p_enrolled_previous_yr, sde.is_smc, sde.smc_tot_m, sde.smc_tot_f, sde.sms_parents_m, sde.sms_parents_f, sde.smc_lgb_m from mst_school_2017_18 sch inner join mst_edu_block_2017_18 e on sch.udise_edu_block_code = e.udise_edu_block_code inner join mst_district_2017_18 d on e.udise_dist_code = d.udise_district_code inner join mst_state_2017_18 s on d.udise_state_code = s.udise_state_code inner join school_details_2017_18 schd on sch.udise_sch_code = schd.udise_sch_code inner join school_details_extended_2017_18 sde on sch.udise_sch_code = sde.udise_sch_code"
    

    编辑:

    正如@Val 所说,另一种方法是从文件中加载 sql 语句。

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 2015-10-26
      • 2021-09-15
      • 1970-01-01
      • 2010-11-17
      • 2021-09-03
      • 2017-03-09
      • 2017-09-24
      • 2018-07-21
      相关资源
      最近更新 更多