1.core-site.xml

  1. <configuration>
  2. <property>
  3. <name>fs.defaultFS</name>
  4. <value>hdfs://192.168.3.61:9820</value>
  5. </property>
  6. <property>
  7. <name>hadoop.tmp.dir</name>
  8. <value>/opt/hadoopdata</value>
  9. </property>
  10. </configuration>

2.pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.qmkj</groupId>
  6. <artifactId>hdfsclienttest</artifactId>
  7. <version>0.1</version>
  8. <name>hdfsclienttest</name>
  9. <!-- FIXME change it to the project's website -->
  10. <url>http://www.example.com</url>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.source>1.7</maven.compiler.source>
  14. <maven.compiler.target>1.7</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>junit</groupId>
  19. <artifactId>junit</artifactId>
  20. <version>4.11</version>
  21. <scope>test</scope>
  22. </dependency>
  23. <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs-client -->
  24. <dependency>
  25. <groupId>org.apache.hadoop</groupId>
  26. <artifactId>hadoop-hdfs-client</artifactId>
  27. <version>3.2.1</version>
  28. <scope>provided</scope>
  29. </dependency>
  30. <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
  31. <dependency>
  32. <groupId>org.apache.hadoop</groupId>
  33. <artifactId>hadoop-common</artifactId>
  34. <version>3.2.1</version>
  35. </dependency>
  36. <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
  37. <dependency>
  38. <groupId>org.apache.hadoop</groupId>
  39. <artifactId>hadoop-hdfs</artifactId>
  40. <version>3.2.1</version>
  41. </dependency>
  42. </dependencies>
  43. <build>
  44. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  45. <plugins>
  46. <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
  47. <plugin>
  48. <artifactId>maven-clean-plugin</artifactId>
  49. <version>3.1.0</version>
  50. </plugin>
  51. <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
  52. <plugin>
  53. <artifactId>maven-resources-plugin</artifactId>
  54. <version>3.0.2</version>
  55. </plugin>
  56. <plugin>
  57. <artifactId>maven-compiler-plugin</artifactId>
  58. <version>3.8.0</version>
  59. </plugin>
  60. <plugin>
  61. <artifactId>maven-surefire-plugin</artifactId>
  62. <version>2.22.1</version>
  63. </plugin>
  64. <plugin>
  65. <artifactId>maven-jar-plugin</artifactId>
  66. <version>3.0.2</version>
  67. </plugin>
  68. <plugin>
  69. <artifactId>maven-install-plugin</artifactId>
  70. <version>2.5.2</version>
  71. </plugin>
  72. <plugin>
  73. <artifactId>maven-deploy-plugin</artifactId>
  74. <version>2.8.2</version>
  75. </plugin>
  76. <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
  77. <plugin>
  78. <artifactId>maven-site-plugin</artifactId>
  79. <version>3.7.1</version>
  80. </plugin>
  81. <plugin>
  82. <artifactId>maven-project-info-reports-plugin</artifactId>
  83. <version>3.0.0</version>
  84. </plugin>
  85. </plugins>
  86. </pluginManagement>
  87. </build>
  88. </project>

3.测试代码

  1. package com.qmkj;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.fs.*;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.net.URI;
  9. /**
  10. * Unit test for simple App.
  11. */
  12. public class AppTest {
  13. FileSystem fs = null;
  14. @Before
  15. public void init() throws Exception {
  16. Configuration conf = new Configuration();
  17. //设立的设置url请注意,设置core-site.xml中配置fs.defaultFS的地址
  18. fs = FileSystem.get(new URI("hdfs://192.168.3.61:9820"), conf, "root");
  19. }
  20. @Test
  21. public void testAdd() throws Exception {
  22. fs.copyFromLocalFile(new Path("D:\\KK_Movies\\kk 2020-02-20 20-45-55.mp4"), new Path("/zhanglei"));
  23. fs.close();
  24. }
  25. /**
  26. * 从hdfs中复制文件到本地文件系统
  27. *
  28. * @throws IOException
  29. * @throws IllegalArgumentException
  30. */
  31. @Test
  32. public void testDownloadFileToLocal() throws IllegalArgumentException, IOException {
  33. // fs.copyToLocalFile(new Path("/mysql-connector-java-5.1.28.jar"), new
  34. // Path("d:/"));
  35. fs.copyToLocalFile(false, new Path("test.txt"), new Path("e:/"), true);
  36. fs.close();
  37. }
  38. /**
  39. * 目录操作
  40. *
  41. * @throws IllegalArgumentException
  42. * @throws IOException
  43. */
  44. @Test
  45. public void testMkdirAndDeleteAndRename() throws IllegalArgumentException, IOException {
  46. // 创建目录
  47. fs.mkdirs(new Path("/zhanglei/b1/c1"));
  48. // 删除文件夹 ,如果是非空文件夹,参数2必须给值true ,删除所有子文件夹
  49. fs.delete(new Path("/b1"), true);
  50. // 重命名文件或文件夹
  51. fs.rename(new Path("/zhanglei"), new Path("/qmkj"));
  52. }
  53. /**
  54. * 查看目录信息,只显示文件
  55. *
  56. * @throws IOException
  57. * @throws IllegalArgumentException
  58. * @throws FileNotFoundException
  59. */
  60. @Test
  61. public void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException {
  62. RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
  63. while (listFiles.hasNext()) {
  64. LocatedFileStatus fileStatus = listFiles.next();
  65. System.out.println(fileStatus.getPath().getName());
  66. System.out.println(fileStatus.getBlockSize());
  67. System.out.println(fileStatus.getPermission());
  68. System.out.println(fileStatus.getLen());
  69. BlockLocation[] blockLocations = fileStatus.getBlockLocations();
  70. for (BlockLocation bl : blockLocations) {
  71. System.out.println("block-length:" + bl.getLength() + "--" + "block-offset:" + bl.getOffset());
  72. String[] hosts = bl.getHosts();
  73. for (String host : hosts) {
  74. System.out.println(host);
  75. }
  76. }
  77. System.out.println("--------------打印的分割线--------------");
  78. }
  79. }
  80. /**
  81. * 查看文件及文件夹信息
  82. *
  83. * @throws IOException
  84. * @throws IllegalArgumentException
  85. * @throws FileNotFoundException
  86. */
  87. @Test
  88. public void testListAll() throws FileNotFoundException, IllegalArgumentException, IOException {
  89. //可以右击方法名,Run 测试一下。
  90. FileStatus[] listStatus = fs.listStatus(new Path("/"));
  91. String flag = "";
  92. for (FileStatus fstatus : listStatus) {
  93. if (fstatus.isFile()) {
  94. flag = "f-- ";
  95. } else {
  96. flag = "d-- ";
  97. }
  98. System.out.println(flag + fstatus.getPath().getName());
  99. System.out.println(fstatus.getPermission());
  100. }
  101. }
  102. }

testDownloadFileToLocal 这里测试请注意,本地也要装hdfs才可以

更多精彩请关注公众号【lovepythoncn】

hadoop3自学入门笔记(3)-java 操作hdfs的更多相关文章

  1. hadoop3自学入门笔记(2)—— HDFS分布式搭建

    一些介绍 Hadoop 2和Hadoop 3的端口区别 Hadoop 3 HDFS集群架构 我的集群规划 name ip role 61 192.168.3.61 namenode,datanode ...

  2. hadoop3自学入门笔记(1)——虚拟机安装和网络配置

    前言 年过30惶惶不安,又逢疫情,还是不断学习,强化自己的能力.hadoop的视频和书籍在15年的时候就看过,但是一直没动手实践过,要知道技术不经过实战,一点提升也没有.因此下定决心边学边做,希望能有 ...

  3. hadoop集群配置和在windows系统上运用java操作hdfs

    安装 配置 概念 hadoop常用shell命令 使用java操作hadoop 本文介绍hadoop集群配置和在windows系统上运用java操作hdfs 安装 http://mirror.bit. ...

  4. hadoop学习(三)HDFS常用命令以及java操作HDFS

    一.HDFS的常用命令 1.查看根目录下的信息:./hadoop dfs -ls 2.查看根目录下的in目录中的内容:./hadoop dfs -ls in或者./hadoop dfs -ls ./i ...

  5. java操作hdfs实例

    环境:window7+eclipse+vmware虚拟机+搭建好的hadoop环境(master.slave01.slave02) 内容:主要是在windows环境下,利用eclipse如何来操作hd ...

  6. 使用java操作HDFS

    新建Java Project; 1,右击项目,属性,Java Build Path,Libraries,Add External JARs(haddopp根目录下的所以jar): 2,做一下项目关联, ...

  7. Mongodb快速入门之使用Java操作Mongodb

    [IT168 专稿]在上一篇文章中,我们学习了Mongodb的安装和初步使用,在本文中,将学习如何使用Java去编程实现对Mongodb的操作. HelloWorld程序 学习任何程序的第一步,都是编 ...

  8. Mongodb入门并使用java操作Mongodb

    转载请注意出处:http://blog.csdn.net/zcm101 最近在学习NoSql,先从Mongodb入手,把最近学习的总结下. Mongodb下载安装 Mongodb的下载安装就不详细说了 ...

  9. java操作hdfs到数据库或者缓存

    使用hadoop工具将数据分析出来以后,须要做入库处理或者存到缓存中.不然就没了意义 一下是使用javaAPI操作hdfs存入缓存的代码: <span style="font-fami ...

随机推荐

  1. linux操作系统运行学习总结

    https://www.cnblogs.com/f-ck-need-u/p/10481466.html 操作系统学习总结 1.linux上面cpu通过上下文切换达到进程的不断切换,通过动态计算切换执行 ...

  2. 图像处理:AlphaBlend混合两张图片

    使用vs2017新建一个项目 混合A,B两张图的基础算法: outColor = srcColor * srcAlpha + destColor * (1 - srcAlpha) 输出颜色 = 源颜色 ...

  3. 前端基础JavaScript

    JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript提交给国际标准化组织ECM ...

  4. linux--->Linux centon6.9 安装mysql

    Linux centon6.9 安装mysql 查看系统自带的安装包 yum list installed | grep mysql 2.上边显示有mysql 有5.1和5.5两个版本,我安装5.5 ...

  5. Docker基础内容之资源限制

    内存限制 --memory:内存限定,格式是数字加单位,单位可以为 B.K.M.G.最小为 4M. --memory-swap:交换分区大小限定 CPU限制 --cpus:表示分配给容器可用的cpu资 ...

  6. SpringCloud与微服务Ⅱ --- 微服务概述

    一.什么是微服务 1) Martin Fowler论文对微服务的阐述(中文版) 2) 对单一应用进行拆分 3) 每一个独立的应用都有一个独立的进程 4) 拥有自己独立的数据库 5) 微服务化的核心就是 ...

  7. 【学习笔记】Linux基础(一):磁盘分区与Linux的安装(以CentOS为例)

    一.磁盘分区与Linux的安装(以CentOS为例) 0.说在安装之前 在Linux中,"一切设备皆文件",设备在/dev这个目录下 /dev/sd[a-p] 表示SCSI/SAT ...

  8. 虚拟机 ubuntu系统忘记密码如何进入

    重启 虚拟机 按住shift键 会出现下面的界面 按住‘e’进入下面的界面往下翻 更改红框勾到的字符串为:  rw init=/bin/bash 然后按F10进行引导 然后输入 :”passwd”  ...

  9. AS中使用真机调试时出现解析错误的问题

    时间:2019/12/8 今天使用usb调试程序时手机上出现了解析错误的问题,其实这个问题很简单,主要可能是你想要调试的程序的最低版本号大于你手机的安卓版本号的原因,只需要修改下面这个地方: buil ...

  10. PAT乙级(Basic Level)真题,福尔摩斯的约会

    题目描述 大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”.大侦探很快就明 ...