Ask Your Question
1

Field order is getting changed

asked 2019-09-25 03:22:20 -0500

anonymous user

Anonymous

updated 2019-09-27 07:19:46 -0500

How do i keep the fields in the same order when reading from the source when i used the below code for my testing it changes the position of the input file what we received.

For example the column list of the names shows like below :

Name
A
E C
D
E

Current Output with the below code

Name
A
D
E
E_C

Expected Output

Name
A
E_C
D
E

Code :

import java.util.regex.* Pattern p = Pattern.compile("[^a-zA-Z0-9]"); for (record in records) {   try {
    def toRemove = []
    def toAdd = [:]
    record.value.each { key, value ->
      if (p.matcher(key).find()) {
        toRemove.add(key)
        toAdd.put(key.replaceAll("[^A-Za-z0-9]", "_"), value)
      }
    }

    toRemove.each { field -> record.value.remove(field) }
    toAdd.each { key, value -> record.value.put(key, value) }

    output.write(record)   } catch (e) {
    // Write a record to the error pipeline
    log.error(e.toString(), e)
    error.write(record, e.toString())   } }
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2019-09-26 11:25:35 -0500

iamontheinet gravatar image

Hi!

Not sure why you're hand-coding it. To replace characters in fields, use Field Renamer or Field Mapper and use Field Order to order/reorder fields.

Hope this helps.

Cheers, Dash

edit flag offensive delete link more

Comments

thanks but i don't want to use three components which can be achieved in a single stage. And if i want to add additional logic i need to add multiple field renamer/mapper i want to avoid it.

Vss@2019 gravatar imageVss@2019 ( 2019-09-26 23:55:13 -0500 )edit

FYI, you don't have to add multiple processors... you can manipulate multiple fields in the same processor.

iamontheinet gravatar imageiamontheinet ( 2019-09-30 12:37:39 -0500 )edit

Thanks for the suggestions

Vss@2019 gravatar imageVss@2019 ( 2019-10-02 13:11:55 -0500 )edit
1

answered 2019-09-26 13:54:13 -0500

metadaddy gravatar image

The short answer is that you're removing the field, then adding it back, so it appears at the end of the map (Groovy's map implementation uses LinkedHashMap under the covers, so the order of insertion is preserved).

Having said that, @iamontheinet's advice stands - use the off-the-shelf processors if you can.

edit flag offensive delete link more

Comments

@meta : Can you please advise how to correct the above code

Vss@2019 gravatar imageVss@2019 ( 2019-09-26 23:55:38 -0500 )edit

Build a new map containing all of the keys as you iterate through them, then just set record.value to the new map.

metadaddy gravatar imagemetadaddy ( 2019-09-27 14:07:06 -0500 )edit

@meta, do we have any sample how to use it.

Vss@2019 gravatar imageVss@2019 ( 2019-10-02 13:11:42 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-09-25 03:22:20 -0500

Seen: 34 times

Last updated: Sep 27