简体   繁体   English

Kafka Connect-JSON转换器-JDBC Sink连接器-列类型JSON

[英]Kafka Connect - JSON Converter - JDBC Sink Connector - Column Type JSON

use case is to store entire message (which is JSON) and key as a record in table which has two columns 'id' and 'data'. 用例是将整个消息(是JSON)和密钥存储为表中的记录,该表具有两列“ id”和“ data”。

The database is Postgres and it supports column type as JSON. 该数据库是Postgres,它支持列类型为JSON。

As per this article, supported types in JSONConverter are string, int64, etc https://cwiki.apache.org/confluence/display/KAFKA/KIP-301%3A+Schema+Inferencing+for+JsonConverter 根据本文,JSONConverter中支持的类型为字符串,int64等https://cwiki.apache.org/confluence/display/KAFKA/KIP-301%3A+Schema+Inferencing+for+JsonConverter

Is it possible to have type of data field as JSON which then can be stored in Postgres DB with column of type JSON. 是否可以将数据字段的类型设置为JSON,然后将其以JSON类型的列存储在Postgres DB中。

schema = `{
"type":"struct",
"fields":[
    {"type":"string", "optional": false, "field":"id"},
    {"type":"string", "optional": false, "field":"data"}
]}`

Sample data payload is 样本数据有效载荷为

"payload": { "id": 10000, "data": {"hello":"world"} }

Above will store data as text and expects column to be of type text in Postgres. 上面将数据存储为文本,并期望Postgres中的列为text类型。 If the column on Postgres is of type JSON then the JDBC Sink Connector will throw an error. 如果Postgres上的列的类型为JSON,则JDBC Sink Connector将引发错误。

Using JSON types on Postgres will help to create index on JSON fields and so forth. 在Postgres上使用JSON类型将有助于在JSON字段上创建索引等等。 Is it possible to use JSONConverter along with JDBC Sink Converter appropriately for storing records with column type JSON. 是否可以将JSONConverter与JDBC Sink Converter一起适当地用于存储列类型为JSON的记录。

Use value.converter.schema.enable=true , and send JSON data like this (with the schema as part of every message, and update the payload section with the actual message data), and it should work with the JDBC Sink. 使用value.converter.schema.enable=true ,并像这样发送JSON数据(将模式作为每条消息的一部分,并使用实际的消息数据更新payload部分),并且它应与JDBC Sink一起使用。

{
    "schema": {
        "type": "struct",
        "fields": [{
            "type": "int32",
            "optional": false,
            "field": "id"
        }, {
            "type": "struct",
            "name": "data",
            "optional": false,
            "fields": [{
               "type": "string",
               "name": "hello",
               "optional":false
            }]
        }],
        "optional": false,
        "name": "foobar"
    },
    "payload": {
        "id": 10000,
        "data": {"hello":"world"}
    }
}

Or you could look into converting your clients to use Avro, and save yourself some network bandwidth. 或者,您可以考虑将客户转换为使用Avro,从而节省一些网络带宽。

JDBC Sink Connector doesn't support PostgreSQL json, jsonb types. JDBC Sink Connector不支持PostgreSQL json,jsonb类型。 It support number of primitive types, datetimes. 它支持多种基本类型,日期时间。

At following page you can find mapping schema types to Databases types (PostgreSQL) https://docs.confluent.io/5.1.0/connect/kafka-connect-jdbc/sink-connector/index.html 在以下页面上,您可以找到映射架构类型到数据库类型(PostgreSQL)的https://docs.confluent.io/5.1.0/connect/kafka-connect-jdbc/sink-connector/index.html

Although, JDBC Source Connector supports json, jsonb types in some part - columns of such type won't be mapped to STRUCT , but will be mapped to STRING type. 虽然JDBC Source Connector支持json,但在某些方面jsonb类型-这种类型的列不会映射到STRUCT ,而是会映射到STRING类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM