使用Java操作HDFS

          在搭完了高可用HDFS集群后,可以在Eclipse中使用Java来操作HDFS,进行文件的读写等操作。

高可用HDFS集群搭建步骤: https://blog.csdn.net/Chris_MZJ/article/details/83033471

使用Eclipse连接HDFS

1、将hadoop-eclipse-plugin-2.6.0.rar放到eclipse的安装目录下,如下图:
在这里插入图片描述

2、重启eclipse,点击eclipse右上角的Open Perspective

在这里插入图片描述
3、选中map/reduce视图,点击open
在这里插入图片描述
4、在下方Map/Reduce Locations视图空白处右键new Hadoop Location
在这里插入图片描述

5、在Host填写一个NameNode节点,我的是node1,如果没有修改系统的hosts文件是会出错的,端口填写8020端口

在这里插入图片描述
6、修改hosts文件:C:\Windows\System32\drivers\etc
在这里插入图片描述

7、打开hosts文件后编辑,添加HDFS的四个节点的路由
在这里插入图片描述
8、eclipse连上HDFS的视图:
在这里插入图片描述
此时,可以看到hdfs的目录结构,但是当选中一个文件进行操作时,报如下错误:
在这里插入图片描述
原因是windows缺少操作hadoop的配置文件hadoop.dll,将hadoop.dll复制放到系统盘的Windows/System32目录下即可。hadoop.dll是winutils中的一个文件,没有这个文件的伙计可以去gitHub下载:https://github.com/srccodes/hadoop-common-2.2.0-bin
直接下载此项目的zip包,下载后是文件名是hadoop-common-2.2.0-bin-master.zip,随便解压到一个目录即可,下载后的目录结构如下,下载完记得将hadoop.dll放到Windows/System32目录下:
在这里插入图片描述
接着重启eclipse,即可查看hdfs中的文件内容,如图:
在这里插入图片描述
注意:如果从将从hadoop.dll文件放到了Windows/System32目录下还是不能查看hdfs的文件内容的话,可能是hadoop.dll的版本不行了,需要重新下载其他的hadoop.dll,下载地址:

链接:https://pan.baidu.com/s/1F0bTIwuxtvFSDEXM4M26CA
提取码:wiog

9、新建一个动态web项目测试HDFS:
在这里插入图片描述
其实配置文件core-site.xml和hdfs-site.xml是从node1节点中hadoop的安装目录下copy出来的 。

测试源代码:

package com.hpe.hadoop.hdfs;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.HdfsBlockLocation;
import org.apache.hadoop.fs.Path;

import com.sun.xml.bind.v2.schemagen.xmlschema.List;

/**
 * 1、查看文件
 * 2、创建新文件夹
 * 3、上传文件
 * 4、下载文件
 * 5、删除文件
 * 6、内部移动
 * 7、内部复制
 * 8、重命名
 * 9、创建新的文件
 * 10、写文件
 * 11、读文件内容
 * @author eversec
 *
 */
public class TestHDFS {
	public static void main(String[] args) throws IOException {
		//操作HDFS之前得先创建配置对象
		Configuration conf = new Configuration(true);
		//创建操作HDFS的对象
		FileSystem fs = FileSystem.get(conf);
		//查看文件系统的内容
		List list = listFileSystem(fs,"/");
		
		//创建文件夹
		createDir(fs,"/test/abc");
		
		//上传文件
		uploadFileToHDFS(fs,"d:/test.txt","/test/abc/");
		
		//下载文件
		downLoadFileFromHDFS(fs,"/test/abc/test.txt","d:/");
		
		//删除.....
		
		//重命名
		renameFile(fs,"/test/abc/test.txt","/test/abc/Angelababy.txt");
		
		//内部移动 内部复制
		innerCopyAndMoveFile(fs,conf,"/test/abc/Angelababy.txt","/");
		
		//创建一个新文件
		createNewFile(fs,"/test/abc/hanhong");
		
		//写文件
		writeToHDFSFile(fs,"/test/abc/hanhong","hello world");
		//追加写
		appendToHDFSFile(fs,"/test/abc/hanhong","\nhello world");
		
		//读文件内容
		readFromHDFSFile(fs,"/test/abc/hanhong");
		
		//获取数据的位置
		getFileLocation(fs,"/test/abc/hanhong");
	}

	private static void getFileLocation(FileSystem fs, String string) throws IOException {
		FileStatus fileStatus = fs.getFileStatus(new Path(string));
		long len = fileStatus.getLen();
		BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(fileStatus, 0, len);
		String[] hosts = fileBlockLocations[0].getHosts();
		for (String string2 : hosts) {
			System.out.println(string2);
		}
		
		HdfsBlockLocation blockLocation = (HdfsBlockLocation)fileBlockLocations[0];
		long blockId = blockLocation.getLocatedBlock().getBlock().getBlockId();
		System.out.println(blockId);
	}

	private static void readFromHDFSFile(FileSystem fs, String string) throws IllegalArgumentException, IOException {
		FSDataInputStream inputStream = fs.open(new Path(string));
		
		FileStatus fileStatus = fs.getFileStatus(new Path(string));
		
		
		
		long len = fileStatus.getLen();
		
		byte[] b = new byte[(int)len];
		int read = inputStream.read(b);
		while(read != -1){
			System.out.println(new String(b));
			read = inputStream.read(b);
		}
		
		
	}

	private static void appendToHDFSFile(FileSystem fs, String filePath, String content) throws IllegalArgumentException, IOException {
		FSDataOutputStream append = fs.append(new Path(filePath));
		append.write(content.getBytes("UTF-8"));
		append.flush();
		append.close();
	}

	private static void writeToHDFSFile(FileSystem fs, String filePath, String content) throws IllegalArgumentException, IOException {
		 FSDataOutputStream outputStream = fs.create(new Path(filePath));
		 outputStream.write(content.getBytes("UTF-8"));
		 outputStream.flush();
		 outputStream.close();
	}

	private static void createNewFile(FileSystem fs, String string) throws IllegalArgumentException, IOException {
		fs.createNewFile(new Path(string));
	}

	private static void innerCopyAndMoveFile(FileSystem fs, Configuration conf,String src, String dest) throws IOException {
		Path srcPath = new Path(src);
		Path destPath = new Path(dest);
		
		//内部拷贝
//		FileUtil.copy(srcPath.getFileSystem(conf), srcPath, destPath.getFileSystem(conf), destPath,false, conf);
		//内部移动
		FileUtil.copy(srcPath.getFileSystem(conf), srcPath, destPath.getFileSystem(conf), destPath,true, conf);
	}

	private static void renameFile(FileSystem fs, String src, String dest) throws IOException {
		Path srcPath = new Path(src);
		Path destPath = new Path(dest);
		
		fs.rename(srcPath, destPath);
	
	}

	private static void downLoadFileFromHDFS(FileSystem fs, String src, String dest) throws IOException {
		Path srcPath = new Path(src);
		Path destPath = new Path(dest);
		//copyToLocal
//		fs.copyToLocalFile(srcPath, destPath);
		//moveToLocal
		fs.copyToLocalFile(true,srcPath, destPath);
	}

	private static void uploadFileToHDFS(FileSystem fs, String src, String dest) throws IOException {
		Path srcPath = new Path(src);
		Path destPath = new Path(dest);
		//copyFromLocal
//		fs.copyFromLocalFile(srcPath, destPath);
		//moveFromLocal
		fs.copyFromLocalFile(true,srcPath, destPath);
	}

	private static void createDir(FileSystem fs, String string) throws IllegalArgumentException, IOException {
		Path path = new Path(string);
		if(fs.exists(path)){
			fs.delete(path, true);
		}
		fs.mkdirs(path);
	}

	private static List listFileSystem(FileSystem fs, String path) throws FileNotFoundException, IOException {
		Path ppath = new Path(path);
		
		FileStatus[] listStatus = fs.listStatus(ppath);
		
		for (FileStatus fileStatus : listStatus) {
			System.out.println(fileStatus.getPath());
		}
		
		return null;
	}
}

正常情况下,测试过程中肯定是会报错的,一般报错如下:
在这里插入图片描述
从上述报错可以看出,主要的错误是Permission denied ,hdfs是防君子不防小人的,由于本机的用户是chris,那么是没有权限对hdfs进行增删改操作的,所以我们需要配置一个环境变量HADOOP_USER_NAME,告诉hdfs当前用户是root用户:
在这里插入图片描述
再次执行代码还是会报错:
在这里插入图片描述
从上面报错可以看出eclipse想对hdfs进行CUD还需要借助winutils,因此需要再次配置环境变量,上面从gitHub下载了winutils的压缩包并解压,配置增加环境变量HADOOP_HOME,值是下载的zip包解压的目录,然后在系统变量path里增加$HADOOP_HOME\bin 即可。

在这里插入图片描述
在这里插入图片描述
至此,eclipse就可以对hdfs为所欲为啦。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值