大数据笔记04--HDFS接口

搭建集群客户端

原因

集群内操作会产生由硬件导致的数据倾斜问题:
若每次上传都是选择某台DN作为客户端,根据默认备份机制,block会优先存储到本机DN,导致该节点磁盘IO大大超过其他节点;同时,block副本会由此节点向其他节点分发,导致网络IO负载过高,久而久之该节点性能会远远低于其他节点,导致数据将更少的往该节点存。

搭建步骤

  1. 准备一台新的服务器或虚拟机,必须能与集群通信
  2. 将配置好的安装包拷贝到该机上
  3. 配置环境变量
  4. 就能在该客户端正常操作HDFS

配置开发环境(windows)

  1. 在本地解压hadoop安装包
  2. 配置环境变量HADOOP_HOME
  3. 在eclipse->dropins->plugins中加入hadoop插件,便于在eclipse中操作HDFS
  4. 在项目中导入hadoop的jar包,在安装包的share/hadoop下的common、hdfs、tools内的jar包以及其lib的包
  5. 将hadoop的配置文件hdfs-site.xml和core-site.xml放入项目source文件夹中

JAVA操作HDFS

FileSystem类的常用方法

  • 查看文件信息
FileStatus[]  listStatus(Path path)

可以获取到的信息

  • long getAccessTime() 获取访问时间
  • long getBlockSize() 获取块大小
  • String getGroup() 获取属组
  • long getLen() 获取文件内容长度
  • long getModificationTime() 获取修改时间
  • String getOwner() 获取属主
  • Path getPath() 获取路径
  • FsPermission getPermission() 获取权限
  • boolean isDirectory() 是否为文件夹
  • boolean isFile() 是否为文件
    在这里插入图片描述
  • 创建文件夹
boolean mkdir(Path path)
  • 上传
void copyFromLocalFile(boolean delSrc, boolean overwrite, Path src, Path dst)
  • 下载
void copyToLocalFile(boolean delSrc, Path src, Path dst)
  • 删除
boolean delete(Path path, boolean r)
  • 重命名
boolean rename(Path src, Path dest)
  • 内部移动、复制(非FileSystem的方法)
//内部移动
FileUtil.copy(srcPath.getFileSystem(conf), srcPath, destPath.getFileSystem(conf), destPath,true, conf);
//内部拷贝
FileUtil.copy(srcPath.getFileSystem(conf), srcPath, destPath.getFileSystem(conf), destPath,false, conf);
  • 创建文件
boolean createNewFile(Path path)
  • 向文件中追加内容
FSDataOutputStream append(Path path)
  • 读文件
FSDataInputStream open(Path path)
  • 获取文件存储位置(block的位置)
BlockLocation[] getFileBlockLocations(FileStatus, start, len)

示例代码

/**
 * 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:/wc","/test/abc/");
		
		//下载文件
		downLoadFileFromHDFS(fs,"/test/abc/wc","d:/");
		
		//删除.....
		
		//重命名
		renameFile(fs,"/test/abc/wc","/test/abc/Angelababy");
		
		//内部移动 内部复制
		innerCopyAndMoveFile(fs,conf,"/test/abc/Angelababy","/");
		
		//创建一个新文件
		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,"/install.log");
	}

	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;
	}
}

错误处理

  1. HADOOP_HOME未配置或hadoop版本不匹配
  2. 无权限–>添加HADOOP_USER_NAME=root
    或修改根目录权限
hdfs dfs -chmod -R 777 /

3.上传文件无大小–>原因未明,替换正确hadoop版本后解决

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值