Mapping a message that contains both basic and JSON fields
When the data format for the message key or value is JSON, the DataStax Apache Pulsar™ connector mapping can include individual fields in the JSON structure.
In the following example, the key is text field and the value is JSON. The key is mapped to the name field and each of the JSON fields to a separate column in the table.
key | value |
---|---|
APPLE |
|
EXXON MOBIL |
|
GENERAL MOTORS |
|
AT&T |
|
FORD MOTOR |
|
JSON records in Pulsar can also have a schema associated with them. |
Table requirements
Ensure the following when mapping fields to columns:
-
Data in the Apache Pulsar™ field is compatible with the database table column data type.
-
Pulsar field mapped to a database primary key (PK) column always contains data. Null values are not allowed in PK columns.
-
Set up the supported database table.
-
Create the keyspace. Ensure that keyspace is replicated to a datacenter that is set in the DataStax Apache Pulsar Connector contactPoints parameter. For example, create the
stocks_keyspace
:cqlsh -e "CREATE KEYSPACE stocks_keyspace \ WITH replication = {'class': 'NetworkTopologyStrategy',\ 'Cassandra': 1};"
The datacenter name is case sensitive. Use nodetool ring to get a list of datacenters. -
Create the table. For example, create the
stocks_table
:cqlsh -e "CREATE TABLE stocks_keyspace.stocks_table ( \ symbol text, \ ts timestamp, \ exchange text, \ industry text, \ name text, \ value double, \ PRIMARY KEY (symbol, ts));"
-
Verify that all nodes have the same schema version using nodetool describering. Replace keyspace_name:
nodetool describering -- keyspace\_name
-
In the DataStax Connector configuration file:
-
Add the topic name to
topics
. -
Define the field-to-column mapping.
Example configurations for
stocks_topic
tostocks_table
using the minimum required settings:tasks.max: 1 topics: stocks_topic topic stocks_topic: stocks_keyspace: stocks_table: mapping: 'symbol=value.symbol, ts=value.ts, exchange=value.exchange, industry=value.industry, name=key, value=value.value'
-
-