acromusashi.stream.ml.common.notify.ResultFilePrinter.java Source code

Java tutorial

Introduction

Here is the source code for acromusashi.stream.ml.common.notify.ResultFilePrinter.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.ml.common.notify;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import storm.trident.operation.TridentOperationContext;

/**
 * ??
 * 
 * @author kimura
 * @param <T> ?
 */
public class ResultFilePrinter<T> implements ResultNotifier<T> {
    /** serialVersionUID */
    private static final long serialVersionUID = -8586160590825985631L;

    /** logger */
    private static Logger logger = LoggerFactory.getLogger(ResultLogPrinter.class);

    /**  */
    protected String filePath;

    /** ? */
    protected String encode;

    /** ? */
    protected String header;

    /**  */
    protected transient FileOutputStream outputStream;

    /**
     * ???????
     * 
     * @param filePath 
     * @param encode ?
     * @param header ?
     */
    public ResultFilePrinter(String filePath, String encode, String header) {
        this.filePath = filePath;
        this.encode = encode;
        this.header = header;
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("rawtypes")
    @Override
    public void initialize(Map conf, TridentOperationContext context) {
        try {
            this.outputStream = new FileOutputStream(this.filePath, true);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void notifyResult(T result) {
        try {
            IOUtils.write(this.header + result.toString(), this.outputStream, this.encode);
        } catch (IOException ex) {
            logger.warn("ResultFilePrint failed. File=" + this.filePath + ", Content=" + this.header
                    + result.toString(), ex);
        }
    }
}