3.1常用类

       3.1.1Configuration

Hadoop配置文件的管理类,该类的对象封装了客户端或者服务器的配置(配置集群时,所有的xml文件根节点都是configuration)

创建一个Configuration对象时,其构造方法会默认加载hadoop中的两个配置文件,分别是hdfs-site.xml以及core-site.xml,这两个文件中会有访问hdfs所需的参数值,主要是fs.default.name,指定了hdfs的地址,有了这个地址客户端就可以通过这个地址访问hdfs了。即可理解为configuration就是hadoop中的配置信息。

 

     3.1.2 FileSystem

该类的对象是一个文件系统对象,对HDFS中的文件进行的一系列操作,如创建等

 

      3.1.3FileStatus

获取文件或者文件夹的元信息!比如:文件路径,文件大小,文件所有者,所在的块大小,文件修改时间,备份数量,权限等!

 

       3.1.4FSDataInputStream

输入流对象! 可以将HDFS中的文件或者文件夹读取到本地!

 

     3.1.5FSDataOutputStream

输出流对象! 可以将本地的文件或者文件夹上传到HDFS中!

 

     3.1.6构建工程

指定工程路径

框架结构:

导入依赖节点

  1. <dependency>
  2. <groupId>org.apache.hadoop</groupId>
  3. <artifactId>hadoop-common</artifactId>
  4. <version>2.8.</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.hadoop</groupId>
  8. <artifactId>hadoop-hdfs</artifactId>
  9. <version>2.8.</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.apache.hadoop</groupId>
  13. <artifactId>hadoop-client</artifactId>
  14. <version>2.8.</version>
  15. </dependency>

查询文件信息

  1. // 查询文件信息
  2. private static void catFile() throws IOException, FileNotFoundException {
  3. // TODO Auto-generated method stub
  4. // 指定集群当中主机的IP+端口
  5. String uri = "hdfs://192.168.77.99:9000";
  6. // 加载Hadoop的配置文件
  7. Configuration con = new Configuration();
  8. // 创建一个可以操作的HDFS对象
  9. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  10. FileStatus[] listStatus = fileSystem.listStatus(new Path("/input/"));
  11. for (FileStatus fileStatus : listStatus) {
  12. /**
  13. * 快捷键Syso
  14. */
  15. System.out.print("file文件信息------》" + fileStatus);
  16. }
  17.  
  18. /**
  19. * 获取单个文件
  20. */
  21.  
  22. /**
  23. * FileStatus fileStatus = fileSystem.getFileStatus(new Path(
  24. * "input/file1.txt")); System.out.println(fileStatus);
  25. */
  26. }

查询文件内容

  1. public static void getFile(String fileName) throws Exception {
  2. // 指定集群中的主机IP+端口号
  3. String uri = "hdfs://192.168.77.99:9000";
  4. // 加载Hadoop的配置文件
  5. Configuration con = new Configuration();
  6. // 创建一个可以操作的HDFS对象
  7. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  8. // 获取指定的文件
  9. FSDataInputStream open = fileSystem.open(new Path(fileName));
  10. // 将文件的内容装载到BufferedReader对象中去
  11. BufferedReader reader = new BufferedReader(new InputStreamReader(open));
  12. String line = "";
  13. // 循环读取文件内容
  14. while ((line = reader.readLine()) != null) {
  15. System.out.println(line);
  16. }
  17. // 关闭资源
  18. reader.close();
  19. open.close();
  20. fileSystem.close();
  21. }

创建一个空的文件

  1. public static void createNewFile(String fileName) throws Exception {
  2. // 指定集群中的主机IP+端口号
  3. String uri = "hdfs://192.168.77.99:9000";
  4. // 加载Hadoop的配置文件
  5. Configuration con = new Configuration();
  6. // 创建一个可以操作的HDFS对象
  7. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  8. if (fileSystem.exists(new Path(fileName))) {
  9. System.out.println("文件已经存在");
  10. } else {
  11. boolean createNewFile = fileSystem
  12. .createNewFile(new Path(fileName));
  13. if (createNewFile) {
  14. System.out.println("成功");
  15. } else {
  16. System.out.println("失败");
  17. }
  18. }
  19. fileSystem.close();
  20. }

在新创建的文件夹写入内容

  1. public static void createFile(String fileName) throws Exception {
  2. // 指定集群中的主机IP+端口号
  3. String uri = "hdfs://192.168.77.99:9000";
  4. // 加载Hadoop的配置文件
  5. Configuration con = new Configuration();
  6. // 创建一个可以操作的HDFS对象
  7. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  8. if (fileSystem.exists(new Path(fileName))) {
  9. System.out.println("文件已经存在");
  10. } else {
  11. FSDataOutputStream create = fileSystem.create(new Path(fileName));
  12. String str = "老黑今天又黑了";
  13. create.write(str.getBytes());
  14. create.flush();
  15. create.close();
  16. }
  17. fileSystem.close();
  18. }

创建文件夹

  1. public static void mkdirFile(String fileName) throws Exception {
  2. // 指定集群中的主机IP+端口号
  3. String uri = "hdfs://192.168.77.99:9000";
  4. // 加载Hadoop的配置文件
  5. Configuration con = new Configuration();
  6. // 创建一个可以操作的HDFS对象
  7. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  8. boolean mkdirs = fileSystem.mkdirs(new Path(fileName));
  9. if (mkdirs) {
  10. System.out.println("成功!");
  11. } else {
  12. System.out.println("失败!");
  13. }
  14. fileSystem.close();
  15. }

重名文件的名字

  1. public static void renameFile(String ordername, String newname)
  2. throws Exception {
  3. // 指定集群中的主机IP+端口号
  4. String uri = "hdfs://192.168.77.99:9000";
  5. // 加载Hadoop的配置文件
  6. Configuration con = new Configuration();
  7. // 创建一个可以操作的HDFS对象
  8. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  9. boolean rename = fileSystem.rename(new Path(ordername), new Path(
  10. newname));
  11. if (rename) {
  12. System.out.println("成功!");
  13. } else {
  14. System.out.println("失败!");
  15. }
  16. fileSystem.close();
  17. }

删除文件夹

  1. @SuppressWarnings("deprecation")
  2. public static void deleteFile(String filename) throws Exception {
  3. // 指定集群中的主机IP+端口号
  4. String uri = "hdfs://192.168.77.99:9000";
  5. // 加载Hadoop的配置文件
  6. Configuration con = new Configuration();
  7. // 创建一个可以操作的HDFS对象
  8. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  9.  
  10. boolean delete = fileSystem.delete(new Path(filename));
  11. if (delete) {
  12. System.out.println("成功!");
  13. } else {
  14. System.out.println("失败!");
  15. }
  16. fileSystem.close();
  17. }

上传文件 Windows上传到HDFS上

  1. public static void fromFile(String localName, String fromName)
  2. throws Exception {
  3. // 指定集群中的主机IP+端口号
  4. String uri = "hdfs://192.168.77.99:9000";
  5. // 加载Hadoop的配置文件
  6. Configuration con = new Configuration();
  7. // 创建一个可以操作的HDFS对象
  8. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  9. fileSystem.copyFromLocalFile(new Path(localName), new Path(fromName));
  10. fileSystem.close();
  11. }

从hdfs下载到Windows

  1. public static void downLoad(String defsFile,String localFile) throws Exception {
  2. String uri = "hdfs://192.168.77.99:9000";
  3. // 加载hadoop配置文件
  4. Configuration con = new Configuration();
  5. // 创建一个可以操作HDFS对象
  6. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  7. fileSystem.copyToLocalFile(new Path(defsFile),new Path (localFile));
  8. fileSystem.close();
  9.  
  10. }

注意:要记得调用方法

完整的代码结构--各种操作的方法

  1. package com.hdfs;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.net.URI;
  8.  
  9. import org.apache.hadoop.conf.Configuration;
  10. import org.apache.hadoop.fs.FSDataInputStream;
  11. import org.apache.hadoop.fs.FSDataOutputStream;
  12. import org.apache.hadoop.fs.FileStatus;
  13. import org.apache.hadoop.fs.FileSystem;
  14. import org.apache.hadoop.fs.Path;
  15.  
  16. public class HDFS {
  17.  
  18. public static void main(String[] args) throws Exception {
  19. // catFile();
  20.  
  21. // getFile("/input/file1.txt");// 查询单个文件信息
  22.  
  23. // createNewFile("/input/file3.txt");
  24. // createFile("/input/file4.txt");
  25. // mkdirFile("/MKDIRS");//创建文件夹
  26. // renameFile("/input/file5.txt","/input/file4.txt");//重命名文件的名字
  27. //deleteFile("/MKDIRS");// 删除文件夹
  28. //fromFile("C:\\Users\\70424\\Desktop\\1.txt","/input");//上传文件Windows到HDFS上
  29. downLoad("/input/file1.txt","C:\\Users\\70424\\Desktop");//从hdfs下载到Windows
  30. }
  31.  
  32. // 查询文件信息
  33. private static void catFile() throws IOException, FileNotFoundException {
  34. // TODO Auto-generated method stub
  35. // 指定集群当中主机的IP+端口
  36. String uri = "hdfs://192.168.77.99:9000";
  37. // 加载Hadoop的配置文件
  38. Configuration con = new Configuration();
  39. // 创建一个可以操作的HDFS对象
  40. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  41. FileStatus[] listStatus = fileSystem.listStatus(new Path("/input/"));
  42. for (FileStatus fileStatus : listStatus) {
  43. /**
  44. * 快捷键Syso
  45. */
  46. System.out.print("file文件信息------》" + fileStatus);
  47. }
  48.  
  49. /**
  50. * 获取单个文件
  51. */
  52.  
  53. /**
  54. * FileStatus fileStatus = fileSystem.getFileStatus(new Path(
  55. * "input/file1.txt")); System.out.println(fileStatus);
  56. */
  57. }
  58.  
  59. // 查询文件内容
  60. public static void getFile(String fileName) throws Exception {
  61. // 指定集群中的主机IP+端口号
  62. String uri = "hdfs://192.168.77.99:9000";
  63. // 加载Hadoop的配置文件
  64. Configuration con = new Configuration();
  65. // 创建一个可以操作的HDFS对象
  66. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  67. // 获取指定的文件
  68. FSDataInputStream open = fileSystem.open(new Path(fileName));
  69. // 将文件的内容装载到BufferedReader对象中去
  70. BufferedReader reader = new BufferedReader(new InputStreamReader(open));
  71. String line = "";
  72. // 循环读取文件内容
  73. while ((line = reader.readLine()) != null) {
  74. System.out.println(line);
  75. }
  76. // 关闭资源
  77. reader.close();
  78. open.close();
  79. fileSystem.close();
  80. }
  81.  
  82. // 创建一个空的文件
  83. public static void createNewFile(String fileName) throws Exception {
  84. // 指定集群中的主机IP+端口号
  85. String uri = "hdfs://192.168.77.99:9000";
  86. // 加载Hadoop的配置文件
  87. Configuration con = new Configuration();
  88. // 创建一个可以操作的HDFS对象
  89. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  90. if (fileSystem.exists(new Path(fileName))) {
  91. System.out.println("文件已经存在");
  92. } else {
  93. boolean createNewFile = fileSystem
  94. .createNewFile(new Path(fileName));
  95. if (createNewFile) {
  96. System.out.println("成功");
  97. } else {
  98. System.out.println("失败");
  99. }
  100. }
  101. fileSystem.close();
  102. }
  103.  
  104. // 在新创建的文件夹写入内容
  105. public static void createFile(String fileName) throws Exception {
  106. // 指定集群中的主机IP+端口号
  107. String uri = "hdfs://192.168.77.99:9000";
  108. // 加载Hadoop的配置文件
  109. Configuration con = new Configuration();
  110. // 创建一个可以操作的HDFS对象
  111. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  112. if (fileSystem.exists(new Path(fileName))) {
  113. System.out.println("文件已经存在");
  114. } else {
  115. FSDataOutputStream create = fileSystem.create(new Path(fileName));
  116. String str = "老黑今天又黑了";
  117. create.write(str.getBytes());
  118. create.flush();
  119. create.close();
  120. }
  121. fileSystem.close();
  122. }
  123.  
  124. // 创建文件夹
  125. public static void mkdirFile(String fileName) throws Exception {
  126. // 指定集群中的主机IP+端口号
  127. String uri = "hdfs://192.168.77.99:9000";
  128. // 加载Hadoop的配置文件
  129. Configuration con = new Configuration();
  130. // 创建一个可以操作的HDFS对象
  131. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  132. boolean mkdirs = fileSystem.mkdirs(new Path(fileName));
  133. if (mkdirs) {
  134. System.out.println("成功!");
  135. } else {
  136. System.out.println("失败!");
  137. }
  138. fileSystem.close();
  139. }
  140.  
  141. // 重名文件的名字
  142. public static void renameFile(String ordername, String newname)
  143. throws Exception {
  144. // 指定集群中的主机IP+端口号
  145. String uri = "hdfs://192.168.77.99:9000";
  146. // 加载Hadoop的配置文件
  147. Configuration con = new Configuration();
  148. // 创建一个可以操作的HDFS对象
  149. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  150. boolean rename = fileSystem.rename(new Path(ordername), new Path(
  151. newname));
  152. if (rename) {
  153. System.out.println("成功!");
  154. } else {
  155. System.out.println("失败!");
  156. }
  157. fileSystem.close();
  158. }
  159.  
  160. // 删除文件夹
  161. @SuppressWarnings("deprecation")
  162. public static void deleteFile(String filename) throws Exception {
  163. // 指定集群中的主机IP+端口号
  164. String uri = "hdfs://192.168.77.99:9000";
  165. // 加载Hadoop的配置文件
  166. Configuration con = new Configuration();
  167. // 创建一个可以操作的HDFS对象
  168. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  169.  
  170. boolean delete = fileSystem.delete(new Path(filename));
  171. if (delete) {
  172. System.out.println("成功!");
  173. } else {
  174. System.out.println("失败!");
  175. }
  176. fileSystem.close();
  177. }
  178.  
  179. // 上传文件 Windows上传到HDFS上
  180. public static void fromFile(String localName, String fromName)
  181. throws Exception {
  182. // 指定集群中的主机IP+端口号
  183. String uri = "hdfs://192.168.77.99:9000";
  184. // 加载Hadoop的配置文件
  185. Configuration con = new Configuration();
  186. // 创建一个可以操作的HDFS对象
  187. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  188. fileSystem.copyFromLocalFile(new Path(localName), new Path(fromName));
  189. fileSystem.close();
  190. }
  191. //从hdfs下载到Windows
  192. public static void downLoad(String defsFile,String localFile) throws Exception {
  193. String uri = "hdfs://192.168.77.99:9000";
  194. // 加载hadoop配置文件
  195. Configuration con = new Configuration();
  196. // 创建一个可以操作HDFS对象
  197. FileSystem fileSystem = FileSystem.get(URI.create(uri), con);
  198. fileSystem.copyToLocalFile(new Path(defsFile),new Path (localFile));
  199. fileSystem.close();
  200.  
  201. }
  202.  
  203. }

HDFS API编程的更多相关文章

  1. 【HDFS API编程】从本地拷贝文件,从本地拷贝大文件,拷贝HDFS文件到本地

    接着之前继续API操作的学习 CopyFromLocalFile: 顾名思义,从本地文件拷贝 /** * 使用Java API操作HDFS文件系统 * 关键点: * 1)create Configur ...

  2. 【HDFS API编程】查看HDFS文件内容、创建文件并写入内容、更改文件名

    首先,重点重复重复再重复: /** * 使用Java API操作HDFS文件系统 * 关键点: * 1)创建 Configuration * 2)获取 FileSystem * 3)...剩下的就是 ...

  3. 【HDFS API编程】jUnit封装-改写创建文件夹

    首先:什么是jUnit  回顾: https://www.cnblogs.com/Liuyt-61/p/10374732.html 上一节我们知道: /** * 使用Java API操作HDFS文件系 ...

  4. 【HDFS API编程】第一个应用程序的开发-创建文件夹

    /** * 使用Java API操作HDFS文件系统 * 关键点: * 1)创建 Configuration * 2)获取 FileSystem * 3)...剩下的就是 HDFS API的操作了*/ ...

  5. 【HDFS API编程】开发环境搭建

    使用HDFS API的方式来操作HDFS文件系统 IDEA Java 使用Maven来管理项目 先打开IDEA,New Project 创建GAV然后next 默认使用的有idea内置的Maven,可 ...

  6. 【HDFS API编程】查看文件块信息

    现在我们把文件都存在HDFS文件系统之上,现在有一个jdk.zip文件存储在上面,我们想知道这个文件在哪些节点之上?切成了几个块?每个块的大小是怎么样?先上测试类代码: /** * 查看文件块信息 * ...

  7. 【HDFS API编程】查看目标文件夹下的所有文件、递归查看目标文件夹下的所有文件

    使用hadoop命令:hadoop fs -ls /hdfsapi/test  我们能够查看HDFS文件系统/hdfsapi/test目录下的所有文件信息 那么使用代码怎么写呢?直接先上代码:(这之后 ...

  8. 【HDFS API编程】副本系数深度剖析

    上一节我们使用Java API操作HDFS文件系统创建了文件a.txt并写入了hello hadoop(回顾:https://www.cnblogs.com/Liuyt-61/p/10739018.h ...

  9. 干货--安装eclipse-hadoop-plugin插件及HDFS API编程两个遇到的重要错误的解决

    在Windows的eclipse上写hdfs的API程序,都会遇到两个错误,在网上查了很多资料,都没有解决的办法,经过了很多时间的研究,终于把这个问题解决了 错误是 1.java.io.IOExcep ...

随机推荐

  1. 解决mysql大小写敏感问题

    先在服务中 找到 my.min 文件 在 [mysqld] 下面添加一行: lower_case_table_names = 0 其中 0:区分大小写,1:不区分大小写 设置好后 需要重启服务   然 ...

  2. linux关闭终端响铃

    title: linux关闭终端响铃 date: 2018-01-25 15:10:14 tags: linux categories: linux 在终端输入或是直接在.bashrc里添加一行 xs ...

  3. LeetCode #003# Longest Substring Without Repeating Characters(js描述)

    索引 思路1:分治策略 思路2:Brute Force - O(n^3) 思路3:动态规划? O(n^2)版,错解之一:420 ms O(n^2)版,错解之二:388 ms O(n)版,思路转变: 1 ...

  4. Mysql 集合链接查询

    MySQL NULL 值处理 需求:我们已经知道MySQL使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作. ...

  5. Java爬虫模拟登录——不给我毛概二的H某大学

    你的账号访问太频繁,请一分钟之后再试! 从大一开始 就用脚本在刷课 在专业课踢的只剩下一门C#的情况下 活活刷到一周的课 大二开始教务系统多了一个非常**的操作 退课池 and 访问频繁缓冲 难道,我 ...

  6. 手游折扣app排行榜前10名_2018哪个折扣app最低最好

    2018游戏圈白皮书发布,PC端游的份额继续下降,页游的比例也在下降,但手游的比例持续3年上升.以渠道为阵营,逐渐小的平台和公会被逐渐淘汰.流量集中在少数几个大的平台.但是这样带来的问题是,平台越来越 ...

  7. # NOI.AC省选赛 第五场T1 子集,与&最大值

    NOI.AC省选赛 第五场T1 A. Mas的童年 题目链接 http://noi.ac/problem/309 思路 0x00 \(n^2\)的暴力挺简单的. ans=max(ans,xor[j-1 ...

  8. Pandas 基础(7) - Group By 分组的相关知识

    首先, 引入这节需要的 csv 文件 (已上传) import pandas as pd city_df = pd.read_csv('/Users/rachel/Sites/pandas/py/pa ...

  9. 【六】jquery之HTML代码/文本/值[下拉列表框、多选框、单选框的选中]

    val()方法不仅能设置元素的值,同时也能获取元素的值.另外,val()方法还有另外一个用处,就是它能使select(下拉列表框).checkbox(多选框)和radio(单选框)相应的选项被选中,在 ...

  10. 微信小程序,加载更多

    html <!-- 头部 --> <view class='tab'> <view class="tab-new {{selected_new?'active' ...