创建一个普通的java项目




导入jar包




附:jar包如何来的









代码阶段
上传下载文件
| package com.sxuek; |
| |
| import org.apache.hadoop.conf.Configuration; |
| import org.apache.hadoop.fs.FileSystem; |
| import org.apache.hadoop.fs.Path; |
| |
| import java.io.IOException; |
| import java.net.URI; |
| import java.net.URISyntaxException; |
| |
| |
| |
| |
| public class Demo { |
| public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException { |
| |
| |
| Configuration conf = new Configuration(); |
| |
| |
| |
| |
| |
| |
| FileSystem fileSystem = FileSystem.get(new URI("hdfs://node1:9000"), conf, "root"); |
| |
| |
| |
| |
| |
| |
| fileSystem.copyFromLocalFile(false, new Path("G:\\shixun\\test1.txt"), new Path("/newDirectory/c")); |
| System.out.println("上传完成!"); |
| |
| |
| |
| |
| |
| System.out.println(fileSystem); |
| } |
| } |
操作java-api的方法
| package com.sxuek; |
| |
| import org.apache.hadoop.conf.Configuration; |
| import org.apache.hadoop.fs.*; |
| import org.codehaus.jackson.map.util.ISO8601Utils; |
| import org.junit.After; |
| import org.junit.Before; |
| import org.junit.Test; |
| import java.io.IOException; |
| import java.net.URI; |
| import java.net.URISyntaxException; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class TestHDFS { |
| public FileSystem fileSystem; |
| |
| @Before |
| public void init() { |
| Configuration conf = new Configuration(); |
| try { |
| fileSystem = this.fileSystem.get(new URI("hdfs://node1:9000"), conf, "root"); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } catch (URISyntaxException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| @Test |
| public void test() { |
| |
| try { |
| boolean mkdirs = fileSystem.mkdirs(new Path("/a/b/c")); |
| System.out.println(mkdirs); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| |
| } |
| |
| |
| @Test |
| public void test1() { |
| try { |
| |
| boolean delete = fileSystem.delete(new Path("/a/b/c"), false); |
| |
| System.out.println(delete); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| @Test |
| public void test2() { |
| try { |
| boolean exists = fileSystem.exists(new Path("/parent")); |
| System.out.println(exists); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| @Test |
| public void test3() { |
| try { |
| boolean isFile = fileSystem.isFile(new Path("/a")); |
| boolean isDirectory = fileSystem.isDirectory(new Path("/a")); |
| System.out.println(isFile); |
| System.out.println(isDirectory); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| @Test |
| public void test4() { |
| boolean rename = false; |
| try { |
| rename = fileSystem.rename(new Path("/a"), new Path("/newDirectory")); |
| System.out.println(rename); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| @Test |
| public void test5() { |
| try { |
| FileStatus fileStatus = fileSystem.getFileStatus(new Path("/newDirectory")); |
| |
| |
| |
| System.out.println("文件的作者为"+fileStatus.getOwner()); |
| System.out.println("文件的所属组为"+fileStatus.getGroup()); |
| System.out.println("文件的权限为"+fileStatus.getPermission()); |
| System.out.println("文件的路径为"+fileStatus.getPath()); |
| System.out.println("文件的最后一次修改时间为"+fileStatus.getModificationTime()); |
| System.out.println("文件是否为文件"+fileStatus.isFile()); |
| System.out.println("文件是否为目录"+fileStatus.isDirectory()); |
| System.out.println("文件的block块的大小为"+fileStatus.getBlockSize()); |
| System.out.println("文件的大小为"+fileStatus.getLen()); |
| System.out.println("文件的副本数为"+fileStatus.getReplication()); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| |
| } |
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void test6() throws IOException { |
| RemoteIterator<LocatedFileStatus> list = fileSystem.listFiles(new Path("/p"), true); |
| while (list.hasNext()){ |
| LocatedFileStatus status = list.next(); |
| System.out.println("文件路径"+status.getPath()); |
| } |
| } |
| |
| |
| @Test |
| public void test7() { |
| try { |
| |
| |
| |
| FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/newDirectory")); |
| for (FileStatus fileStatus: fileStatuses) { |
| System.out.println(fileStatus.getPath()); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| |
| } |
| |
| @After |
| public void destory() { |
| if (fileSystem != null) { |
| try { |
| fileSystem.close(); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| } |
练习题
| package com.sxuek; |
| |
| import org.apache.hadoop.conf.Configuration; |
| import org.apache.hadoop.fs.FileStatus; |
| import org.apache.hadoop.fs.FileSystem; |
| import org.apache.hadoop.fs.Path; |
| |
| import java.io.IOException; |
| import java.net.URI; |
| import java.net.URISyntaxException; |
| |
| |
| |
| public class Practice { |
| public static void main(String[] args) { |
| Configuration conf = new Configuration(); |
| try { |
| FileSystem fileSystem = FileSystem.get(new URI("hdfs://node1:9000"), conf, "root"); |
| FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/newDirectory")); |
| find(fileSystem, fileStatuses); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } catch (URISyntaxException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| public static void find(FileSystem fileSystem, FileStatus[] fileStatuses) { |
| for (FileStatus fileStatus : fileStatuses) { |
| Path path = fileStatus.getPath(); |
| try { |
| if (fileSystem.isDirectory(path)) { |
| System.out.println(path); |
| FileStatus[] fileStatuses1 = fileSystem.listStatus(path); |
| find(fileSystem, fileStatuses1); |
| } else { |
| System.out.println(path); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!