使用Logstash批量创建和批量更新ElasticSearch索引文档

时间:2019-11-01 19:05:57

标签: elasticsearch logstash

文档没有更新,而是新创建的,而不是更新文档。

COMPANY_ID是唯一列。

样本数据

COMPANY_NAME,LOGO_EXT,COMPANY_ID

ABC LIMITED,JPG,ABC000001

XYZ LIMITED,PNG,ABC000002

AAA LLC,ABC000003

我能够创建索引和文档。

问题是,当我更新索引时,将创建文档而不是更新文档。 例如。 之前 ABC LIMITED,JPG,ABC000001

之后 ABCD LIMITED,JPG,ABC000001

因此,只应更新COMPANY_NAME。

1。使用以下代码成功创建索引:-

BAT文件

cd C:\ logstash-7.3.1 \ bin logstash -f C:\ logstash.conf

C:\ logstash.conf文件

input {  
    jdbc {  
        jdbc_driver_library => "C:\sqljdbc_7.4\enu\mssql-jdbc-7.4.1.jre8.jar"  
        jdbc_driver_class => "com.microsoft.sqlserver.jdbc.SQLServerDriver"  
        jdbc_connection_string => "jdbc:sqlserver://;user=;password=;"  
        jdbc_user => ""  
        jdbc_password => ""  
        statement => "SELECT COMPANY_NAME,LOGO_EXT,COMPANY_ID from dbo.CompanyMaster WITH(NOLOCK) ORDER BY COMPANY_NAME"  
    }  
}  
filter {}  
output {  
    stdout {  
        codec => json_lines  
    }  
    elasticsearch {  
        hosts => "http://localhost:9200"  
        index => "companylistindex"  
        document_id => "%{COMPANY_ID}"
        action => index
    }  
}  

2。更新代码

input {  
    jdbc {  
        jdbc_driver_library => "C:\sqljdbc_7.4\enu\mssql-jdbc-7.4.1.jre8.jar"  
        jdbc_driver_class => "com.microsoft.sqlserver.jdbc.SQLServerDriver"  
        jdbc_connection_string => "jdbc:sqlserver://;user=;password=;"  
        jdbc_user => ""  
        jdbc_password => ""  
        statement => "SELECT COMPANY_NAME,LOGO_EXT,COMPANY_ID from dbo.CompanyMaster WITH(NOLOCK) WHERE ModifiedOn>'2019-11-01'"  
    }  
}  
filter {}  
output {  
    stdout {  
        codec => json_lines  
    }  
    elasticsearch {  
        hosts => "http://localhost:9200"  
        index => "companylistindex"  
        document_id => "%{COMPANY_ID}"
    }  
}

请帮助我更新文档。

注意:如果不同,则仅需要更新COMPANY_NAME或LOGO_EXT。 COMPANY_ID是唯一列。

1 个答案:

答案 0 :(得分:0)

除了@Polynomial Proton建议的注释之外,您不再需要2输出节。只需像下面的1部分那样:

output {  
    stdout {  
        codec => json_lines  
    }  
    elasticsearch {  
        hosts => "http://localhost:9200"  
        index => "companylistindex"  
        document_id => "%{COMPANY_ID}"
        action => "update"
        doc_as_upsert => "true"
    }  
}

这将同时处理索引和更新。