acromusashi.stream.bolt.hdfs.HdfsStreamWriter.java Source code

Java tutorial

Introduction

Here is the source code for acromusashi.stream.bolt.hdfs.HdfsStreamWriter.java

Source

/**
* Copyright (c) Acroquest Technology Co, Ltd. All Rights Reserved.
* Please read the associated COPYRIGHTS file for more details.
*
* THE SOFTWARE IS PROVIDED BY Acroquest Technolog Co., Ltd.,
* WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDER BE LIABLE FOR ANY
* CLAIM, DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
package acromusashi.stream.bolt.hdfs;

import java.io.IOException;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

/**
 * HDFS?????Writer<br>
 * HDFS?1???1?
 * 
 * @author kimura
 */
public class HdfsStreamWriter {
    /** ?????Writer */
    private FSDataOutputStream delegateStream;

    /** 1?????????? */
    private boolean isFileSyncEachTime = false;

    /**
     * ?????
     */
    public HdfsStreamWriter() {
    }

    /**
     * ??HDFS??Open?
     * 
     * @param filePath HDFS
     * @param fs 
     * @param isFileSyncEachTime ?????????
     * @throws IOException Open
     */
    public void open(String filePath, FileSystem fs, boolean isFileSyncEachTime) throws IOException {
        Path dstPath = new Path(filePath);

        if (fs.exists(dstPath) == true) {
            this.delegateStream = fs.append(dstPath);
        } else {
            this.delegateStream = fs.create(dstPath);
        }

        this.isFileSyncEachTime = isFileSyncEachTime;
    }

    /**
     * ????
     * 
     * @param outputStr 
     * @throws IOException 
     */
    public void append(String outputStr) throws IOException {
        this.delegateStream.writeChars(outputStr);

        if (this.isFileSyncEachTime) {
            sync();
        }
    }

    /**
     * ??????
     * 
     * @param outputLine 
     * @throws IOException 
     */
    public void appendLine(String outputLine) throws IOException {
        this.delegateStream.writeChars(outputLine);
        this.delegateStream.writeChars(System.getProperty("line.separator"));

        if (this.isFileSyncEachTime) {
            sync();
        }
    }

    /**
     * ?????HDFS????
     * 
     * @throws IOException
     *             ??
     */
    public void sync() throws IOException {
        this.delegateStream.flush();
        this.delegateStream.sync();
    }

    /**
     * Close?
     * 
     * @throws IOException
     *             
     */
    public void close() throws IOException {
        sync();
        this.delegateStream.close();
    }
}