1 year ago

#359102

test-img

Dustin Wilson

Dynamic JSON structure (TM Forum Spec) in a ksqldb stream in Kafka?

Good morning, everyone. I need to create a stream to parse the below structure. Specifically, I need to do some data routing based on the ->event->appointment->category value. As I understand it, if this were a static structure I could use a STRUCT to replicate it. However, I can't be certain that any of the pieces other than the one I want are actually there during any given call. How can I parse this?

{
    "id": "d67c872c-9a66-11ec-b909-0242ac120002",
    "eventId": "6a0d1600-9a67-11ec-b909-0242ac120002",
    "eventTime": "2022-03-02T20:22:44.484Z",
    "eventType": "AppointmentCreate",
    "correlationId": "f08bb3a4-9a66-11ec-b909-0242ac120002",
    "timeOcurred": "2022-03-02T20:22:44.484Z",
    "event": {
        "appointment": {
            "id": "8aaa4b2c-3140-474d-9763-62f5d5c2bcbb",
            "category": "InternetInstall",
            "creationDate": "2022-02-16T03:42:02.156Z",
            "description": "regular",
            "externalId": "11138945",
            "attachment": [
                {
                    "attachmentType": "embeddedObject",
                    "content": "{ \"timeZone\": \"Central\",\"orderId\": \"14567897\", \"ticketId\": \"\", \"points\": \"55\" }",
                    "mimeType": "application/json",
                    "name": "appointmentDetails",
                    "@schemaLocation": "https://cdn.redact.cloud/schemas/tmf/v4/Attachment.schema.json",
                    "@type": "Attachment"
                }
            ],
            "contactMedium": [
                {
                    "mediumType": "telephoneNumber",
                    "characteristic": {
                        "phoneNumber": "8125551212",
                        "@schemaLocation": 
                        "@type": "MediumCharacteristic"
                    },
                    "@type": "ContactMedium"
                },
                {
                    "mediumType": "emailAddress",
                    "characteristic": {
                        "emailAddress": "jimthebutcher@gmail.com",
                        "@schemaLocation": 
                        "@type": "MediumCharacteristic"
                    },
                    "@type": "ContactMedium"
                }
            ],
            "note": [
                {
                    "date": "2022-02-16T03:42:02.156Z",
                    "text": "New install needs router and extender. Please call before going to site 8153433657\n",
                    "@type": "Note"
                },
                {
                    "date": "2022-02-16T03:42:02.156Z",
                    "text": "EQ-DB\nEQ-NDI\nBXT\nD1 100 FRAN FIBER TO HALLWAY INSTALLED ONT LL 17.11 AND WHW",
                    "@type": "Note"
                }
            ],
            "relatedEntity": [
                {
                    "id": "40d35ce9-68e4-420b-91c2-054e4641be16",
                    "name": "OFSC",
                    "role": "schedulingPlatform",
                    "@type": "RelatedEntity",
                    "@referredType": "EntitySpecification"
                }
            ],
            "relatedParty": [
                {
                    "id": "1792493",
                    "name": "Smith, James",
                    "role": "customer",
                    "@type": "RelatedParty",
                    "@referredType": "OFSCIndividual"
                },
                {
                    "id": "8475",
                    "name": "adcberwin",
                    "role": "technician",
                    "@type": "RelatedParty",
                    "@referredType": "OFSCTechnician"
                }
            ],
            "relatedPlace": {
                "city": "Evansville",
                "postcode": "47715",
                "stateOrProvince": "IN",
                "locality": "BETTENDORF",
                "streetName": "123 Main St",
                "geographicLocation": {
                    "type": "Point",
                    "coordinates": [
                        -90.48015,
                        41.5607
                    ]
                },
                "role": "serviceAddress",
                "@type": "GeographicAddress"
            },
            "status": "initialized",
            "validFor": {
                "endDateTime": "2022-02-16T16:30:00.000Z",
                "startDateTime": "2022-02-16T15:00:00.000Z"
            },
            "@type": "Appointment"
        }
    }
}

Thanks so much in advance for your help.

I have tried using MAP calls, but it seems that the structure is too complex (and I'm probably doing it wrong). I can see the data in the topic, but when I try to create a stream with map the JSON just shows as null.

ksql> create stream "r_events" (                                                                                                                                                             
>"key" VARCHAR KEY,                                                                                                                                                                          
>"value" MAP <VARCHAR, VARCHAR>                                                                                                                                                              
>) with (kafka_topic = 'appointment-topic', VALUE_FORMAT = 'JSON');

ksql> select * from "r_events" emit changes;
+--------------------------------------------------------------------------------------------+------------+
|key                                                                                         |value                                                                                       |
+--------------------------------------------------------------------------------------------+------------+
|{Key:"1"}                                                                                   |null                                                                                        |

I get that same null result no matter how I create the stream. The above TMF data structure is an example of one of the messages in the topic. Any help would be greatly appreciated.

apache-kafka

apache-kafka-streams

ksqldb

0 Answers

Your Answer

Accepted video resources