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 ...
随机推荐
- django做redis缓存
django中应用redis:pip3 install django-redis - 配置 CACHES = { "default": { "BACKEND": ...
- 2019-03-28-day021-抽象类与接口类
今日内容 type和class 继承 抽象类 接口类 多态 java 鸭子类型 pickle模块 collections.namedtuple type和class ##type ##class pr ...
- Primise --(mongoose's default promise library)
今天在学nodejs的时候,遇到一个错误;刚开始完全不知道说的是什么,为什么会出现这个错误 DeprecationWarning: Mongoose: mpromise (mongoose's def ...
- ss-libev控制脚本
适用于:shadowsocks-libev-3.0.3 操作系统:CentOS6.8 #!/bin/sh SHADOWSOCKS_SERVER="/usr/local/shadowsocks ...
- 【机器学习基础】SVM实现分类识别及参数调优(二)
前言 实现分类可以使用SVM方法,但是需要人工调参,具体过程请参考here,这个比较麻烦,小鹅不喜欢麻烦,正好看到SVM可以自动调优,甚好! 注意 1.reshape的使用: https://docs ...
- Java中的容器 I————浅谈List
一.List接口的继承关系 List接口是Collection接口的子接口,而ArrayList和LinkedList以及Vector是其实现类. List的特点是可以将元素维护在特定的序列中,可以再 ...
- 12.double的int方
给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 不要用Math.pow(double,double)哟.效率极其低下,比连成慢好多: 题 ...
- 卸载Mariadb-报错
1. rpm -qa|grep aria MariaDB-client-10.1.22-1.x86_64MariaDB-devel-10.1.22-1.x86_64MariaDB-shared-10. ...
- upstream timed out (110: Connection timed out) while reading response header from upstream
Nginx报错日志有如下内容: upstream timed out (110: Connection timed out) while reading response header from up ...
- HDU5952 Counting Cliques (暴力深搜+剪枝) (2016ACM/ICPC亚洲赛区沈阳站 Problem E)
题目链接:传送门 题目: Counting Cliques Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total S ...