Ask Your Question
1

How can I convert an existing record's root field to MAP?

asked 2018-02-13 15:48:31 -0500

jeff gravatar image

I have a record whose root field is of some scalar type (say, for example STRING or BYTE_ARRAY). How can I convert the root type to a MAP so that subfields can be added, while preserving the previous root field value (ex: as /original)?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2019-08-15 12:11:19 -0500

metadaddy gravatar image

This may be an instance where a script is clearer than off-the-shelf stages. Here it is in Jython:

for record in records:
  try:
    # Save existing value, create new map, put saved value in the map
    tmp = record.value
    record.value = sdcFunctions.createMap(True)
    record.value['fieldName'] = tmp

    # Write record to processor output
    output.write(record)

  except Exception as e:
    # Send record to error
    error.write(record, str(e))

This will work whatever the type of the current root element.

edit flag offensive delete link more
2

answered 2018-02-14 08:09:12 -0500

todd gravatar image

I'm curious about this too. How about trying an Expression Evaluator with the emptyMap function to the / output field as shown in this screenshot?

image description

or

${record:value('/')} for / Output Field

edit flag offensive delete link more

Comments

Thanks, your suggestion of setting output field of / with ${emptyMap()} was exactly what I needed. The key in my case was to also capture the original root in an attribute so that I can then restore it afterward (after the root is set to map). That was done in another expression processor before.

jeff gravatar imagejeff ( 2018-02-14 22:05:09 -0500 )edit
2

answered 2019-08-15 11:07:10 -0500

jeff gravatar image

To close the loop, this becomes a multi step process.

  1. Create an Expression Evaluator processor (or use an existing one) to set the current root field value to some header attribute. Because those are associated with the record, they will survive the root field being changed. Ex: header attribute expression for previousRoot = ${record:value('/')} Note that if the root field is something other than a string, you will need to handle serialization to a string somehow, since record attribute values are only strings.
  2. After that, create an Expression Evaluator processor to set the root field (/) to ${emptyMap()}, which will clear out the existing root and replace it with a new MAP field.
  3. After that, create an Expression Evaluator to set the record attribute value you stored in step 1 to a new field under the new MAP root. Ex: /rootField = ${record:attribute('previousRoot')} Again, note that if the previous root field type was something other than STRING, you will need to take care of deserializing from STRING here.
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-02-13 15:48:31 -0500

Seen: 745 times

Last updated: Aug 15