HDFS中JAVA API的使用(hadoop的文件上传和下载)

HDFS是一个分布式文件系统,既然是文件系统,就可以对其文件进行操作,比如说新建文件、删除文件、读取文件内容等操作。下面记录一下使用JAVA API对HDFS中的文件进行操作的过程。

  对分HDFS中的文件操作主要涉及一下几个类:

  Configuration类:该类的对象封转了客户端或者服务器的配置。

  FileSystem类:该类的对象是一个文件系统对象,可以用该对象的一些方法来对文件进行操作。FileSystem fs = FileSystem.get(conf);通过FileSystem的静态方法get获得该对象。

  FSDataInputStream和FSDataOutputStream:这两个类是HDFS中的输入输出流。分别通过FileSystem的open方法和create方法获得。

  具体如何对文件操作清下下面例子:

package hadoop;

import java.io.InputStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

/**
 * 测试文件:
 * @author z714303584
 * 
 * HDFS文件上传下载实例
 */
public class HbaseTest {
	
	//hadoop fs的配置文件
	static  Configuration conf = new Configuration(true);
	static{
		//指定hadoop fs的地址
		conf.set("fs.default.name", "hdfs://master:9000");
	}
	
	/**
	 * 将本地文件(filePath)上传到HDFS服务器的指定路径(dst)
	 * @param filePath
	 * @param dst
	 * @throws Exception
	 */
	public static void uploadFileToHDFS(String filePath,String dst) throws Exception {
		//创建一个文件系统
		FileSystem fs = FileSystem.get(conf);
		Path srcPath = new Path(filePath);
		Path dstPath = new Path(dst);
		Long start = System.currentTimeMillis();
		fs.copyFromLocalFile(false, srcPath, dstPath);
		System.out.println("Time:"+ (System.currentTimeMillis() - start));
		
		System.out.println("________________________Upload to "+conf.get("fs.default.name")+"________________________");
		fs.close();
		getDirectoryFromHdfs(dst);
	}
	/**
	 * 下载文件
	 * @param src
	 * @throws Exception
	 */
	public static void downLoadFileFromHDFS(String src) throws Exception {
		FileSystem fs = FileSystem.get(conf);
		Path  srcPath = new Path(src);
		InputStream in = fs.open(srcPath);
		try {
			//将文件COPY到标准输出(即控制台输出)
			IOUtils.copyBytes(in, System.out, 4096,false);
		}finally{
			 IOUtils.closeStream(in);
			fs.close();
		}
	}
	/**
	 * 遍历指定目录(direPath)下的所有文件
	 * @param direPath
	 * @throws Exception
	 */
	public static void  getDirectoryFromHdfs(String direPath) throws Exception{
		
		FileSystem fs = FileSystem.get(URI.create(direPath),conf);
		FileStatus[] filelist = fs.listStatus(new Path(direPath));
		for (int i = 0; i < filelist.length; i++) {
			System.out.println("_________________***********************____________________");
			FileStatus fileStatus = filelist[i];
			System.out.println("Name:"+fileStatus.getPath().getName());
			System.out.println("size:"+fileStatus.getLen());
			System.out.println("_________________***********************____________________");
		}
		fs.close();
	}
	/**
	 * 测试方法
	 * @param args
	 */
	public static void main(String[] args) {
		try {
//			getDirectoryFromHdfs("/hbase/");
			
//			uploadFileToHDFS("F:/数据库优化/2000W/1800w-2000w.csv", "/zhuss/hotel/data/");
			
			downLoadFileFromHDFS("/zhuss/hotel/data/最后5000.csv");
			
			
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值