package dada;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader; 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; public class HDFSToll { //路径是否存在
public static boolean testExist(Configuration conf,String path) throws IOException
{
FileSystem fs=FileSystem.get(conf);
return fs.exists(new Path(path));
}
//创建目录
public static boolean mkdir (Configuration conf ,String remoteDir)throws IOException
{
FileSystem fs=FileSystem.get(conf);
Path dirPath=new Path(remoteDir);
boolean result=fs.mkdirs(dirPath);
fs.close();
return result;
}
/**
* 删除目录
*/
public static boolean rmDir(Configuration conf, String remoteDir) throws IOException {
FileSystem fs = FileSystem.get(conf);
Path dirPath = new Path(remoteDir);
/* 第二个参数表示是否递归删除所有文件 */
boolean result = fs.delete(dirPath, true);
fs.close();
return result;
}
//创建文件
public static void touch(Configuration conf,String remoteFilePath )throws IOException
{
FileSystem fs=FileSystem.get(conf);
Path remotePath=new Path(remoteFilePath);
FSDataOutputStream outputStream =fs.create(remotePath);
outputStream.close();
fs.close();
}
//删除文件
public static boolean rm(Configuration conf,String remoteFilePath)throws IOException
{
FileSystem fs=FileSystem.get(conf);
Path remotePath=new Path(remoteFilePath);
boolean result=fs.delete(remotePath,false);
fs.close();
return result;
}
//追加文件内容 到末尾
public static void appendContentToFile(Configuration conf,String content,String remoteFilePath)throws IOException
{
FileSystem fs=FileSystem.get(conf);
Path remotePath=new Path(remoteFilePath);
FSDataOutputStream out=fs.append(remotePath);
out.write(content.getBytes());
out.close();
fs.close();
} //追加文件内容到开头
public static void appendContentToFile1(Configuration conf,String content,String remoteFilePath)throws IOException
{
String localTmpPath = "/usr/local/hadoop/enen.txt"; // 移动到本地
moveToLocalFile(conf, remoteFilePath, localTmpPath);
// 创建一个新文件
touch(conf, remoteFilePath);
// 先写入新内容
appendContentToFile(conf, content, remoteFilePath);
// 再写入原来内容
appendContentToFile(conf, localTmpPath, remoteFilePath); System.out.println("已追加内容到文件开头: " + remoteFilePath);
}
/** * 复制文件到指定路径 * 若路径已存在,则进行覆盖 */ public static void copyFromLocalFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException { FileSystem fs = FileSystem.get(conf); Path localPath = new Path(localFilePath); Path remotePath = new Path(remoteFilePath); /* fs.copyFromLocalFile 第一个参数表示是否删除源文件,第二个参数表示是否覆盖 */ fs.copyFromLocalFile(false, true, localPath, remotePath); fs.close(); } //将文件1写入文件2
public static void appendFile1ToFile2(Configuration conf,String remoteFilePath,String remoteFilePath2)throws IOException
{
FileSystem fs=FileSystem.get(conf);
Path file=new Path(remoteFilePath);
FSDataInputStream getIt=fs.open(file);
BufferedReader d=new BufferedReader(new InputStreamReader(getIt));
String content1=d.readLine();
Path remotePath=new Path(remoteFilePath2);
FSDataOutputStream out=fs.append(remotePath);
out.write(content1.getBytes());
d.close();
out.close();
fs.close();
}
/** * 追加文件内容 */
public static void appendToFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException { FileSystem fs = FileSystem.get(conf); Path remotePath = new Path(remoteFilePath); /* 创建一个文件读入流 */ FileInputStream in = new FileInputStream(localFilePath); /* 创建一个文件输出流,输出的内容将追加到文件末尾 */ FSDataOutputStream out = fs.append(remotePath); /* 读写文件内容 */ byte[] data = new byte[1024]; int read = -1; while ( (read = in.read(data)) > 0 ) { out.write(data, 0, read); } out.close(); in.close(); fs.close(); }
/** * 下载文件到本地 * 判断本地路径是否已存在,若已存在,则自动进行重命名 */ public static void copyToLocal(Configuration conf, String remoteFilePath, String localFilePath) throws IOException { FileSystem fs = FileSystem.get(conf); Path remotePath = new Path(remoteFilePath); File f = new File(localFilePath); /* 如果文件名存在,自动重命名(在文件名后面加上 _0, _1 ...) */ if (f.exists()) { System.out.println(localFilePath + " 文件已存在."); Integer i = 0; while (true) { f = new File(localFilePath + "_" + i.toString()); if (!f.exists()) { localFilePath = localFilePath + "_" + i.toString(); System.out.println("将重新命名为: " + localFilePath); break;//重命名文件 } i++; } // System.out.println("将重新命名为: " + localFilePath); } else System.out.println(localFilePath + " 文件不存在."); // 下载文件到本地 Path localPath = new Path(localFilePath); fs.copyToLocalFile(remotePath, localPath); fs.close(); }
/**
* 移动文件到本地 * 移动后,删除源文件 */
public static void moveToLocalFile(Configuration conf, String remoteFilePath, String localFilePath) throws IOException { FileSystem fs = FileSystem.get(conf); Path remotePath = new Path(remoteFilePath); Path localPath = new Path(localFilePath); fs.moveToLocalFile(remotePath, localPath); }
}

hadoop的文件操作整理java的更多相关文章

  1. Hadoop HelloWord Examples -对Hadoop FileSystem进行操作 - 基于Java

    我之前对hadoop的各种文件操作都是基于命令行的,但是进阶后,经常需要直接从java的代码中对HDFS进行修改.今天来练习下. 一个简单的demo,将hdfs的一个文件的内容拷贝到另外hdfs一个文 ...

  2. c文件操作整理

    <c陷阱与缺陷> FILE *fp; fp = fopen(file, "r+"); 编程者也许认为,程序一旦执行上述操作完毕,就可以自由地进行读取和写入的操作了.遗憾 ...

  3. Golang文件操作整理

    基本操作 文件创建 创建文件的时候,一定要注意权限问题,一般默认的文件权限是 0666 关于权限的相关内容,具体可以参考鸟叔p141 这里还是再回顾下,文件属性 r w x r w x r w x,第 ...

  4. PHP文件操作整理

    三种目录表示: ./     代表当前目录 ../    代表父级目录 /   代表根目录 常用的文件操作函数有 通用读写:                  fpen()  fwrite() fre ...

  5. Python-字典、集合、字符编码、文件操作整理-Day3

    1.字典 1.1.为什么有字典: 有个需求,存所有人的信息 这时候列表就不能轻易的表示完全names = ['stone','liang'] 1.2.元组: 定义符号()t = (1,2,3)tupl ...

  6. 文件操作(Java)

    学习内容:文件操作        1.输入流:InputStream类是字节输入流的抽象类,常用的一些方法有: raed()方法:从输入流中读取数据的下一个字节 reset()方法:将输入指针返回到当 ...

  7. Hadoop HDFS文件操作

    1.创建目录 import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.ha ...

  8. hadoop 一些文件操作

    在HDFS上面,FileSystem创建目录 复制本地文件到HDFS 获取集群中的节点

  9. 文件操作 -- 生成java文件

    import hashlibimport os def genJavaFile(packageName, soFile):    className, suffix = soFile.split('. ...

随机推荐

  1. java程序中的经常出现的的异常处理课后总结

    一.JDK中常见的异常情况 1.常见异常总结图 2.java中异常分类 Throwable类有两个直接子类: (1)Exception:出现的问题是可以被捕获的 (2)Error:系统错误,通常由JV ...

  2. Prometheus简介【转】

    Prometheus简介 Prometheus受启发于Google的Brogmon监控系统(相似的Kubernetes是从Google的Brog系统演变而来),从2012年开始由前Google工程师在 ...

  3. tomcat安装apr报错解决

    参考http://www.cnblogs.com/nuccch/p/7598361.html 1.no c complie 安装gcc解决 2.rm: cannot remove `libtoolT' ...

  4. php 实现店铺装修1

    一.原型分析 1.店铺未装修的情况下,使用默认样式,哪个是默认样式由后台告知: 2.所有的样式由后台进行维护(但后台始终有一个默认样式,不可删除不可编辑),所有样式,只要用户未编辑过,则默认按照商品的 ...

  5. python读取csv转换为dataframe

    前言: 由于在处理结构性数据的时候经常会读取本地形如:.xls.xlsx.csv等的数据.所以今天就花了点时间来总结一下利用python读取csv数据并且转换为dataframe的数据框架.话不多说, ...

  6. PV 动态供给【转】

    前面的例子中,我们提前创建了 PV,然后通过 PVC 申请 PV 并在 Pod 中使用,这种方式叫做静态供给(Static Provision). 与之对应的是动态供给(Dynamical Provi ...

  7. eclipse生成mybatis的逆向工程-mybatis代码自动生成

    首先,工作中一直在使用命令方式的mybatis的代码自动生成,今天把自己的笔记本直接搞一个在eclipse中生成的逆向代码生成工程,方便自己在家学习使用,在搞这个工程的过程中由于自己搞了一套环境,所i ...

  8. web页面性能优化

    web前端页面性能优化 网站的划分一般为二:前端和后台.我们可以理解成后台是用来实现网站的功能的,比如:实现用户注册,用户能够为文章发表评论等等.而前端呢? 其实应该是属于功能的表现.并且影响用户访问 ...

  9. jenkins#安装gitlab

    通过docker安装gitlab 参考 ------------------------------ 拉docker 镜像: docker pull gitlab/gitlab-ce 创建目录存储gi ...

  10. lucene实践 - 索引维护、多域查询、高亮显示

    之前的博客搜索栏用的是 sql 模糊查询进行查找,最近学完lucene,要学以致用啊,就把sql搜索给替换下来吧 中间遇到一些问题,也是学过程中没有提到的,所以说,还是实践出真知啊. lucene分开 ...