如何将.txt文件转换为Hadoop的序列文件格式
要有效利用Hadoop中的map-reduce作业,我需要将数据存储在hadoop's sequence file format中。然而,目前的数据只是平坦的.txt格式。任何人都可以提出一种方法,我可以将.txt文件转换为序列文件?如何将.txt文件转换为Hadoop的序列文件格式
所以最简单的答案只是一个具有SequenceFile输出的“身份”作业。
看起来这在java中:
public static void main(String[] args) throws IOException,
InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
Job job = new Job(conf);
job.setJobName("Convert Text");
job.setJarByClass(Mapper.class);
job.setMapperClass(Mapper.class);
job.setReducerClass(Reducer.class);
// increase if you need sorting or a special number of files
job.setNumReduceTasks(0);
job.setOutputKeyClass(LongWritable.class);
job.setOutputValueClass(Text.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
job.setInputFormatClass(TextInputFormat.class);
TextInputFormat.addInputPath(job, new Path("/lol"));
SequenceFileOutputFormat.setOutputPath(job, new Path("/lolz"));
// submit and wait for completion
job.waitForCompletion(true);
}
这取决于TXT文件的格式是什么。每记录一行吗?如果是这样,你可以简单地使用TextInputFormat,它为每一行创建一条记录。在您的映射器中,您可以解析该行并根据您的选择使用它。
如果它不是每条记录一行,则可能需要编写自己的InputFormat实现。看看this tutorial了解更多信息。
如果你的数据是不是在HDFS,你需要把它上传到HDFS。两个选项:
i)hdfs -put在你的.txt文件上,一旦你在HDFS上得到它,你可以将它转换为seq文件。 ii)在HDFS客户端框中输入文本文件作为输入,并通过创建SequenceFile.Writer并向其添加(键值)来使用序列文件API将其转换为SeqFile。
如果你不关心键,U可以使行号码作为关键和完整的文本价值。
我需要使用第一个选项。 我该怎么做? – zohar 2012-01-23 15:00:11
你也可以创建一个中间表,LOAD DATA的CSV内容直入,然后创建第二个表作为sequencefile(分区,集群,等..),并从中间表中插入选择。您还可以设置压缩,例如选项,然后
set hive.exec.compress.output = true;
set io.seqfile.compression.type = BLOCK;
set mapred.output.compression.codec = org.apache.hadoop.io.compress.SnappyCodec;
create table... stored as sequencefile;
insert overwrite table ... select * from ...;
的MR框架会照顾heavylifting的你,因此您不必编写Java代码的麻烦。
import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
//White, Tom (2012-05-10). Hadoop: The Definitive Guide (Kindle Locations 5375-5384). OReilly Media - A. Kindle Edition.
public class SequenceFileWriteDemo {
private static final String[] DATA = { "One, two, buckle my shoe", "Three, four, shut the door", "Five, six, pick up sticks", "Seven, eight, lay them straight", "Nine, ten, a big fat hen" };
public static void main(String[] args) throws IOException {
String uri = args[ 0];
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), conf);
Path path = new Path(uri);
IntWritable key = new IntWritable();
Text value = new Text();
SequenceFile.Writer writer = null;
try {
writer = SequenceFile.createWriter(fs, conf, path, key.getClass(), value.getClass());
for (int i = 0; i < 100; i ++) {
key.set(100 - i);
value.set(DATA[ i % DATA.length]);
System.out.printf("[% s]\t% s\t% s\n", writer.getLength(), key, value);
writer.append(key, value); }
} finally
{ IOUtils.closeStream(writer);
}
}
}
不错的简单例子! – user249654 2013-05-23 06:32:02
uri在这里是什么? – 2014-02-07 04:41:07
,如果你已经安装了Mahout的 - 它叫做:seqdirectory - 它可以做它
所以,如果我有100个.txt文件这会给我100个.SEQ文件,对不对?如果我想要一个大的.seq文件怎么办? – dranxo 2012-08-03 23:00:35
我猜测:job.setNumReduceTasks(1); – dranxo 2012-08-03 23:07:15
@rcompton是完全相同 – 2012-08-04 08:16:01