Java读写HDFS文件
一、依赖包maven路径
- <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
- <dependency>
- <groupId>org.apache.hadoop</groupId>
- <artifactId>hadoop-client</artifactId>
- <version>2.7.3</version>
- <scope>runtime</scope>
- </dependency>
二、针对HDFS文件的操作类HDFSOperate
- package com.hdfs.util;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintStream;
- import java.net.URI;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FSDataInputStream;
- import org.apache.hadoop.fs.FSDataOutputStream;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IOUtils;
- /**
- * 针对HDFS文件的操作类
- */
- public class HDFSOperate {
- /**
- * 新增(创建)HDFS文件
- * @param hdfs
- */
- public void createHDFS(String hdfs){
- try {
- Configuration conf = new Configuration();
- conf.setBoolean("dfs.support.append", true);
- conf.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
- conf.set("dfs.client.block.write.replace-datanode-on-failure.enable","true");
- FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
- Path path = new Path(hdfs);
- //判断HDFS文件是否存在
- if(fs.exists(path)){
- //System.out.println(hdfs + "已经存在!!!");
- }else{
- FSDataOutputStream hdfsOutStream = fs.create(new Path(hdfs));
- hdfsOutStream.close();
- }
- fs.close();
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- /**
- * 在HDFS文件后面追加内容
- * @param hdfs
- * @param appendContent
- */
- public void appendHDFS(String hdfs,String appendContent){
- try {
- Configuration conf = new Configuration();
- conf.setBoolean("dfs.support.append", true);
- conf.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
- conf.set("dfs.client.block.write.replace-datanode-on-failure.enable","true");
- FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
- Path path = new Path(hdfs);
- //判断HDFS文件是否存在
- if(fs.exists(path)){
- //System.out.println(hdfs + "已经存在!!!");
- }else{
- FSDataOutputStream hdfsOutStream = fs.create(new Path(hdfs));
- hdfsOutStream.close();
- }
- FSDataOutputStream hdfsOutStream = fs.append(new Path(hdfs));
- byte [] str = appendContent.getBytes("UTF-8");//防止中文乱码
- hdfsOutStream.write(str);
- hdfsOutStream.close();
- fs.close();
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- /**
- * 修改HDFS文件内容 / 删除就是替换为空
- * @param hdfs : hdfs文件路径
- * @param sourceContent :要修改的hdfs文件内容
- * @param changeContent :需要修改成的文件内容
- */
- public void change(String hdfs,String sourceContent,String changeContent){
- try {
- Configuration conf = new Configuration();
- conf.setBoolean("dfs.support.append", true);
- conf.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
- conf.set("dfs.client.block.write.replace-datanode-on-failure.enable","true");
- FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
- Path path = new Path(hdfs);
- //判断HDFS文件是否存在
- if(fs.exists(path)){
- //System.out.println(hdfs + "已经存在!!!");
- FSDataInputStream in = fs.open(path);
- BufferedReader bf=new BufferedReader(new InputStreamReader(in));//防止中文乱码
- String totalString = "";
- String line = null;
- while ((line = bf.readLine()) != null) {
- totalString += line;
- }
- String changeString = totalString.replace(sourceContent, changeContent);
- FSDataOutputStream hdfsOutStream = fs.create(new Path(hdfs));
- byte [] str = changeString.getBytes("UTF-8");
- hdfsOutStream.write(str);
- hdfsOutStream.close();
- }else{
- //System.out.println(hdfs + "不存在,无需操作!!!");
- }
- fs.close();
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- /**
- * 判断要追加的内容是否存在
- * @param hdfs
- * @param appendContent
- * @return
- */
- public Boolean isContentExist(String hdfs,String appendContent){
- try {
- Configuration conf = new Configuration();
- conf.setBoolean("dfs.support.append", true);
- conf.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
- conf.set("dfs.client.block.write.replace-datanode-on-failure.enable","true");
- FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
- Path path = new Path(hdfs);
- //判断HDFS文件是否存在
- if(fs.exists(path)){
- //System.out.println(hdfs + "已经存在!!!");
- FSDataInputStream in = fs.open(path);
- BufferedReader bf=new BufferedReader(new InputStreamReader(in));//防止中文乱码
- String totalString = "";
- String line = null;
- while ((line = bf.readLine()) != null) {
- totalString += line;
- }
- if(totalString.contains(appendContent)){
- return true;
- }
- }else{
- //System.out.println(hdfs + "不存在,无需操作!!!");
- }
- fs.close();
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- return false;
- }
- public static void main(String[] args) throws IOException {
- String hdfs = "hdfs://192.168.168.200:9000/test/tes.txt";
- HDFSOperate hdfsOperate = new HDFSOperate();
- hdfsOperate.createHDFS(hdfs);
- hdfsOperate.appendHDFS(hdfs,"测试新增内容");
- //hdfsOperate.change(hdfs, "测试新增内容", "测试修改成功");
- }
- }
Java读写HDFS文件的更多相关文章
- Java读写hdfs上的avro文件
1.通过Java往hdfs写avro文件 import java.io.File; import java.io.IOException; import java.io.OutputStream; i ...
- Java读写资源文件类Properties
Java中读写资源文件最重要的类是Properties 1) 资源文件要求如下: 1.properties文件是一个文本文件 2.properties文件的语法有两种,一种是注释,一种属性配置. 注 ...
- Java读写txt文件
1.Java读取txt文件 1.1.使用FileInputStream: public static String readFile(File file, String charset){ //设置默 ...
- Java 读写XML文件 API--org.dom4j
om4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,同时它也是一个开放源代码的软件 ...
- C++读写EXCEL文件OLE,java读写excel文件POI 对比
C++读写EXCEL文件方式比较 有些朋友问代码的问题,将OLE读写的代码分享在这个地方,大家请自己看.http://www.cnblogs.com/destim/p/5476915.html C++ ...
- java读写excel文件( POI解析Excel)
package com.zhx.base.utils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi ...
- Java读写.properties文件实例,解决中文乱码问题
package com.lxk.propertyFileTest; import java.io.*; import java.util.Properties; /** * 读写properties文 ...
- 《Java知识应用》Java读写DBF文件
1. 准备: Jar包下载:链接: https://pan.baidu.com/s/1Ikxx-vkw5vSDf9SBUQHBCw 提取码: 7h58 复制这段内容后打开百度网盘手机App,操作更方便 ...
- java读写大文件
java读写2G以上的大文件(推荐使用以下方法) static String sourceFilePath = "H:\\DataSource-ready\\question.json&qu ...
随机推荐
- 判断input checkbox选中
$("#chexk").get(0).checked $("#chexk").is(':checked')
- vue国际化插件
1.安装 $ npm install vue-i18n 2.引入 import VueI18n from 'vue-i18n' Vue.use(VueI18n) const i18n = new Vu ...
- JAVA_全局配置文件(配置网址,url等等)_第一种方式
一.概述 当使用httpClient调其他系统接口时,需要通过地址来发送post请求. 这时我们有不同的环境,那么就有两个问题. 1是地址不能写在代码中,而是要写在配置文件. 2是不同环境配置文件应该 ...
- 2017第八届蓝桥杯C/C++ B组省赛-等差素数列
标题:等差素数列 2,3,5,7,11,13,....是素数序列. 类似:7,37,67,97,127,157 这样完全由素数组成的等差数列,叫等差素数数列. 上边的数列公差为30,长度为6. 200 ...
- JavaScript Dom基础-9-Dom查找方法; 设置DOM元素的样式; innerHTML属性的应用; className属性的应用; DOM元素上添加删除获取属性;
JavaScript Dom基础 学习目标 1.掌握基本的Dom查找方法 domcument.getElementById() Domcument.getElementBy TagName() 2.掌 ...
- [LeetCode&Python] Problem 492. Construct the Rectangle
For a web developer, it is very important to know how to design a web page's size. So, given a speci ...
- Java之从头开始编写简单课程信息管理系统
编写简单的课程管理系统对于新手并不友好,想要出色的完成并不容易以下是我的一些经验和方法 详情可参考以下链接: https://www.cnblogs.com/dream0-0/p/10090828.h ...
- deque中的细节问题,以及reverse和max_element的使用
deque中使用迭代器进行运算的函数都默认以.end()为结束,而实际上.end()所返回的是最后一个元素的下一个位置,所以进行数据比较时,应对其-1才是对应的最后一个数据,而函数中则是直接使用.en ...
- 2017.4.7 Sprng MVC工作流程描述图
图一: 图二: Spring工作流程描述 1. 用户向服务器发送请求,请求被Spring 前端控制Servelt DispatcherServlet捕获: 2. Dispa ...
- oracle ZHS16GBK的数据库导入到字符集为AL32UTF8的数据库(转载+自己经验总结)
字符集子集向其超集转换是可行的,如此例 ZHS16GBK转换为AL32UTF8. 导出使用的字符集将会记录在导出文件中,当文件导入时,将会检查导出时使用的字符集设置,如果这个字符集不同于导入客户端的N ...