Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Remark: This is currently in redesign.

The KeyValue feature allows to read, process and write data as key-value pairs, which don't have an fixed schema like tuples.

To use the feature you have to install it and use the "KeyValueObject" or "NestedKeyValueObject" data handler. The applicable operators will be automatically chosen. Until now selection and projection are supported. Also there are operators to transform key-value objects to tuples and the other way round. The feature also includes wrapper for handling of JSON and BSON data.

Table of Contents

Operators

The following new or modificated operators are provided in keyvalue feature.

KeyValueToTuple (Deprecated, use ToTuple operator instead.)

Transforms key-value object to tuples. All data fields which are not defined in schema will be lost. Use the SCHEMA attribute to define:

  • the path of the input that should be part of the tuple. This could be a dot based path: e.g. root.id.value. This is only possible if the result is not a KeyValueObject! You can also use JSONPATH.
  • optional the name that should be use in the schema of the tuple (remark special charactes from Jsonpath like "$", "*", etc. are replaced automatically
  • the datatype of the element that is retrieved from the key value object. This could also be a key value object or List

Examples for schema could be:

Code Block
          ['$','root','KeyValueObject'],
          ['monday', 'List(Integer)'],
          ['$..monday.length()','MondaySize','mondayCount','Integer'],
          ['$.uuid', 'String'],
          ['$.monday[0]',MondayFirst, 'Integer'],


WIth the type field you can set the type of the tuple.

Code Block
languagepql
#DEFINE SCHEMA [['timestamp.unixtimestamp', 'List(Integer)'],['timestamp.iso', 'String'],['track.name', 'String']]
tuple = KEYVALUETOTUPLE({schema=${SCHEMA}, KEEPINPUT = 'false', TYPE = 'type'}, receiverJSON)

TupleToKeyValue

Transforms tuples to key-value objects based on the tuples schema.

Code Block
languagepql
tupleToKeyValue = TUPLETOKEYVALUE({type='KEYVALUEOBJECT'}, receiverTuple)

Select

The select operator can be used for key value objects in the same way as for tuples.

Code Block
languagepql
selectJSON = SELECT({predicate='track.artist.name = "Evanescence"'}, receiverJSON)
selectJSONList = SELECT({predicate='timestamp.unixtimestamp[0] = 1162304033'}, receiverJSON)

Project

For the key value projection the "paths" attribute has to be used. The "attributes" attribute only works for relational tuples.

...

Code Block
languagepql
renamed = RENAME({aliases = ['Zeit', 'ZeitNew', 'Band', 'BandNew'], pairs = 'true'}, mapped)

Map

For the key value MAP operator the "kvexpressions" attribute has to be used. The "expressions" attribute only works for relational tuples.

Code Block
languagepql
output = MAP({
              kvexpressions = [
								['timestamp.unixtimestamp', 'Zeit'],
								['toLong(timestamp.unixtimestamp + 1)', 'Zeit2'],
                                ['track.artist.name', 'Band'],
                                ['track.name', 'Lied']
							]
             }, input)

MEP-Functions

There are some MEP-Functions that can be used in the MAP-Operator:

  • KeyValueObject: asKeyValue(Object kv): Cast an object to a KeyValueObject (could be necessary if Odysseus can not determine the type of an object). KV must be a KeyValueObject, else there will be a ClassCastException
  • KeyValueObject: toKeyValue(String): Creates a new KeyValueObject from a String JSON Expression
  • Object: getElement(KeyValueObject kv, String path): Returns the one element identified by the path expression. 
  • Tuple: getElements(KeyValueObject kv, String path): Returns a tuple of elements identified by the path expression.
  • Object: path(KeyValueObject kv, String path): Returns the one object that is identified by the JSONPATH epxression. Slower than getElement(s) but more expressive.

Wrapper

The feature adds protocol handler for the common key-value data formats JSON and BSON (binary JSON).

...

Code Block
languagepql
 SENDERjson = SENDER({
    transport='TCPServer',
    wrapper='GenericPush',
    protocol='JSON',
    dataHandler='KeyValueObject',
    SINK="SENDERjson",
    options=[
        ['port', '8080'],
        ['HOST','localhost'],
    ]}, json)
receiverJSON = RECEIVE({
    transport='TCPClient',
    source= 'ReceiverJSON',
    protocol='JSON',
    dataHandler='KeyValueObject',
    options=[['port', '8080']]         
})

BSON

BSON can be used in exactly the same way as JSON - just replace the protocol handler.

...