【Hadoop】API操作
创始人
2024-06-03 15:56:05
0

客户端准备

主机映射

IDEA

Maven

    org.apache.hadoophadoop-client3.1.3org.apache.hadoophadoop-hdfs3.1.3org.apache.hadoophadoop-hdfs-client3.1.3providedjunitjunit4.12org.slf4jslf4j-log4j121.7.30

日志

src/main/resources
log4j.rootLogger=INFO, stdout 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n 
log4j.appender.logfile=org.apache.log4j.FileAppender 
log4j.appender.logfile.File=target/spring.log 
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout 
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

fs.mkdirs(new Path("目录"));
上传
@Testpublic void testPut() throws IOException {// 参数解读:参数一:表示删除原数据; 参数二:是否允许覆盖;参数三:原数据路径; 参数四:目的地路径fs.copyFromLocalFile(false, true, new Path("D:\\sunwukong.txt"), new Path("hdfs://hadoop102/xiyou/huaguoshan"));}

下载

@Test
public void testGet() throws IOException {// 参数的解读:参数一:原文件是否删除;参数二:原文件路径HDFS; 参数三:目标地址路径Win ; 参数四:crc校验文件//fs.copyToLocalFile(true, new Path("hdfs://hadoop102/xiyou/huaguoshan/"), new Path("D:\\"), true);fs.copyToLocalFile(false, new Path("hdfs://hadoop102/a.txt"), new Path("D:\\"), false);
}
注意:如果执行上面代码,下载不了文件,有可能是你电脑的微软支持的运行库少,需 要安装一下微软运行库。 删除
@Testpublic void testRm() throws IOException {// 参数解读:参数1:要删除的路径; 参数2 : 是否递归删除// 删除文件//fs.delete(new Path("/jdk-8u212-linux-x64.tar.gz"),false);// 删除空目录//fs.delete(new Path("/xiyou"), false);// 删除非空目录fs.delete(new Path("/jinguo"), true);}
更名和移动
    @Testpublic void testmv() throws IOException {// 参数解读:参数1 :原文件路径; 参数2 :目标文件路径// 对文件名称的修改//fs.rename(new Path("/input/word.txt"), new Path("/input/ss.txt"));// 文件的移动和更名//fs.rename(new Path("/input/ss.txt"),new Path("/cls.txt"));// 目录更名fs.rename(new Path("/input"), new Path("/output"));}

文件详情

    @Testpublic void fileDetail() throws IOException {// 获取所有文件信息RemoteIterator listFiles = fs.listFiles(new Path("/"), true);// 遍历文件while (listFiles.hasNext()) {LocatedFileStatus fileStatus = listFiles.next();System.out.println("==========" + fileStatus.getPath() + "=========");System.out.println(fileStatus.getPermission());System.out.println(fileStatus.getOwner());System.out.println(fileStatus.getGroup());System.out.println(fileStatus.getLen());System.out.println(fileStatus.getModificationTime());System.out.println(fileStatus.getReplication());System.out.println(fileStatus.getBlockSize());System.out.println(fileStatus.getPath().getName());// 获取块信息BlockLocation[] blockLocations = fileStatus.getBlockLocations();System.out.println(Arrays.toString(blockLocations));}}
public int compareTo(Object o)          // 比较两个对象是否指向相同的路径
public long getAccessTime()             // 得到访问时间
public long getBlockSize()              // 得到块大小
public String getGroup()                // 得到组名
public long getLen()                    // 得到文件大小
public long getModificationTime()       // 得到修改时间
public String getOwner()                // 获取所有者信息
public Path getPath()                   // 获取Path路径
public FsPermission getPermission()     // 获取权限信息
public short getReplication()           // 获取块副本数
public path getsymlink()                // 获取符号链接的Path路径
public boolean isSymlink()              // 是否为符号链接
public void readFields(DataInput in)    // 序列化读取字段
public void setPath(final Path p)       // 设置Path路径
public void setSymlink(final Path p)    // 设置符号链接
public void write(DataOutput out)       // 序列化写入字段

文件和文件夹判断

    @Testpublic void testFile() throws IOException {FileStatus[] listStatus = fs.listStatus(new Path("/"));for (FileStatus status : listStatus) {if (status.isFile()) {System.out.println("文件:" + status.getPath().getName());} else {System.out.println("目录:" + status.getPath().getName());}}}

写hdfs

写相当于另一种上传文件

        logger.info("Begin Write file into hdfs");//Create a pathPath hdfswritepath = new Path(newFolderPath + "/" + fileName);//Init output streamFSDataOutputStream outputStream=fs.create(hdfswritepath);//Cassical output stream usageoutputStream.writeBytes(fileContent);outputStream.close();logger.info("End Write file into hdfs");

读hdfs

logger.info("Read file into hdfs");//Create a pathPath hdfsreadpath = new Path(newFolderPath + "/" + fileName);//Init input streamFSDataInputStream inputStream = fs.open(hdfsreadpath);//Classical input stream usageString out= IOUtils.toString(inputStream, "UTF-8");logger.info(out);inputStream.close();fs.close();

问题

1.

客户端去操作 HDFS 时,是有一个用户身份的。默认情况下,HDFS 客户端 API 会从采 用 Windows 默认用户访问 HDFS,会报权限异常错误。所以在访问 HDFS 时,一定要配置 用户。 解决: 1. 
String user="用户名"
fs = FileSystem.get(uri,configuration,user)
//或者 设置配置文件
System.setProperty("HADOOP_USER_NAME", "用户名")
System.setProperty("user.name", "用户名")

2.

 

 

-DHADOOP_USER_NAME=root 

2.

参数优先级排序:(1)客户端代码中设置的值

(2)ClassPath 下的用户自定义配置文 件

(3)然后是服务器的自定义配置(xxx-site.xml) (4)服务器的默认配置(xxx-default.xml) 创建目录  configuration.set() 为客户端代码中设置配置参数

为ClassPath下用户自定的配置文件

基本代码模板



如下实例dfs.replication1

来源:

尚硅谷

(31条消息) FileStatus类介绍_filestatus提供的所有属性和方法_yanzhelee的博客-CSDN博客

(31条消息) Java API 读写 hdfs分布式文件系统 (创建目录、读文件、写文件)_BeautifulGrils的博客-CSDN博客

 

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
带头循环双向链表来咯!!! 前言:继上文,我们了解了结构最简单的一种链表---单链表那么我们今天就来...