java本地文件合并传人hdfs_将存储在本地的大量分散的小文件,合并并保存在hdfs文件系统中...

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.URI;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

public class PutMerge {

private InputStream in = null;

private OutputStream out = null;

private String localPath;

private String hdfsPath;

public PutMerge(String localPath, String hdfsPath) {

this.localPath = localPath;

this.hdfsPath = hdfsPath;

}

public void start() throws Exception {

File file = new File(localPath);

Configuration conf = new Configuration();

FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);

out = fs.create(new Path(hdfsPath));

work(file);

close();

}

/*

* 1.根据用户定义的参数设置本地目录和HDFS的目标文件

*

* 2.创建一个输出流写入到HDFS文件

*

* 3.遍历本地目录中的每个文件,打开文件,并读取文件内容,将文件的内容写到HDFS文件中。

*/

private void work(File file) throws Exception {

if (file.isFile()) {

int byteRead = 0;

byte[] buffer = new byte[256];

while ((byteRead = in.read(buffer)) > 0) {

out.write(buffer, 0, byteRead);

}

} else if (file.isDirectory()) {

File[] files = file.listFiles();

for (int i = 0; i < files.length; i++) {

if (files[i].isFile()) {

in = new BufferedInputStream(new FileInputStream(files[i]));

int byteRead = 0;

byte[] buffer = new byte[256];

while ((byteRead = in.read(buffer)) > 0) {

out.write(buffer, 0, byteRead);

}

} else {

work(files[i]);

}

}

}

}

private void close() throws IOException {

if (in != null) {

in.close();

}

if (out != null) {

out.close();

}

}

/**

* @param args

* @throws IOException

*/

public static void main(String[] args) throws Exception {

if (args.length < 2) {

System.out.println("Usage:\n\t " + PutMerge.class.getName()

+ " [LocalPath] [HDFSPath]");

System.exit(1);

}

new PutMerge(args[0], args[1]).start();

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值