HDFS API 读操作 -seek指针操作 ,append追加文件

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

1.通过java.net.URL类访问写入HDFS数据

        /*
	 * 通过java.net.URL类访问写入HDFS数据
	 * 结论:通过URL的方式不能实现对HDFS的写操作,抛java.net.UnknownServiceException: 
                protocol (协议)doesn't support output
	 */
	@Test
	public void writeByURL() throws Exception {
		URL _url  =new URL("hdfs://master1:9000/spaceQuota/hello.txt");
		URLConnection conn = _url.openConnection();
		OutputStream out = conn.getOutputStream();
		out.write("hello world".getBytes()); //向集群文件 写hello world
		out.close();
	}    

2.通过FileSystem API做write操作

          /*
	 * 通过FileSystem API做写操作
	 */
	@Test
	public void  writeByAPI() throws IOException{
		Configuration conf = new Configuration();
		FileSystem fs  =FileSystem.get(conf);
		Path file = new Path("/spaceQuota/hello.txt");
		FSDataOutputStream out = fs.create(file);
		out.write("hello world".getBytes());
		out.close();
	}

3.通过FileSystem API做写操作,动态设置相关参数:replication为2和blocksize为10字节

-----------------------------------------------------------------------------------------------
	/**
	 * 通过FileSystem API做写操作,动态设置相关参数:replication和blocksize
	 * 有一些参数集群生效,例如:dfs.namenode.fs-limits.min-block-size=10
	 * 注:加载config信息的优先级:
	 *     代码级-->{classPath/***-site.xml}-->{HOADOOP_HOME/etc/haddop/***-site.xml}
	 */
	@Test
	public void writeByAPIForBlocksize() throws IOException{
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://master1:9000");
		//conf.set("dfs.bytes-per-checksum", "10");//设置校验和为10
		FileSystem fs  =FileSystem.get(conf);
		Path file = new Path("/spaceQuota/hello6666.txt");
		FSDataOutputStream out = fs.create(file, true, 4096,(short)2, 10);
                //  fs.create(f, overwrite, bufferSize, replication, blockSize)
		out.write("hello world".getBytes());
		out.close();
	}
问题:出现如下错误时:
                org.apache.hadoop.HadoopIllegalArgumentException: 
                Invalid values: dfs.bytes-per-checksum (=512) must divide block size (=10).
原因是:block大小设置为10时,相应的dfs.bytes-per-checksum 也要重新设置
        解决:         
           conf.set("dfs.bytes-per-checksum", "10");//设置校验和为10

    4.seek操作

        /*
	 * 通过FileSystem API做read操作, 设置位置信息seek()
	 * 总结:FSDataInputStream:可执行Seekable(),
	 * 而FSDataOutputStream没有,
	 */
	@Test
	public void seekByAPI() throws IOException{
		Configuration conf=new Configuration();
		FileSystem fs=FileSystem.get(conf);
		Path file=new Path("/spaceQuota/hello.txt");
		FSDataInputStream in=fs.open(file);
		IOUtils.copyBytes(in,System.out,4096,false);//注意:流不能关闭
		in.seek(0);//使输入指针回到0,相当于又读了一遍到控制台
		IOUtils.copyBytes(in,System.out,4096,true);
	}
    结果:
     

    5.测试一致模型 :描述文件读/写的数据可见性

---------------------------------------------------------------------------------------
	/**
	 * 通过读写,测试文件系统的一致模型
	 */
	@Test
	public void  writeByAPIText() throws IOException{
		Configuration conf = new Configuration();
		conf.set("dfs.bytes-per-checksum", "10");
		FileSystem fs  =FileSystem.get(conf);
		Path file = new Path("/spaceQuota/hello.txt");
		//创建文件时,文件立即可见
		FSDataOutputStream out = fs.create(file,true,4096,(short)2,10);
		out.write("hello world1a".getBytes());
		out.flush();
		out.write("hello world2a".getBytes());
		out.hflush();
		out.write("hello world3a".getBytes());
		out.hsync();
		out.close();
	}	
	@Test
	public void  readByAPIText() throws IOException{
		Configuration conf = new Configuration();
		FileSystem fs  =FileSystem.get(conf);
		Path file = new Path("/spaceQuota/hello.txt");
		FSDataInputStream in = fs.open(file);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] b = new byte[1024];
		int bytesRead;
		while ((bytesRead = in.read(b))!=-1) {
			 out.write(b, 0, bytesRead);
		}
		System.out.println(new String(out.toByteArray()));
//		IOUtils.copyBytes(in, System.out, 4096,true);
	}

    6.append追加操作

---------------------------------------------------------------------------------------------
	/*
	 * 注意:如集群节点少于3个,会抛异常;解决方案修改
        【dfs.client.block.write.replace-datanode-on-failure.policy=NEVER】
	 */
	@Test
	public void  appendByAPI() throws IOException{
		Configuration conf = new Configuration();
		conf.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER"); //修改属性参数
		FileSystem fs = FileSystem.get(conf);
		Path file = new Path("/spaceQuota/hello.txt");
		FSDataOutputStream out = fs.append(file);
		out.writeChars("aaaa");
		out.close();
	}









  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值