org.apache.metron.writer.WriterToBulkWriter.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.metron.writer.WriterToBulkWriter.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.metron.writer;

import org.apache.storm.tuple.Tuple;
import com.google.common.collect.Iterables;
import org.apache.metron.common.configuration.writer.SingleBatchConfigurationFacade;
import org.apache.metron.common.configuration.writer.WriterConfiguration;
import org.apache.metron.common.writer.BulkMessageWriter;
import org.apache.metron.common.writer.MessageWriter;
import org.apache.metron.common.writer.BulkWriterResponse;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class WriterToBulkWriter<MESSAGE_T> implements BulkMessageWriter<MESSAGE_T>, Serializable {
    MessageWriter<MESSAGE_T> messageWriter;

    public static transient Function<WriterConfiguration, WriterConfiguration> TRANSFORMATION = config -> new SingleBatchConfigurationFacade(
            config);

    public WriterToBulkWriter(MessageWriter<MESSAGE_T> messageWriter) {
        this.messageWriter = messageWriter;
    }

    @Override
    public void init(Map stormConf, WriterConfiguration config) throws Exception {
        messageWriter.init();
    }

    @Override
    public BulkWriterResponse write(String sensorType, WriterConfiguration configurations, Iterable<Tuple> tuples,
            List<MESSAGE_T> messages) throws Exception {
        BulkWriterResponse response = new BulkWriterResponse();
        if (messages.size() > 1) {
            response.addAllErrors(new IllegalStateException("WriterToBulkWriter expects a batch of exactly 1"),
                    tuples);
            return response;
        }

        try {
            messageWriter.write(sensorType, configurations, Iterables.getFirst(tuples, null),
                    Iterables.getFirst(messages, null));
        } catch (Exception e) {
            response.addAllErrors(e, tuples);
            return response;
        }

        response.addAllSuccesses(tuples);
        return response;
    }

    @Override
    public String getName() {
        return messageWriter.getName();
    }

    @Override
    public void close() throws Exception {
        messageWriter.close();
    }
}