hadoopカスタム出力フォーマット

3167 ワード

1.主にこのOutputFormatクラスを継承し、その実現方法
getRecordWriter
checkOutputSpecs
getOutputCommitter

2.主に最初の方法を実現し、ここでは結果をmysqlに出力する.
public class NewSqlOutputFormat extends OutputFormat<Text, IntWritable> {

    @Override
    public RecordWriter getRecordWriter(TaskAttemptContext context)
            throws IOException, InterruptedException {
        // TODO Auto-generated method stub
        return new SqlRecordWriter();  //       
    }


    @Override
    public void checkOutputSpecs(JobContext context) throws IOException, InterruptedException {
        // TODO Auto-generated method stub

    }


    @Override
    public OutputCommitter getOutputCommitter(TaskAttemptContext context) throws IOException, InterruptedException {
        // TODO Auto-generated method stub
           return new FileOutputCommitter

                   (FileOutputFormat.getOutputPath(context), context);
    }
    protected class SqlRecordWriter extends RecordWriter<Text, IntWritable>{

        //          

        @Override
        public void write(Text key, IntWritable value) throws IOException, InterruptedException {
            // TODO Auto-generated method stub
            String data=key.toString();
            int num=value.get();
            Dao dao=new Dao();
            try {
                dao.insertdata(data, num);       mysql 
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


        @Override
        public void close(TaskAttemptContext context) throws IOException, InterruptedException {
            // TODO Auto-generated method stub

        }

}
}