IBMStreams / streamsx.json

Toolkit for working with JSON in SPL applications.
http://ibmstreams.github.io/streamsx.json/
Other
3 stars 19 forks source link

Example of TupleToJSON needed #53

Closed millerbryan closed 4 years ago

millerbryan commented 8 years ago

After reading through the docs a few times I am unable to determine how to tell my TupleToJSON operator what the output format should look like. I created a data type to represent the structure and it compiles OK but I get a runtime exception:

Exception in thread "Thread-11" java.lang.Exception: Attribute "myJsonString" must be one of the following types: [RSTRING, USTRING]

type
    eType = rstring storeID, int64 zoneID, rstring customer ;
    eMQTTType = rstring deviceType, rstring deviceId, rstring eventId, eType d, rstring ts ;

    // Convert child tuple back to JSON
    (stream<eMQTTType myJsonString> TupleToJSON_6_out0) as TupleToJSON_6 = TupleToJSON(Aggregate_4_out0)
        {
            logic
                onTuple Aggregate_4_out0 :
                {
                    println("TupleToJSON input: " +(rstring) Aggregate_4_out0) ;
                }
        }

If I declare the output attribute to be of type rstring, the JSON is created but the hierarchy is flat and the root of the JSON structure is named myJsonString which I do not want.

I was able able figure out JSONToTuple but the docs are really fuzzy going the other way.

ddebrunner commented 8 years ago

Since I need to solve the same issue for the iotf toolkit I'll look at this.

ddebrunner commented 8 years ago

FYI - I'm adding an operator to the streamsx.iotf toolkit that uses TupleToJson to create the JSON payload for a device command:

https://github.com/ddebrunner/streamsx.iotf/blob/splcmd/com.ibm.streamsx.iotf/com.ibm.streamsx.iotf/command.spl

A use of it is here:

https://github.com/ddebrunner/streamsx.iotf/blob/splcmd/samples/CountEvents/com.ibm.streamsx.iotf.sample.countevents/CountEvents.spl

Basically the input schema is:

rstring deviceType, rstring deviceId, rstring cmdId, tuple<rstring eventId, int32 count> countInfo

So that the command payload is the nested tuple countInfo resulting in the JSON payload being

{"eventId":"sensors","count":64}

and the full command received by a device using Quarks as:

{"command":"eventCount",
  "tsms":1458256447624,
  "format":"json",
  "payload":{"eventId":"sensors","count":64}
 }

Next step is to see if it's possible without forcing the use of a nested tuple, in this case supporting: rstring deviceType, rstring deviceId, rstring cmdId, rstring eventId, int32 count

Note eventId is just a value in the command, as this app is basically aggregating event counts and sending them back to the device as device commands

rnostream commented 4 years ago

Close this as it is an old issue and seems to be of kind "How to" and no replies are sent after last comment..