Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Field order is getting changed

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
B C
D
E

Current Output with the below code

Name
A
D
E
B_C

Expected Output

Name
A
B_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())   } }

Field order is getting changed

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
B C C
D
E

Current Output with the below code

Name
A
D
E
B_C
C_C

Expected Output

Name
A
B_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())   } }

Field order is getting changed

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
C E C
D
E

Current Output with the below code

Name
A
D
E
C_C
E_C

Expected Output

Name
A
B_C
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())   } }