Hadoop 上传小文件 合成sequencefile 记录

    在Hadoop中,支持对二进制文件的处理,而sequencefile的方法,便是其中的重点。

    以下为实践中实现的经验:(本次记录上传文件。)

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;

public class SequenceFileWriteDemo {
	private static String[] Data={
		"one,two show one",
		"three,four show two",
		"five,six show three",
		"seven,eight show four",
		"nine,ten show five"
	};
	public void writeText() throws IOException{
		String uri="hdfs://192.168.50.28:8020/user/root/jyl/testImageSequenceFile";
		Configuration con=new Configuration();
		FileSystem fs=FileSystem.get(URI.create(uri), con);
		Path path=new Path("uri");
		IntWritable key=new IntWritable();
		Text value=new Text();
		SequenceFile.Writer writer=null;
		writer=SequenceFile.createWriter(fs, con, 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);
		}
		IOUtils.closeStream(writer);
	}
	
	public void writeImage() throws IOException{
		String uri="hdfs://192.168.50.28:8020/user/root/jyl/testByteImageSequenceFile";
		Configuration con=new Configuration();
		FileSystem fs=FileSystem.get(URI.create(uri), con);
		Path path=new Path(uri);
		BytesWritable key=new BytesWritable();
		
		BytesWritable value=new BytesWritable();
		
		File file1=new File("/mnt/disk1/yl/images/zhouzhou.jpg");
		File file2=new File("/mnt/disk1/yl/images/gouhuo.jpg");
		
		InputStream in1=new FileInputStream(file1);
		InputStream in2=new FileInputStream(file2);
		
		byte[] byte1=new byte[(int) file1.length()];
		byte[] byte2=new byte[(int) file2.length()];
		
		
		in1.read(byte1);
		in2.read(byte2);
		
		SequenceFile.Writer writer=null;
		writer=SequenceFile.createWriter(fs, con, path, BytesWritable.class,value.getClass());
		
		byte[] b1=new byte[1];
		b1[0]=1;
		BytesWritable bw1 = new BytesWritable(b1);
		
		byte[] b2=new byte[1];
		b2[0]=2;
		BytesWritable bw2 = new BytesWritable(b2);
		
		
		value.set(byte1, 0, byte1.length);
		writer.append(bw1,value);
		value.set(byte2, 0, byte2.length);
		writer.append(bw2,value);
		
		IOUtils.closeStream(writer);
	}
	
	public static void main(String[] args) throws IOException {
		SequenceFileWriteDemo demo=new SequenceFileWriteDemo();
		demo.writeImage();
//		demo.writeText();
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值