SequenceFile 读写操作

http://blog.csdn.net/xiao_jun_0820/article/details/27708371

import java.io.IOException;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.SequenceFile.CompressionType;
import org.apache.hadoop.io.SequenceFile.Reader;
import org.apache.hadoop.io.SequenceFile.Writer;
import org.apache.hadoop.io.SequenceFile.Writer.Option;
import org.apache.hadoop.io.Text;


public class TestSequenceFile {


    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // // TODO Auto-generated method stub
        Configuration conf = new Configuration();
        Path seqFile = new Path("/test/seqFile2.seq");
        // Writer内部类用于文件的写操作,假设Key和Value都为Text类型
        SequenceFile.Writer writer = SequenceFile.createWriter(
                // 配置
                conf,
                // 文件位置
                Writer.file(seqFile),
                // 顺序文件K
                Writer.keyClass(Text.class),
                // 顺序文件V
                Writer.valueClass(Text.class),
                // 是否压缩
                Writer.compression(CompressionType.NONE)
                );


        // 通过writer向文档中写入记录
        writer.append(new Text("key"), new Text("value"));


        IOUtils.closeStream(writer);// 关闭write流


        // 通过reader从文档中读取记录
        SequenceFile.Reader reader = new SequenceFile.Reader(
                // 配置
                conf,
                // 读文件
                Reader.file(seqFile)
                );
        Text key = new Text();
        Text value = new Text();
        // 将文件读出来,打印
        while (reader.next(key, value)) {
            System.out.println(key);
            System.out.println(value);
        }
        IOUtils.closeStream(reader);// 关闭read流
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值