彷徨 | HDFS客户端API编程基本java操作 | 一

news/2024/5/20 4:09:17 标签: HDFS, 客户端

1 : 上传本地文件到HDFS

	@Test
	public void testUpload() throws Exception {
		Configuration conf = new Configuration();
		//默认值,可以不设置
		conf.set("dfs.blocksize", "128m");
		
		// 1.先获取一个访问HDFS客户端对象   
		// 参数1:URI-hdfs集群的访问地址   参数2:客户端需要携带的参数对象  参数3:指明客户端的身份
		FileSystem fs = FileSystem.get(new URI("hdfs://marshal:9000"), conf, "root");
		
		//fs的copyFromLocalFile()方法上传文件
		//ziliao.docx为给文件重命名
		fs.copyFromLocalFile(new Path("G:/a.docx"), new Path("/ziliao.docx"));
		
		//关闭资源
		fs.close();
	}

上传结果:

2 : 创建目录

	/**
	 * 测试创建目录
	 * @throws Exception
	 */
	@Test
	public void testMkdir() throws Exception {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(new URI("hdfs://marshal:9000"), conf, "root");
		//创建一个目录,aaa下面的bbb
		fs.mkdirs(new Path("/aaa/bbb"));
		fs.close();
	}

创建结果:

3 : 下载文件到本地

方法一:

下载操作,会涉及到客户端本地系统的访问,hadoop为本地访问专门封装了本地平台库(C语言)

具体做法:将本地库解压到任意位置,并将解压目录配置到HADOOP_HOME环境变量中

	/**
	 * 测试下载文件
	 * 具体做法:将本地库解压到任意位置,并将解压目录配置到HADOOP_HOME环境变量中
	 * @throws Exception
	 */
	@Test
	public void testDownLoad() throws Exception {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(new URI("hdfs://marshal:9000"), conf, "root");
		fs.copyToLocalFile(new Path("/ziliao.docx"), new Path("E:/"));
		fs.close();
	}

下载结果:

方法二 : 此方法不需要hadoop本地C语言库

	/**
	 * 测试下载文件
	 * @throws Exception
	 */
	@Test
	public void testDownLoad() throws Exception {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(new URI("hdfs://marshal:9000"), conf, "root");
		//方法一:使用hadoop本地C语言库
		//具体做法:将本地库解压到任意位置,并将解压目录配置到HADOOP_HOME环境变量中
		//fs.copyToLocalFile(new Path("/ziliao.docx"), new Path("E:/"));
		//方法二:使用java类库  第一个参数为是否是否删除源.中间俩个参数为路径,最后一个参数useRawLocalFileSystem为是用本地java库
		fs.copyToLocalFile(false, new Path("/ziliao.docx"), new Path("E:/"), true);
		fs.close();
	}

下载结果:

4 : 删除文件

	/**
	 * 测试删除
	 * @throws Exception
	 */
	@Test
	public void testRm() throws Exception {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(new URI("hdfs://marshal:9000"), conf, "root");
		//参数一:要删除的路径. 参数二:是否递归
		fs.delete(new Path("/aaa"), true);
		fs.close();
	}

删除之前:

删除之后:

5 : 移动或重命名文件或文件夹

	/**
	 * 测试移动或重命名文件或文件夹
	 * @throws Exception
	 */
	@Test
	public void testMv() throws Exception {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(new URI("hdfs://marshal:9000"), conf, "root");
		//第一个参数为原文件名或路径,第二个参数为修改的文件名或路径
		fs.rename(new Path("/ziliao.docx"), new Path("/haha.docx"));
		fs.close();
	}

重命名之前:

重命名之后:

6 : 判断文件或文件夹是否存在

代码;

	/**
	 * 判断文件是否存在
	 * @throws Exception
	 */
	@Test
	public void testIfExist() throws Exception {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(new URI("hdfs://marshal:9000"), conf, "root");
		boolean exists = fs.exists(new Path("/aaa"));
		System.out.println(exists);
		fs.close();
	}

文件 :

判断结果 : 不存在

7 : 判断一个路径是否为文件

	/**
	 * 判断文件或文件夹是否存在
	 * @throws Exception
	 */
	@Test
	public void testIfExist() throws Exception {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(new URI("hdfs://marshal:9000"), conf, "root");
		//boolean exists = fs.exists(new Path("/aaa"));
		boolean isfile = fs.isFile(new Path("haha.docx"));
		//System.out.println(exists);
		System.out.println(isfile);
		fs.close();
	}

8 : 查看文件目录,仅显示文件信息

	/**
	 * 查看文件目录
	 * @throws IOException
	 * @throws InterruptedException
	 * @throws URISyntaxException
	 */
	@Test
	public void testLs1() throws IOException, InterruptedException, URISyntaxException {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(new URI("hdfs://marshal:9000/"), conf, "root");
		// 思考:为何返回迭代器?
		RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path("/"), true);
		while(iterator.hasNext()) {
			LocatedFileStatus file = iterator.next();	
			System.out.println("文件的所属组:" + file.getGroup());
			System.out.println("文件的所有者:" + file.getOwner());
			System.out.println("文件的访问时间:" + file.getAccessTime());
			System.out.println("文件的块大小:" + file.getBlockSize());
			System.out.println("文件的总长度:" + file.getLen());
			System.out.println("文件的修改时间:" + file.getModificationTime());
			System.out.println("文件的副本数:" + file.getReplication());
			System.out.println("文件的路径:" + file.getPath());
			System.out.println("文件的权限:" + file.getPermission());
			BlockLocation[] blockLocations = file.getBlockLocations();
			System.out.println("文件的块位置信息---------------------------");
			for (BlockLocation blk : blockLocations) {
				System.out.println("块长度:" + blk.getLength());
				System.out.println("块在文件中的起始偏移量:" + blk.getOffset());
				System.out.println("块所在的datanode主机:" + Arrays.toString(blk.getHosts()));
			}
			System.out.println("文件的块位置信息---------------------------");
		}
	}

文件如下 : 

运行结果:

文件的所属组:supergroup
文件的所有者:root
文件的访问时间:1532698212551
文件的块大小:134217728
文件的总长度:729927107
文件的修改时间:1532698346956
文件的副本数:3
文件的路径:hdfs://marshal:9000/aaa/bbb/ideaIU-2018.1.6.win.zip
文件的权限:rw-r--r--
文件的块位置信息---------------------------
块长度:134217728
块在文件中的起始偏移量:0
块所在的datanode主机:[marshal002, marshal001, marshal]
块长度:134217728
块在文件中的起始偏移量:134217728
块所在的datanode主机:[marshal001, marshal002, marshal]
块长度:134217728
块在文件中的起始偏移量:268435456
块所在的datanode主机:[marshal002, marshal001, marshal]
块长度:134217728
块在文件中的起始偏移量:402653184
块所在的datanode主机:[marshal002, marshal001, marshal]
块长度:134217728
块在文件中的起始偏移量:536870912
块所在的datanode主机:[marshal002, marshal003, marshal]
块长度:58838467
块在文件中的起始偏移量:671088640
块所在的datanode主机:[marshal001, marshal003, marshal]
文件的块位置信息---------------------------
文件的所属组:supergroup
文件的所有者:root
文件的访问时间:1532687744326
文件的块大小:134217728
文件的总长度:1622342
文件的修改时间:1532681955786
文件的副本数:3
文件的路径:hdfs://marshal:9000/haha.docx
文件的权限:rw-r--r--
文件的块位置信息---------------------------
块长度:1622342
块在文件中的起始偏移量:0
块所在的datanode主机:[marshal, marshal001, marshal003]
文件的块位置信息---------------------------
文件的所属组:supergroup
文件的所有者:root
文件的访问时间:1532684863581
文件的块大小:134217728
文件的总长度:139
文件的修改时间:1532511577743
文件的副本数:3
文件的路径:hdfs://marshal:9000/hdfs-mgmt.sh
文件的权限:rw-r--r--
文件的块位置信息---------------------------
块长度:139
块在文件中的起始偏移量:0
块所在的datanode主机:[marshal002, marshal001, marshal003]
文件的块位置信息---------------------------

9 : 查看文件目录,显示文件以及文件夹信息

	/**
	 * 查看文件目录,显示文件和文件夹信息
	 * @throws IOException
	 * @throws InterruptedException
	 * @throws URISyntaxException
	 */
	public void testLs2() throws IOException, InterruptedException, URISyntaxException {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(new URI("hdfs://cts01:9000/"), conf, "root");
		// 思考:为何返回数组?
		FileStatus[] listStatus = fs.listStatus(new Path("/"));
		for (FileStatus f : listStatus) {
			System.out.println(f.getPath());
		}
		fs.close();
	}

运行结果 : 

hdfs://marshal:9000/aaa
hdfs://marshal:9000/haha.docx
hdfs://marshal:9000/hdfs-mgmt.sh

写的不好 , 请多关照 ! ! ! 

希望可以帮到大家


http://www.niftyadmin.cn/n/753056.html

相关文章

mui下拉刷新(更高自由度)

<div id"content" class"mui-scroll-wrapper"><div class"mui-scroll"><div class"loader loader-1"><!--下拉刷新动画&#xff1a;http://www.lanrenzhijia.com/js/4440.html--><div class"loader-o…

彷徨 | HDFS客户端基本shell操作

# HDFS 的shell练习 # hdfs version 查看HDFS版本 # hadoop fs -ls / 查看HDFS根目录下的文件和目录 # hdfs dfs -ls -R /这条会列出/目录下的左右文件&#xff0c;由于有-R参数&#xff0c;会在文件夹和子文件夹下执行ls操作 , 会递归 # hadoop fs -mkdir /test 在根目录创建…

JavaScript函数的默认参数(default parameter)

JavaScript函数的默认参数(default parameter) js函数参数的默认值都是undefined&#xff0c; ES5里&#xff0c;不支持直接在形参里写默认值。所以&#xff0c;要设置默认值&#xff0c;就要检测参数是否为undefined&#xff0c;按需求赋值。 function multiply(a, b) {b typ…

图片加载失败后的处理

$(".headImg img").error(function() {$(".headImg img").attr("src", "images/defaultHead.jpg");//加载默认图片$(".headImg img").error(null); //防止死循环})$("#serverItems img").error(function() {var i…

彷徨 | MobaXterm的安装和使用

官网:https://mobaxterm.mobatek.net/ 百度云:链接&#xff1a;https://pan.baidu.com/s/1cEut7cBDU5yZWQtUW8vBDQ 密码&#xff1a;inuw 1.先来看看SSH是什么&#xff0c;定义如下&#xff1a; SSH是一种可以保证用户远程登录到系统的协议。实际上&#xff0c;SSH是一个网络…

I帧、P帧、B帧、GOP、IDR 和PTS, DTS之间的关系

一.视频传输原理 视频是利用人眼视觉暂留的原理&#xff0c;通过播放一系列的图片&#xff0c;使人眼产生运动的感觉。单纯传输视频画面&#xff0c;视频量非常大&#xff0c;对现有的网络和存储来说是不可接受的。为了能够使视频便于传输和存储&#xff0c;人们发现视频有大量…

图片上传(ajax通过base64格式的字符串方式上传图片至服务端)

&#xff08;1&#xff09;getBase64(imgPath);//path为img路径 &#xff08;2&#xff09;function getBase64(imgPath) { //图片转base64 function getBase64Image(img, width, height) { //width、height调用时传入具体像素值&#xff0c;控制大小 ,不传则默认图像大小 …

洛谷P5292 [HNOI2019]校园旅行(二分图+最短路)

题面 传送门 题解 如果暴力的话&#xff0c;我们可以把所有的二元组全都扔进一个队列里&#xff0c;然后每次往两边更新同色点&#xff0c;这样的话复杂度是\(O(m^2)\) 怎么优化呢&#xff1f; 对于一个同色联通块&#xff0c;如果它是一个二分图&#xff0c;我们只要保留一棵生…