操作hdfs的api

操作hdfs的api

一、连接namenode以及读取hdfs中指定文件

public void testConnectNamenode() throws Exception{
	Configuration conf=new Configuration();
	FileSystem fs=FileSystem.get(new URI("hdfs://192.168.234.21:9000"), conf);
	InputStream in=fs.open(new Path("/park/1.txt"));
	OutputStream out=new FileOutputStream("1.txt");
	in.skip(5);
	IOUtils.copyBytes(in, out, conf);
}

二、上传文件到hdfs上

public void testPut() throws Exception{
	Configuration conf=new Configuration();
	conf.set("dfs.replication","1");
	FileSystem fs=FileSystem.get(new URI("hdfs://192.168.234.21:9000"),conf,"root");
	ByteArrayInputStream in=new ByteArrayInputStream("hello hdfs".getBytes());
	OutputStream out=fs.create(new Path("/park/2.txt"));
	IOUtils.copyBytes(in, out, conf);
}

三、从hdfs上删除文件

public void testDelete()throws Exception{
	Configuration conf=new Configuration();
	FileSystem fs=FileSystem.get(new URI("hdfs://192.168.234.21:9000"),conf,"root");
	//true表示无论目录是否为空,都删除掉。可以删除指定的文件
	fs.delete(new Path("/park01"),true);
	//false表示只能删除不为空的目录。
	fs.delete(new Path("/park01"),false);
	fs.close();
}    

四、在hdfs上创建文件夹

public void testMkdir()throws Exception{
	Configuration conf=new Configuration();
	FileSystem fs=FileSystem.get(new URI("hdfs://192.168.234.21:9000"),conf,"root");
	fs.mkdirs(new Path("/park02"));
}

五、查询hdfs指定目录下的文件

public void testLs()throws Exception{
	Configuration conf=new Configuration();
	FileSystem fs=FileSystem.get(new URI("hdfs://192.168.234.21:9000"),conf,"root");
	FileStatus[] ls=fs.listStatus(new Path("/"));
	for(FileStatus status:ls){
		System.out.println(status);
	}
}

六、递归查看指定目录下的文件

public void testLs()throws Exception{
	Configuration conf=new Configuration();
	FileSystem fs=FileSystem.get(new URI("hdfs://192.168.234.214:9000"),conf,"root");
	RemoteIterator<LocatedFileStatus> rt=fs.listFiles(new Path("/"), true);
	while(rt.hasNext()){
		System.out.println(rt.next());
	}
}

七、重命名

public void testCreateNewFile() throws Exception{
	Configuration conf=new Configuration();
	FileSystem fs=FileSystem.get(new URI("hdfs://192.168.234.176:9000"),conf,"root");
	fs.rename(new Path("/park"), new Path("/park01"));
}

八、从本地上传一个文件到HDFS上

public void testCopyFromLoaclFileSystem() throws Exception{
	Configuration conf=new Configuration();
	FileSystem fs=FileSystem.get(new URI("hdfs://192.168.234.176:9000"),conf,"root");
	fs.copyFromLocalFile(new Path("5.txt"), new Path("/park01"));
}

九、获取文件的块信息

public void testCopyFromLoaclFileSystem() throws Exception{
	Configuration conf=new Configuration();
	FileSystem fs=FileSystem.get(new URI("hdfs://192.168.234.176:9000"),conf,"root");
	BlockLocation[] data=fs.getFileBlockLocations(new Path("/park01/1.txt"),0,Integer.MaxValue);
	for(BlockLocation bl:data){
		System.out.println(bl);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值