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. ubuntu 用管理员身份进入系统

    我们需要以管理员的身份进入系统,才能在系统中畅通无阻     以管理员的身份进入系统,往下看: Ubuntu安装好后,root初始密码(默认密码)不知道,需要设置. 1.先用安装Ubuntu的时候创建 ...

  2. 修改DUILIB任务栏中显示的图标和EXE图标

    在资源中添加ICO图标,获取属性名,在主窗口文件中的函数InitWindow或OnCreate中添加如下代码: SetIcon(IDR_MAINFRAME); 修改EXE显示图标,在主窗口中加入如下代 ...

  3. 第1节 storm日志告警:1、 - 5、日志监控告警业务需求、代码、集群运行、总结

    如何解决短信或者邮件频繁发送的问题:每次发送的时候都先查询数据库记录,看一下有没有给这个人发送消息,上一次发送消息的时间是什么时候,如果发送时间间隔小于半个小时,就不要再发了 ============ ...

  4. keil 生成bin文件

     在 User 配置页面中,提供了三种类型的用户指令输入框,在不同组的框输入指令,可控制指令的执行时间,分别是编译前(Before Compile c/c++ file).构建前(Before Bui ...

  5. oracle随机生成时间

    Julian Date定义 Julian Date:有效的日期范围是从公元前4713年1月1日到公元9999年12月31日.儒略日(julian date)是自公元前4713年1月1日中午12时起经过 ...

  6. JSONObject.fromObject() 转string时,实体里面的时间错乱的问题

    在把要入库的数据实体转成JSON字符串发给kafka的时候,出现了问题,转换完以后,就变成这样子的了,真的是第一次见到这种,真的是长见识了 然后百度了一下:https://www.cnblogs.co ...

  7. (译)Calculus on Computational Graphs: Backpropagation

    Posted on August 31, 2015 Introduction Backpropagation is the key algorithm that makes training deep ...

  8. 2.13 阶段实战 使用layui重构选课系统

    一.说在前面   昨天  学习表单校验插件validate,并使用ajax 自定义校验规则   今天 使用layui重构选课系统 二.题目要求 1.项目需求: 本项目所开发的学生选课系统完成学校对学生 ...

  9. Linux下如何查找sqlnet.ora 和listener.ora 和tnsnames.ora 配置文件的目录

    1.首先切换到oracle 用户下 使用env 查看数据库配置文件信息 2.然后找到LD_LIBRARY_PATH=/home/opt/oracle/product/11.2.0.4/db_1 (配置 ...

  10. tools.lombok

    @Slf4j @Data @Accessors @Builder