Not able to insert JSON from PostgreSQL to elasticsearch. Getting error - “Exception when executing JDBC query”

半城伤御伤魂 提交于 2021-02-08 10:44:22

问题


I am trying to migrate data from postgresql server to elasticsearch. The postgres data is in JSONB format. When I am starting the river, I am getting the below error.

 [INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600}
 [2019-01-07T14:22:34,625][INFO ][logstash.inputs.jdbc ] (0.128981s) SELECT to_json(details) from inventory.retailer_products1 limit 1
 [2019-01-07T14:22:35,099][WARN ][logstash.inputs.jdbc ] Exception when executing JDBC query {:exception=>#<Sequel::DatabaseError: Java::OrgLogstash::MissingConverterException: Missing Converter handling for full class name=org.postgresql.util.PGobject, simple name=PGobject>}
 [2019-01-07T14:22:36,568][INFO ][logstash.pipeline ] Pipeline has terminated {:pipeline_id=>"main", :thread=>"#<Thread:0x6067806f run>"}

I think the logstash is not able to identify the JSON data type.

Below is my logstash conf file

    input {
        jdbc {
            jdbc_connection_string => "jdbc:postgresql://localhost:5432/mydb"
            jdbc_user => "postgres"
            jdbc_password => "password"
            jdbc_validate_connection => true
            jdbc_driver_library => "/home/dell5/Downloads/postgresql-9.4.1208.jar"
            jdbc_driver_class => "org.postgresql.Driver"
            statement => "SELECT to_json(details) from inventory.retailer_products1 limit 1"
        }
    }

filter{
    json{
        source => "to_json"
    }
}

output {
    elasticsearch {
        index => "products-retailer"
        document_type => "mapping-retailer"
        hosts => "localhost"
        }
        stdout{}
    }

The mapping I have defined for this is as below

{
"products-retailer": {
    "mappings": {
        "mapping-retailer": {
            "dynamic": "false",
                "properties": {
                    "category": {
                        "type": "keyword"
                    },
                    "id": {
                        "type": "keyword"
                    },
                   "products": {
                        "type": "nested",
                        "properties": {
                                "barcode": {
                                    "type": "text"
                                },
                                "batchno": {
                                    "type": "text"
                                },
                                "desc": {
                                    "type": "text"
                                },
                                "expirydate": {
                                    "type": "date",
                                    "format": "YYYY-MM-DD"
                                },
                                "imageurl": {
                                    "type": "text"
                                },
                                "manufaturedate": {
                                    "type": "date",
                                    "format": "YYYY-MM-DD"
                                },
                                "mrp": {
                                    "type": "text"
                                },
                                "name": {
                                    "type": "text",
                                    "fields": {
                                        "ngrams": {
                                            "type": "text",
                                            "analyzer": "autocomplete"
                                        }
                                    }
                                },
                                "openingstock": {
                                    "type": "text"
                                },
                                "price": {
                                    "type": "text"
                                },
                                "purchaseprice": {
                                    "type": "text"
                                },
                                "sku": {
                                     "type": "text"
                                },
                                "unit": {
                                    "type": "text"
                                }
                            }
                        },
                        "retailerid": {
                            "type": "keyword"
                        },
                        "subcategory": {
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    }

The sample data in postgres column is below. It has nested json that I have defined in the mapping of elasticsearch.

{
    "id": "",
    "Category": "Bread and Biscuits",
    "products": {
        "MRP": "45",
        "SKU": "BREAD-1",
        "Desc": "Brown Bread",
        "Name": "Brown Bread",
        "Unit": "Packets",
        "Brand": "Britannia",
        "Price": "40",
        "BarCode": "1234567890",
        "BatchNo": "456789",
        "ImageUrl": "buscuits.jpeg",
        "ExpiryDate": "2019-06-01",
        "OpeningStock": "56789",
        "PurchasePrice": "30",
        "ManufactureDate": "2018-11-01"
    },
    "RetailerId": "1",
    "SubCategory": "Bread"
}

Please suggest what am I missing here and if this is the right way to do it.

I am using Elasticsearch 6.5.1. PostgreSQL 9.5.


回答1:


PGObject does not have capability to convert the json came from to_json method. use internal casting for converting jsonobject to text like this.

SELECT to_json(details)::text from inventory.retailer_products1 limit 1.

Now you can parse the json string in logstash.




回答2:


I have been hitting the same error today and it appears that logstash is unable to convert Postgres json or jsonb data fields (PgObject).

I cast my object fields into TEXT datatype and it stopped yelling at me and started ingesting data. As to whether it is indexing correctly remains to be seen.

I will update my response when i have a better idea if this was the correct approach.



来源:https://stackoverflow.com/questions/54085353/not-able-to-insert-json-from-postgresql-to-elasticsearch-getting-error-excep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!