linux<—>HDFS

HDFS之间

与linux基本相同
-setrep:设置HDFS中文件的副本数量

  1. [atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -setrep 10 /sanguo/shuguo/kongming.txt

HDFS—>client下载

  1. hadoop fs
  2. -getmerge
  3. -get = -copyToLocal
  4. hdfspath linuxpath

Client—>HDFS上传

  1. hadoop fs
  2. -appendToFile
  3. -put = -copyFormLocal
  4. -moveFromLocal
  5. linuxpath hdfspath

查看hdfs路径

  1. hdfs getconf -confKey fs.default.name

IDEA(API)<—>HDFS

创建maven工程

设置依赖

POM文件:

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.12</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.logging.log4j</groupId>
  9. <artifactId>log4j-slf4j-impl</artifactId>
  10. <version>2.12.0</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.hadoop</groupId>
  14. <artifactId>hadoop-client</artifactId>
  15. <version>3.1.3</version>
  16. </dependency>
  17. </dependencies>

设置配置文件

在项目的src/main/resources目录下,新建一个文件,命名为“log4j2.xml”,在文件中填入\

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="error" strict="true" name="XMLConfig">
  3. <Appenders>
  4. <!-- 类型名为Console,名称为必须属性 -->
  5. <Appender type="Console" name="STDOUT">
  6. <!-- 布局为PatternLayout的方式,
  7. 输出样式为[INFO] [2018-01-22 17:34:01][org.test.Console]I'm here -->
  8. <Layout type="PatternLayout"
  9. pattern="[%p] [%d{yyyy-MM-dd HH:mm:ss}][%c{10}]%m%n" />
  10. </Appender>
  11. </Appenders>
  12. <Loggers>
  13. <!-- 可加性为false -->
  14. <Logger name="test" level="info" additivity="false">
  15. <AppenderRef ref="STDOUT" />
  16. </Logger>
  17. <!-- root loggerConfig设置 -->
  18. <Root level="info">
  19. <AppenderRef ref="STDOUT" />
  20. </Root>
  21. </Loggers>
  22. </Configuration>

代码思路

  • 不创建流:
  1. 创建文件系统:

  2. 操作:文件系统调用方法进行操作

  3. 关闭资源


  • 创建流:

1.创建文件系统

2.
创建输入流
创建输出流
调用hadoop包的io流工具类IOUtil

3.关闭资源


API操作:
上传、下载、文件夹删除、文件名更改(移动文件夹)、文件详情查看、文件和文件夹判断

  1. public class HDFSClient {
  2. FileSystem fs = null;
  3. @Before//在操作执行前执行
  4. public void before() throws URISyntaxException, IOException, InterruptedException {
  5. //创建连接对象
  6. Configuration configuration = new Configuration();
  7. configuration.set("dfs.replication","2");
  8. fs = FileSystem.get(new URI("hdfs://hadoop102:9820"),
  9. configuration,"atguigu");
  10. }
  11. @After//在操作执行后执行
  12. public void after() throws IOException {
  13. //关闭资源
  14. if (fs != null) {
  15. fs.close();
  16. }
  17. }
  18. @Test
  19. public void upload() throws IOException {
  20. fs.copyFromLocalFile(false,true,
  21. new Path("C:\\Users\\ldc\\AppData\\Roaming\\feiq\\Recv Files\\时间同步问题.txt"),
  22. new Path("\\"));
  23. }
  24. @Test
  25. public void download() throws IOException {
  26. fs.copyToLocalFile(true,new Path("\\时间同步问题.txt"),new Path("C:\\Users\\ldc\\AppData\\Roaming\\feiq\\Recv Files"));
  27. }
  28. @Test
  29. public void delete() throws IOException {
  30. fs.delete(new Path("\\mybash"),true);
  31. }
  32. @Test
  33. public void rename() throws IOException {
  34. //修改名字
  35. // fs.rename(new Path("/happy"), new Path("/nothappy"));
  36. //移动文件
  37. fs.rename(new Path("/happy/jdk-8u212-linux-x64.tar.gz"),new Path("/good/hh"));
  38. System.out.println("成功移动文件");
  39. }
  40. /*
  41. * @Author ldc
  42. * @Description //TODO 生成目录
  43. * @Date 15:58 2021/4/12
  44. * @Param []
  45. * @return void
  46. */
  47. @Test
  48. public void mkdir() throws IOException {
  49. fs.mkdirs(new Path("/good"));
  50. }
  51. /*
  52. * @Author ldc
  53. * @Description //TODO 查看文件详情,只能是文件,不能是目录!
  54. * @Date 16:01 2021/4/12
  55. * @Param []
  56. * @return void
  57. */
  58. @Test
  59. public void list() throws IOException {
  60. //recursive 是找到指定目录下的所有文件,listfiles返回的是迭代器,功能和liststatus差不多
  61. RemoteIterator<LocatedFileStatus> lsInterator = fs.listFiles(new Path("\\"), false);
  62. while (lsInterator.hasNext()) {
  63. LocatedFileStatus next = lsInterator.next();
  64. System.out.println("文件名是:"+ next.getPath().getName());
  65. System.out.println("文件所属群是:"+ next.getGroup());
  66. System.out.println("文件所属主是:"+ next.getOwner());
  67. System.out.println("文件块的位置是:"+ Arrays.toString(next.getBlockLocations()));
  68. if (next.isDirectory()) {
  69. System.out.println("这个是目录");
  70. } else if (next.isFile()) {
  71. System.out.println("这个是文件");
  72. }
  73. System.out.println("===========================================");
  74. }
  75. }
  76. @Test
  77. public void touch() throws IOException {
  78. fs.createNewFile(new Path("/a.txt"));
  79. }
  80. @Test
  81. public void fileType() throws IOException {
  82. //返回的指定路径的所有文件以及文件夹,返回的是数组,功能和listfiles差不多
  83. FileStatus[] fileStatuses = fs.listStatus(new Path("\\"));
  84. for (FileStatus fileStatus : fileStatuses) {
  85. if (fileStatus.isFile()) {
  86. System.out.println(fileStatus.getPath().getName()+"是文件");
  87. } else {
  88. System.out.println(fileStatus.getPath().getName()+"是目录");
  89. }
  90. }
  91. }
  92. //io流
  93. @Test
  94. public void uploadIO() throws IOException {
  95. FileInputStream fis = new FileInputStream("C:\\Users\\ldc\\AppData\\Roaming\\feiq\\Recv Files\\尚硅谷大数据技术之Hadoop(入门)V3.0.docx");
  96. FSDataOutputStream fds = fs.create(new Path("\\nothappy\\尚硅谷大数据技术之Hadoop(入门)V3.0.docx"));
  97. //引入io流工具类
  98. IOUtils.copyBytes(fis,fds,1024,true);
  99. }
  100. @Test
  101. public void downloadIO() throws IOException {
  102. FSDataInputStream fis = fs.open(new Path("\\nothappy\\尚硅谷大数据技术之Hadoop(入门)V3.0.docx"));
  103. FileOutputStream fos = new FileOutputStream("C:\\ldc_zoom\\ShangBigDatas\\03-BigData\\04-hadoop\\04-hadoop\\4.视频\\尚硅谷大数据技术之Hadoop(入门)V3.0.docx");
  104. IOUtils.copyBytes(fis,fos,1024,true);
  105. }
  106. }