JAVA操作HDFS

Java操作hdfs有两种方式,这一节主要是用java
客户端操作,主要是对hdfs的增删查改。

import java.io.IOException;
import java.net.URISyntaxException;


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.hdfs.RemoteBlockReader;

import java.net.URI;
import org.junit.Before;
import org.junit.Test;

public class hdfstest {
	    FileSystem fs = null;
		@SuppressWarnings("deprecation")
		@Before
		public void init() throws IOException,URISyntaxException,InterruptedException {
			 Configuration conf = new Configuration();
			 conf.set("fs.defaultFS", "hdfs://node1:9000");
//			 get方法取得文件操作系统的客户端实力对象
			 fs= FileSystem.get(new URI("hdfs://node1:9000"),conf,"hadoop");	 
		}
//		上传文件
		@Test
		public void testUpload() throws IOException,InterruptedException{
			fs.copyFromLocalFile(new Path("c:/hadoop.lib"), new Path("/hadoop.lib.copy") );
			fs.close();}
		
		
//		创建新的目录
		@Test
		public void mkDirs() throws IOException {
			boolean mkdirs = fs.mkdirs(new Path ("/testmkdir/aaa"));
			System.out.println(mkdirs);}
			
		
//		删除目录
		@Test
		public void testdelete() throws IOException {
			boolean flag = fs.delete(new Path("/testmkdir/aaa"));
			System.out.println(flag);
		}
		
//		查询文件
		@Test
		public void testLs() throws IOException {
			FileStatus[] listatus  =fs.listStatus(new Path("/"));
			for(FileStatus list:listatus) {
				System.out.println(list.getBlockSize());
				System.out.println(list.getGroup());
				System.out.println(list.getPath());
			}
		}
		public static void main(String[] args)throws IOException,InterruptedException {}
			
		
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值