gpt4 book ai didi

hadoop - 使用 Hadoop 2.0 Apis 读写 Sequencefile

转载 作者:可可西里 更新时间:2023-11-01 14:08:51 26 4
gpt4 key购买 nike

我正在寻找一个使用新 API 读取和写入序列文件的示例。

实际上我需要知道如何使用这些函数

 createWriter(Configuration conf, org.apache.hadoop.io.SequenceFile.Writer.Option... opts) 

旧定义不适合我:

SequenceFile.createWriter( fs, conf, path, key.getClass(), value.getClass());

同样,我需要知道读取序列文件的代码是什么,因为以下内容已弃用:

SequenceFile.Reader(fs, path, conf);

这里是使用相同的方法-

    String uri = args[0];
Configuration conf = new Configuration();
Path path = new Path( uri);

IntWritable key = new IntWritable();
Text value = new Text();

CompressionCodec Codec = new GzipCodec();
SequenceFile.Writer writer = null;
Option optPath = SequenceFile.Writer.file(path);
Option optKey = SequenceFile.Writer.keyClass(key.getClass());
Option optVal = SequenceFile.Writer.valueClass(value.getClass());
Option optCom = SequenceFile.Writer.compression(CompressionType.RECORD, Codec);

writer = SequenceFile.createWriter( conf, optPath, optKey, optVal, optCom);

最佳答案

public class SequenceFilesTest {
@Test
public void testSeqFileReadWrite() throws IOException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.getLocal(conf);
Path seqFilePath = new Path("file.seq");
SequenceFile.Writer writer = SequenceFile.createWriter(conf,
Writer.file(seqFilePath), Writer.keyClass(Text.class),
Writer.valueClass(IntWritable.class));

writer.append(new Text("key1"), new IntWritable(1));
writer.append(new Text("key2"), new IntWritable(2));

writer.close();

SequenceFile.Reader reader = new SequenceFile.Reader(conf,
Reader.file(seqFilePath));

Text key = new Text();
IntWritable val = new IntWritable();

while (reader.next(key, val)) {
System.err.println(key + "\t" + val);
}

reader.close();
}
}

关于hadoop - 使用 Hadoop 2.0 Apis 读写 Sequencefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16070587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com