Ask Your Question
1

Jython record iteration cause stack overflow error

asked 2019-03-01 10:42:47 -0500

daveh gravatar image

updated 2019-03-05 10:27:03 -0500

metadaddy gravatar image

Using the Dev Raw Data origin to create the following record:

{
  "f1": "abc",
  "f2": "xyz",
  "f3": "lmn"
}

Why does to following jython script work:

for record in records: 
    record.value['child'] = {}
    for k, v in record.value.items():   
      x = v  # let's see if v is the problem
      record.value['child'][k] = 2
    output.write(record)

>      Record1-Output Record1 :
>     MAP
>     f1 :
>     STRING abc
>     f2 :
>     STRING xyz
>     f3 :
>     STRING lmn
>      child :
>     MAP
>     f1 :
>     INTEGER 2
>     f2 :
>     INTEGER 2
>     f3 :
>     INTEGER 2
>     child :
>     INTEGER 2

But the following script (note assigment change for the 'child' key from constant to an attempt to get the value from one of the records other keys):

for record in records: 
    record.value['child'] = {}
    for k, v in record.value.items():   
      x = v  # let's see if v is the problem
      record.value['child'][k] = v
    output.write(record)

CAUSES A Closejava.lang.StackOverflowError ????

Thanks.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-03-01 15:09:42 -0500

iamontheinet gravatar image

updated 2019-03-05 13:27:11 -0500

Hi!

The exception is caused by the nested recursion that occurs by adding the 'child' dictionary to the record and then manipulating its value while iterating over the record key-value pairs. (Hard-coded value of 2 works because it's a single element/item and not a dictionary.)

Replace your code with the following and it will work:

for record in records:
  try:
    key_value_pairs = {}
    for (k,v) in record.value.items():
      key_value_pairs[k] = v
      #optionally delete root entry
      #del record.value[k]

    record.value['child'] = key_value_pairs

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

image description

Cheers, Dash

edit flag offensive delete link more

Comments

Perfect, thanks for the tip. Works great now. Much obliged.

daveh gravatar imagedaveh ( 2019-03-05 10:25:38 -0500 )edit

You're welcome!

iamontheinet gravatar imageiamontheinet ( 2019-03-05 10:30:56 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-03-01 10:42:47 -0500

Seen: 29 times

Last updated: Mar 05