一、依赖包maven路径


  1. <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
  2. <dependency>
  3. <groupId>org.apache.hadoop</groupId>
  4. <artifactId>hadoop-client</artifactId>
  5. <version>2.7.3</version>
  6. <scope>runtime</scope>
  7. </dependency>

二、针对HDFS文件的操作类HDFSOperate


  1. package com.hdfs.util;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.PrintStream;
  8. import java.net.URI;
  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.FileSystem;
  13. import org.apache.hadoop.fs.Path;
  14. import org.apache.hadoop.io.IOUtils;
  15. /**
  16. * 针对HDFS文件的操作类
  17. */
  18. public class HDFSOperate {
  19. /**
  20. * 新增(创建)HDFS文件
  21. * @param hdfs
  22. */
  23. public void createHDFS(String hdfs){
  24. try {
  25. Configuration conf = new Configuration();
  26. conf.setBoolean("dfs.support.append", true);
  27. conf.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
  28. conf.set("dfs.client.block.write.replace-datanode-on-failure.enable","true");
  29. FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
  30. Path path = new Path(hdfs);
  31. //判断HDFS文件是否存在
  32. if(fs.exists(path)){
  33. //System.out.println(hdfs + "已经存在!!!");
  34. }else{
  35. FSDataOutputStream hdfsOutStream = fs.create(new Path(hdfs));
  36. hdfsOutStream.close();
  37. }
  38. fs.close();
  39. } catch (Exception e) {
  40. // TODO: handle exception
  41. e.printStackTrace();
  42. }
  43. }
  44. /**
  45. * 在HDFS文件后面追加内容
  46. * @param hdfs
  47. * @param appendContent
  48. */
  49. public void appendHDFS(String hdfs,String appendContent){
  50. try {
  51. Configuration conf = new Configuration();
  52. conf.setBoolean("dfs.support.append", true);
  53. conf.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
  54. conf.set("dfs.client.block.write.replace-datanode-on-failure.enable","true");
  55. FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
  56. Path path = new Path(hdfs);
  57. //判断HDFS文件是否存在
  58. if(fs.exists(path)){
  59. //System.out.println(hdfs + "已经存在!!!");
  60. }else{
  61. FSDataOutputStream hdfsOutStream = fs.create(new Path(hdfs));
  62. hdfsOutStream.close();
  63. }
  64. FSDataOutputStream hdfsOutStream = fs.append(new Path(hdfs));
  65. byte [] str = appendContent.getBytes("UTF-8");//防止中文乱码
  66. hdfsOutStream.write(str);
  67. hdfsOutStream.close();
  68. fs.close();
  69. } catch (Exception e) {
  70. // TODO: handle exception
  71. e.printStackTrace();
  72. }
  73. }
  74. /**
  75. * 修改HDFS文件内容 /  删除就是替换为空
  76. * @param hdfs : hdfs文件路径
  77. * @param sourceContent :要修改的hdfs文件内容
  78. * @param changeContent :需要修改成的文件内容
  79. */
  80. public void change(String hdfs,String sourceContent,String changeContent){
  81. try {
  82. Configuration conf = new Configuration();
  83. conf.setBoolean("dfs.support.append", true);
  84. conf.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
  85. conf.set("dfs.client.block.write.replace-datanode-on-failure.enable","true");
  86. FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
  87. Path path = new Path(hdfs);
  88. //判断HDFS文件是否存在
  89. if(fs.exists(path)){
  90. //System.out.println(hdfs + "已经存在!!!");
  91. FSDataInputStream in = fs.open(path);
  92. BufferedReader bf=new BufferedReader(new InputStreamReader(in));//防止中文乱码
  93. String totalString = "";
  94. String line = null;
  95. while ((line = bf.readLine()) != null) {
  96. totalString += line;
  97. }
  98. String changeString = totalString.replace(sourceContent, changeContent);
  99. FSDataOutputStream hdfsOutStream = fs.create(new Path(hdfs));
  100. byte [] str = changeString.getBytes("UTF-8");
  101. hdfsOutStream.write(str);
  102. hdfsOutStream.close();
  103. }else{
  104. //System.out.println(hdfs + "不存在,无需操作!!!");
  105. }
  106. fs.close();
  107. } catch (Exception e) {
  108. // TODO: handle exception
  109. e.printStackTrace();
  110. }
  111. }
  112. /**
  113. * 判断要追加的内容是否存在
  114. * @param hdfs
  115. * @param appendContent
  116. * @return
  117. */
  118. public Boolean isContentExist(String hdfs,String appendContent){
  119. try {
  120. Configuration conf = new Configuration();
  121. conf.setBoolean("dfs.support.append", true);
  122. conf.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
  123. conf.set("dfs.client.block.write.replace-datanode-on-failure.enable","true");
  124. FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
  125. Path path = new Path(hdfs);
  126. //判断HDFS文件是否存在
  127. if(fs.exists(path)){
  128. //System.out.println(hdfs + "已经存在!!!");
  129. FSDataInputStream in = fs.open(path);
  130. BufferedReader bf=new BufferedReader(new InputStreamReader(in));//防止中文乱码
  131. String totalString = "";
  132. String line = null;
  133. while ((line = bf.readLine()) != null) {
  134. totalString += line;
  135. }
  136. if(totalString.contains(appendContent)){
  137. return true;
  138. }
  139. }else{
  140. //System.out.println(hdfs + "不存在,无需操作!!!");
  141. }
  142. fs.close();
  143. } catch (Exception e) {
  144. // TODO: handle exception
  145. e.printStackTrace();
  146. }
  147. return false;
  148. }
  149. public static void main(String[] args) throws IOException {
  150. String hdfs = "hdfs://192.168.168.200:9000/test/tes.txt";
  151. HDFSOperate hdfsOperate = new HDFSOperate();
  152. hdfsOperate.createHDFS(hdfs);
  153. hdfsOperate.appendHDFS(hdfs,"测试新增内容");
  154. //hdfsOperate.change(hdfs, "测试新增内容", "测试修改成功");
  155. }
  156. }

Java读写HDFS文件的更多相关文章

  1. Java读写hdfs上的avro文件

    1.通过Java往hdfs写avro文件 import java.io.File; import java.io.IOException; import java.io.OutputStream; i ...

  2. Java读写资源文件类Properties

    Java中读写资源文件最重要的类是Properties 1) 资源文件要求如下: 1.properties文件是一个文本文件 2.properties文件的语法有两种,一种是注释,一种属性配置.  注 ...

  3. Java读写txt文件

    1.Java读取txt文件 1.1.使用FileInputStream: public static String readFile(File file, String charset){ //设置默 ...

  4. Java 读写XML文件 API--org.dom4j

    om4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,同时它也是一个开放源代码的软件 ...

  5. C++读写EXCEL文件OLE,java读写excel文件POI 对比

    C++读写EXCEL文件方式比较 有些朋友问代码的问题,将OLE读写的代码分享在这个地方,大家请自己看.http://www.cnblogs.com/destim/p/5476915.html C++ ...

  6. java读写excel文件( POI解析Excel)

    package com.zhx.base.utils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi ...

  7. Java读写.properties文件实例,解决中文乱码问题

    package com.lxk.propertyFileTest; import java.io.*; import java.util.Properties; /** * 读写properties文 ...

  8. 《Java知识应用》Java读写DBF文件

    1. 准备: Jar包下载:链接: https://pan.baidu.com/s/1Ikxx-vkw5vSDf9SBUQHBCw 提取码: 7h58 复制这段内容后打开百度网盘手机App,操作更方便 ...

  9. java读写大文件

    java读写2G以上的大文件(推荐使用以下方法) static String sourceFilePath = "H:\\DataSource-ready\\question.json&qu ...

随机推荐

  1. python day12 ——1.生成器2.生成器表达式 3.列表推导式

    一.生成器 什么是生成器. 生成器实质就是迭代器. 在python中有三种方式来获取生成器: 1. 通过生成器函数. 2. 通过各种推导式来实现生成器 . 3. 通过数据的转换也可以获取生成器. 1. ...

  2. python day 03作业答案

    1. (10) name='aleX leNb' print(name.split('l',1)) (13) name='aleX leNb' a=name.replace('a','A') prin ...

  3. HDU 6077 17多校4 Time To Get Up 水题

    Problem Description Little Q's clock is alarming! It's time to get up now! However, after reading th ...

  4. tomcat自动缓存的几种解决方式

    第一种方法:打开一个项目,这里我打开的Mail项目,然后点击Myeclipse菜单栏中的project-选择clean: 选择要clean的项目,确定即可不用进入tomcat服务器直接清理缓存. 上面 ...

  5. Deinstall卸载RAC之Oracle软件及数据库+GI集群软件

    Deinstall卸载Oracle软件及数据库+GI集群软件 1. 本篇文档应用场景: 需要安装新的ORACLE RAC产品,系统没有重装,需要对原环境中的RAC进行卸载: #本篇文档,在AIX 6. ...

  6. Android Studio安卓导出aar包与Unity 3D交互

    Unity与安卓aar 包交互 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...

  7. P1220 关路灯 (区间dp)

    题目链接:传送门 题目大意: 总共有N盏灯,老张从点C(1 ≤ C ≤ N)开始关灯(关灯不需要等待时间,C点的灯直接关掉),与此同时灯开始烧电(已知功率Pi). 老张每次可以往左走关最近的灯或者往右 ...

  8. css实现三栏布局,两边定宽,中间自适应

    1.利用定位实现 css代码如下: .box{overflow: hidden;height: 100px;margin: 10px 0;} .box>div{height: 100%;} #b ...

  9. python 闭包和迭代器

    一  函数名的运用:(函数名是一个变量,但它是一个特殊变量,与括号配合可以执行变量. (1) 函数名可以赋值给其他变量 def chi(): print("吃月饼") fn=chi ...

  10. linux下使用小票打印

    linux下使用小票打印 打印机: Xprinter XP-58IIH指令支持: ESC/POS接口: USB, 蓝牙 Linux系统: Centos7 蓝牙配对很快, 配对好后就是连接状态. 但很快 ...