hadoop自定义输出格式

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

        }

}
}

你可能感兴趣的:(hadoop)