All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.pulsar.io.hdfs3.sink.seq.HdfsAbstractSequenceFileSink Maven / Gradle / Ivy

There is a newer version: 4.0.0-SNAPSHOT.ursa
Show newest version
/*
 * 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.pulsar.io.hdfs3.sink.seq;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.SequenceFile.Writer;
import org.apache.hadoop.io.SequenceFile.Writer.Option;
import org.apache.pulsar.functions.api.Record;
import org.apache.pulsar.io.core.KeyValue;
import org.apache.pulsar.io.core.Sink;
import org.apache.pulsar.io.hdfs3.sink.HdfsAbstractSink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * HDFS Sink that writes it contents to HDFS as Sequence Files.
 *
 * @param  - The incoming Key type
 * @param  - The incoming Value type
 * @param  - The HDFS Key type
 * @param  - The HDFS Value type
 */
public abstract class HdfsAbstractSequenceFileSink
    extends HdfsAbstractSink implements Sink {

    private static final Logger LOG = LoggerFactory.getLogger(HdfsAbstractSequenceFileSink.class);

    protected AtomicLong counter;
    protected FSDataOutputStream hdfsStream;
    protected Writer writer = null;

    public abstract KeyValue convert(KeyValue kv);

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

    @Override
    protected void createWriter() throws IOException {
       writer = getWriter();
    }

    @Override
    public void write(Record record) {
       try {
            KeyValue kv = extractKeyValue(record);
            KeyValue keyValue = convert(kv);
            writer.append(keyValue.getKey(), keyValue.getValue());
            unackedRecords.put(record);
        } catch (IOException | InterruptedException e) {
            LOG.error("Unable to write to file " + getPath(), e);
            record.fail();
        }
    }

    protected Writer getWriter() throws IOException {
        counter = new AtomicLong(0);
        List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy