通过FileSystem API 访问获取HDFS数据,创建文件,获取文件相关属性的方法

54 篇文章 0 订阅
44 篇文章 0 订阅

1.从hadoop URL读取数据

 
	static {
		URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());	 
	}

	@Test
	public void readByURL() throws Exception {
		URL _url = new URL("hdfs://master:9000/test.txt");//指定路径
		InputStream in = _url.openStream();//打开流
		IOUtils.copyBytes(in, System.out, 4096, false);
                    //传参:System.out:表示输出到控制台
                            4096:缓冲大小buffsize
                            false:是否关闭流
		IOUtils.closeStream(in);
	

2.
        通过FileSystem API访问读取HDFS数据

    
        /*
	 * 通过FileSystem API访问读取HDFS数据
	 * $>hadoop fs -cat  /spaceQuota/text.txt
	 */
	@Test
	public void readByFS() throws IOException{
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);
		String _path = "hdfs://master:9000/spaceQuota/text.txt";
		FSDataInputStream  in = fs.open(new Path(_path));
		IOUtils.copyBytes(in, System.out, 4096, true);
	}
 

3.  通过FileSystem API创建文件夹

        /*
	 * 通过FileSystem API创建文件夹
	 * $>hadoop fs -mkdir  /mkdir_byAPI  //没有修改权限,可能会报错
	 * 注意:修改集群权限: $>hadoop fs -chmod -R a+w /
	 */
	@Test
	public void mkdirByAPI()throws Exception{
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);
		Path _path = new Path("/mkdir_byAPI");
		fs.mkdirs(_path);
		fs.close();
	}


  4.FileStatus对象,获取文件的相关属性

        /*
	 * 通过FileSystem API获取一些相关参数,有一些过期的方法,如何采用现有的
	 */
	@Test
	public void getPar()throws Exception{
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);
		FileStatus fstu = fs.getFileStatus(new Path("/spaceQuota/text.txt"));
		long _blocksize = fstu.getBlockSize();
		System.out.println("getAccessTime="+fstu.getAccessTime());
		System.out.println("blockszie="+_blocksize);
		System.out.println("getGroup="+fstu.getGroup());
		System.out.println("getLen="+fstu.getLen());
		System.out.println("getModificationTime="+fstu.getModificationTime());
		System.out.println("getOwner="+fstu.getOwner());
		System.out.println("getReplication="+fstu.getReplication());
		System.out.println("getPath="+fstu.getPath());
	}

 









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值