Search in sources :

Example 1 with FlattenMode

use of com.github.wnameless.json.flattener.FlattenMode in project nifi by apache.

the class FlattenJson method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final String mode = context.getProperty(FLATTEN_MODE).getValue();
    final FlattenMode flattenMode = getFlattenMode(mode);
    String separator = context.getProperty(SEPARATOR).evaluateAttributeExpressions(flowFile).getValue();
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        session.exportTo(flowFile, bos);
        bos.close();
        String raw = new String(bos.toByteArray());
        final String flattened = new JsonFlattener(raw).withFlattenMode(flattenMode).withSeparator(separator.charAt(0)).withStringEscapePolicy(() -> StringEscapeUtils.ESCAPE_JAVA).flatten();
        flowFile = session.write(flowFile, os -> os.write(flattened.getBytes()));
        session.transfer(flowFile, REL_SUCCESS);
    } catch (Exception ex) {
        session.transfer(flowFile, REL_FAILURE);
    }
}
Also used : CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ExpressionLanguageCompiler(org.apache.nifi.expression.ExpressionLanguageCompiler) SideEffectFree(org.apache.nifi.annotation.behavior.SideEffectFree) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) ValidationResult(org.apache.nifi.components.ValidationResult) FlattenMode(com.github.wnameless.json.flattener.FlattenMode) FlowFile(org.apache.nifi.flowfile.FlowFile) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) ProcessSession(org.apache.nifi.processor.ProcessSession) IOException(java.io.IOException) AllowableValue(org.apache.nifi.components.AllowableValue) List(java.util.List) JsonFlattener(com.github.wnameless.json.flattener.JsonFlattener) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) Tags(org.apache.nifi.annotation.documentation.Tags) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) Collections(java.util.Collections) FlowFile(org.apache.nifi.flowfile.FlowFile) JsonFlattener(com.github.wnameless.json.flattener.JsonFlattener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FlattenMode(com.github.wnameless.json.flattener.FlattenMode) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 FlattenMode (com.github.wnameless.json.flattener.FlattenMode)1 JsonFlattener (com.github.wnameless.json.flattener.JsonFlattener)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 StringEscapeUtils (org.apache.commons.text.StringEscapeUtils)1 SideEffectFree (org.apache.nifi.annotation.behavior.SideEffectFree)1 CapabilityDescription (org.apache.nifi.annotation.documentation.CapabilityDescription)1 Tags (org.apache.nifi.annotation.documentation.Tags)1 AllowableValue (org.apache.nifi.components.AllowableValue)1 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)1 ValidationResult (org.apache.nifi.components.ValidationResult)1 ExpressionLanguageCompiler (org.apache.nifi.expression.ExpressionLanguageCompiler)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1