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 ...
随机推荐
- 基于ArduinoLeonardo板子的BadUSB攻击实战
0X00 前言 在Freebuf上许多同学已经对HID攻击谈了自己的看法,如维克斯同学的<利用Arduino快速制作Teensy BadUSB>无论从科普还是实践都给我们详尽的描述了Bad ...
- “Cannot make a static reference to the non-static method”处理方法
报错原文:Cannot make a static reference to the non-static method maxArea(Shape[]) from the type ShapeTes ...
- python day05作业
- JAVA拼合数组方法
方法一: package org.ken.array; import java.lang.reflect.Array; import java.util.Arrays; public class Jo ...
- Spring Boot 揭秘与实战(七) 实用技术篇 - FreeMarker 模板引擎
文章目录 1. FreeMaker 代替 JSP 作为页面渲染 2. 生成静态文件 3. 扩展阅读 4. 源代码 Spring Boot 提供了很多模板引擎的支持,例如 FreeMarker.Thym ...
- redis实现api限流
redis官方给出了参考文档:INCR 这里参考第一种方法,使用token bucket实现:每个用户每秒有一个Counter: func RateLimiter(uid string, rlType ...
- 【Java】输出目录结构
import java.io.*; import java.io.File; import java.io.IOException; public class FileUtil { public st ...
- 1100C NN and the Optical Illusion
推公式,水题.cos()函数是默认弧度制的 #include <iostream> #include <cstring> #include <string> #in ...
- Learning by doing——小黄杉获得感想
突然想起来前一个月答应了栋哥要写一篇博客的,后来一直忙于复习就忘了. 不过答应了的事就要完成嘛. 获得感言 首先就是非常高兴的了,这也是对我的能力的一种肯定 这次的获得原因是期中考最快满分,emmm侧 ...
- linux 的IP配置和网络问题的排查
1.6 IP的配制, 首先要会用: ifconfig 和加相关参数如: ifconfig -a, 来查看,自己的电脑网络配制. 再次就必需要知道,默认IP配制文件的地方: cd /etc/sysc ...