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. DeepLearning算法文章

    算法源码: learn_dl : https://github.com/hanbt/learn_dl rnn-from-scratch : https://github.com/pangolulu/r ...

  2. 新闻网大数据实时分析可视化系统项目——12、Hive与HBase集成进行数据分析

    (一)Hive 概述 (二)Hive在Hadoop生态圈中的位置 (三)Hive 架构设计 (四)Hive 的优点及应用场景 (五)Hive 的下载和安装部署 1.Hive 下载 Apache版本的H ...

  3. Unity ShaderLab 学习笔记(一)

    因为项目的问题,有个效果在iOS上面无法实现出来- 因为shader用的HardSurface的,在android上面跑起来没有问题- 以为在iOS上也不会有问题,但是悲剧啊,技能效果一片漆黑- 而且 ...

  4. HTML<a>标签作为锚链接

    1. 什么是锚链接? <a></a>标签主要作为超链接和锚链接使用.超链接主要指不同html页面之间的跳转,而锚链接指的是同一页面之间的跳转. 锚链接的两种效果: 在当前页面中 ...

  5. Redar Chart_Study

    1.Select a theme 2.Experiment with visual customization 3.Edit groups and categories 4.Creat a scrip ...

  6. P1061 判断题

    P1061 判断题 转跳点:

  7. Rolling Update【转】

    滚动更新是一次只更新一小部分副本,成功后,再更新更多的副本,最终完成所有副本的更新.滚动更新的最大的好处是零停机,整个更新过程始终有副本在运行,从而保证了业务的连续性. 下面我们部署三副本应用,初始镜 ...

  8. Codeforces 591 B:Rebranding

    B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. 主机ping虚拟机失败。虚拟机ping主机,可以ping通。

    原文:https://blog.csdn.net/ww1473345713/article/details/51490525 今天打开虚拟机,然后用Xshell远程连接,发现连接不上.按照以下顺序检查 ...

  10. 使用JavaScript实现一个简单的编译器

    在前端开发中也会或多或少接触到一些与编译相关的内容,常见的有 将ES6.7代码编译成ES5的代码 将SCSS.LESS代码转换成浏览器支持的CSS代码 通过uglifyjs.uglifycss等工具压 ...