org.apache.nifi.processors.csv.CsvToJson.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.nifi.processors.csv.CsvToJson.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.nifi.processors.csv;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.IOUtils;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.processor.*;
import org.apache.nifi.annotation.behavior.ReadsAttribute;
import org.apache.nifi.annotation.behavior.ReadsAttributes;
import org.apache.nifi.annotation.behavior.WritesAttribute;
import org.apache.nifi.annotation.behavior.WritesAttributes;
import org.apache.nifi.annotation.lifecycle.OnScheduled;
import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.annotation.documentation.SeeAlso;
import org.apache.nifi.annotation.documentation.Tags;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.io.InputStreamCallback;
import org.apache.nifi.processor.io.OutputStreamCallback;
import org.apache.nifi.processor.util.StandardValidators;
import org.apache.nifi.util.ObjectHolder;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.*;

@Tags({ "example" })
@CapabilityDescription("Provide a description")
@SeeAlso({})
@ReadsAttributes({ @ReadsAttribute(attribute = "", description = "") })
@WritesAttributes({ @WritesAttribute(attribute = "", description = "") })
public class CsvToJson extends AbstractProcessor {

    public static final PropertyDescriptor CSV_HEADER = new PropertyDescriptor.Builder().name("CSV Header")
            .description("The CSV header line.").required(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
            .build();

    public static final Relationship SUCCESS = new Relationship.Builder().name("success")
            .description("Success relationship").build();

    public static final Relationship FAILURE = new Relationship.Builder().name("failure")
            .description("Failure relationship").build();

    private List<PropertyDescriptor> descriptors;
    private Set<Relationship> relationships;

    private String[] headers;
    private JsonFactory jsonFactory = new JsonFactory();

    @Override
    protected void init(final ProcessorInitializationContext context) {
        final List<PropertyDescriptor> descriptors = new ArrayList<PropertyDescriptor>();
        descriptors.add(CSV_HEADER);
        this.descriptors = Collections.unmodifiableList(descriptors);

        final Set<Relationship> relationships = new HashSet<Relationship>();
        relationships.add(SUCCESS);
        relationships.add(FAILURE);
        this.relationships = Collections.unmodifiableSet(relationships);
    }

    @Override
    public Set<Relationship> getRelationships() {
        return this.relationships;
    }

    @Override
    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
        return descriptors;
    }

    @OnScheduled
    public void onScheduled(final ProcessContext context) {
        final String headerValue = context.getProperty(CSV_HEADER).getValue();
        this.headers = headerValue.split("[,]");
    }

    @Override
    public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
        FlowFile flowFile = session.get();
        if (flowFile == null) {
            return;
        }

        final ObjectHolder<String> contentHolder = new ObjectHolder<>(null);

        session.read(flowFile, new InputStreamCallback() {
            @Override
            public void process(InputStream inputStream) throws IOException {
                contentHolder.set(IOUtils.toString(inputStream));
            }
        });

        CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(headers);

        try {
            CSVParser csvParser = new CSVParser(new StringReader(contentHolder.get()), csvFileFormat);

            List<CSVRecord> records = csvParser.getRecords();
            if (records.size() == 0) {
                getLogger().error("No records found");
                session.transfer(flowFile, FAILURE);
            } else if (records.size() > 1) {
                getLogger().error("More than one record found");
                session.transfer(flowFile, FAILURE);
            } else {
                final CSVRecord record = records.get(0);

                flowFile = session.write(flowFile, new OutputStreamCallback() {
                    @Override
                    public void process(OutputStream outputStream) throws IOException {
                        try (JsonGenerator generator = jsonFactory.createJsonGenerator(outputStream)) {
                            generator.writeStartObject();

                            Map<String, String> recordMap = record.toMap();
                            for (Map.Entry<String, String> entry : recordMap.entrySet()) {
                                generator.writeStringField(entry.getKey(), entry.getValue());
                            }

                            generator.writeEndObject();
                        }
                    }
                });

                session.transfer(flowFile, SUCCESS);
            }

        } catch (IOException e) {
            getLogger().error(e.getMessage(), e);
            session.transfer(flowFile, FAILURE);
        }

    }
}