客户端准备
主机映射
IDEA
Maven
org.apache.hadoop hadoop-client 3.1.3 org.apache.hadoop hadoop-hdfs 3.1.3 org.apache.hadoop hadoop-hdfs-client 3.1.3 provided junit junit 4.12 org.slf4j slf4j-log4j12 1.7.30
日志
src/main/resourceslog4j.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.replication 1
来源:
尚硅谷
(31条消息) FileStatus类介绍_filestatus提供的所有属性和方法_yanzhelee的博客-CSDN博客
(31条消息) Java API 读写 hdfs分布式文件系统 (创建目录、读文件、写文件)_BeautifulGrils的博客-CSDN博客
下一篇:Fluent中创建监测点