HDFS入门级代码

HDFS作为分布式文件系统,本人也不想过多的解释,就只想把初级的代码粘上来。

1.文件上传
public static void main(String [] args) throws IOException, InterruptedException, URISyntaxException {
		
	//文件上传
		Configuration conf= new Configuration();
		
		FileSystem fS=FileSystem.get(new URI("hdfs://hadoop02:9000") ,conf,"node");
		
		fS.copyFromLocalFile(true, new Path("f:/zhangsan.txt"), new Path("hdfs://hadoop02:9000/usr/local/zhangsan-cp.txt"));
		
		fS.close();
		System.out.println("successful");
 }
2.文件下载
 @Test
		public void putFileToHDFS() throws Exception, IOException{
			 Configuration configuration=new Configuration();
			 FileSystem fs=FileSystem.get(new URI("hdfs://hadoop02:9000"), configuration,"node");
			 fs.copyToLocalFile(false, new Path("hdfs://hadoop02:9000/usr/local/zyq-cp.txt"),new Path( "f:/zyqt.txt"), true);
			 fs.close();
		System.out.print("successful");
	}
3.文件创建
	//创建文件夹
		@Test
		public void mkdirAtHDFS()throws Exception{
			Configuration conf=new Configuration();
			
			FileSystem fs=FileSystem.get(new URI("hdfs://hadoop02:9000"),conf,"node");
			
			fs.mkdirs(new Path("hdfs://hadoop02:9000/usr/local/output01"));
		}
4.文件删除
	@Test
	public void deleteAtHDFS() throws Exception{
		Configuration conf=new Configuration();
		FileSystem fs=FileSystem.get(new URI("hdfs://hadoop02:9000"),conf,"node");
		fs.delete(new Path("hdfs://hadoop02:9000/usr/local/output01"),true);
		System.out.print("succeed");
	}
5.重命名文件
@Test
	public void renameAtHDFS()throws Exception{
		
		Configuration configuration=new Configuration();
		FileSystem fs=FileSystem.get(new URI("hdfs://hadoop02:9000"),configuration,"node");
		fs.rename(new Path("hdfs://hadoop02:9000/usr/local/output01/bajie.txt"), new Path("hdfs://hadoop02:9000/usr/local/output01/rbajie.txt"));
		System.out.println("Successful");
	}
6.文件详情查看
//文件详情查看
		@Test
		public void readListFiles() throws Exception{
			Configuration conf=new Configuration();
			
			FileSystem fs = FileSystem.get(new URI("hdfs://hadoop02:9000"), conf,"node");
			RemoteIterator<LocatedFileStatus>listFiles=fs.listFiles(new Path("/"),true);
			
			while(listFiles.hasNext()) {
				LocatedFileStatus fileStatus=listFiles.next();
				
				System.out.println(fileStatus.getPath().getName());
				System.out.println(fileStatus.getBlockSize());
				System.out.println(fileStatus.getPermission());
				System.out.println(fileStatus.getLen());
				BlockLocation[] blocklocations=fileStatus.getBlockLocations();
				
				for (BlockLocation b1:blocklocations) {
					System.out.println("block-offset:"+b1.getOffset());
					String[] hosts=b1.getHosts();
					for(String host:hosts) {
						System.out.println(host);
					}
				}
				System.out.println("----------------------本段已完,开启下一段---------------------------------------");
			}
			System.out.println("succeed");
		}
7.查看文件夹
	public void findAtHDFS() throws Exception,IllegalArgumentException,IOException{
			Configuration conf=new Configuration();
			FileSystem fs=FileSystem.get(new URI("hdfs://hadoop02:9000"), conf, "node");
			
			FileStatus[] listStatus=fs.listStatus(new Path("/"));
			
			for (FileStatus status:listStatus) {
				if (status.isFile()) {
					System.out.println("f--"+status.getPath().getName());
				}
				else {
					System.out.println("d--"+status.getPath().getName());
				}
			}
			System.out.println("successful");
		} 
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值