hadoop单机模式读取sequence文件


import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.ReflectionUtils;
import org.apache.mahout.fpm.pfpgrowth.convertors.string.TopKStringPatterns;

public class ReadSeqFile {
String uri = "/home/hadoop/fpdir/output/part-r-00000";

private static SequenceFile.Reader reader = null;
private static Configuration conf = new Configuration();

public static class ReadFileMapper extends
Mapper<LongWritable, Text, Text, Text> {

@Override
public void map(LongWritable key, Text value,Context context) {
Text text = (Text) ReflectionUtils.newInstance(reader.getKeyClass(), conf);
TopKStringPatterns k =  (TopKStringPatterns)ReflectionUtils.newInstance(reader.getValueClass(), conf);
try {
while (reader.next(text, k)) {
//System.out.printf("%s\t%s\n", text, k);
context.write(text, new Text(k.toString()));
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
/**
* @param args
* @throws IOException
* @throws InterruptedException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

Job job = new Job(conf,"read seq file");
job.setJarByClass(ReadSeqFile.class);
job.setMapperClass(ReadFileMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
Path path = new Path("/home/hadoop/fpdir/output/part-r-00000");
FileSystem fs = FileSystem.get(conf);
reader = new SequenceFile.Reader(fs, path, conf);
FileInputFormat.addInputPath(job, path);
FileOutputFormat.setOutputPath(job, new Path("/home/hadoop/fpdir/testReadSeq"));
System.exit(job.waitForCompletion(true)?0:1);

}


 
10:07:28
威神 2015/6/3 10:07:28

package hadoop1;

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.Writable;

public class SeqTest {
public static void main(String[] args) {
org.apache.hadoop.io.SequenceFile.Reader reader = null;
java.io.FileOutputStream fos = null;
try {
String uri = "/home/hadoop/fpdir/output/part-r-00000";
org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
FileSystem fs = FileSystem.get(conf);
Path path = new Path(uri);
reader = new org.apache.hadoop.io.SequenceFile.Reader(fs, path, conf);
Writable key = (Writable) org.apache.hadoop.util.ReflectionUtils.newInstance(reader.getKeyClass(), conf);
Writable value = (Writable) org.apache.hadoop.util.ReflectionUtils.newInstance(reader.getValueClass(), conf);

int n=0;
while(reader.next(key, value)){
/* 如果解析出是乱码,尝试用户UTF8转码 */
//String valueStr = new String(value.toString().getBytes("ISO8859_1"),"GB2312");
System.out.println(value.toString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeStream(reader);
IOUtils.closeStream(fos);
}

}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值