当前位置: 首页 > news >正文

HDFS 文件操作

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

package com.dj.hadoop;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URI;


import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.util.Progressable;


/**
 * @description hdfs文件操作测试
 * @author dj
 * @date 2012-10-10
 */
public class FileHandlerTest {


public static String basePath = "hdfs://192.168.10.166:54310";
public static String fileRootPath = basePath
+ "/home/hadoop/data/hadoopdata";


public static void uploadLocalFileToHDFS(File localFile, Path destPath)
throws Exception {
Configuration config = new Configuration();
FileSystem.setDefaultUri(config, new URI(basePath));
FileSystem hdfs = FileSystem.get(config);
// Path dst = new Path(fileRootPath,destPath);
// hdfs.copyFromLocalFile(src, dst);
FSDataOutputStream out = hdfs.create(destPath,new Progressable() {
@Override
public void progress() {
    System.out.println("文件进度");  
}
});
   InputStream in = new BufferedInputStream(new FileInputStream(localFile));  
IOUtils.copy(in, out);
hdfs.close();
}

/**
* 文件下载
* @param destPath
* @param localDir
* @throws Exception 
*/
public static void downloadFileFromHDFS(Path destPath,File localDir) throws Exception {
Configuration config = new Configuration();
FileSystem.setDefaultUri(config, new URI(basePath));
FileSystem hdfs = FileSystem.get(config);
if (hdfs.exists(destPath)) {
FSDataInputStream in = hdfs.open(destPath);
FileStatus stat = hdfs.getFileStatus(destPath);
byte[] buffer = new byte[Integer.parseInt(String.valueOf(stat.getLen()))];
in.readFully(0, buffer);
in.close();
hdfs.close();

IOUtils.write(buffer, new FileOutputStream(localDir+"/"+destPath.getName()));
}
}


/**
* 删除文件
* @param destPath
* @throws Exception
*/
public static boolean deleteFile(Path destPath)throws Exception {
Configuration config = new Configuration();
FileSystem.setDefaultUri(config, new URI(basePath));
FileSystem hdfs = FileSystem.get(config);
if (hdfs.exists(destPath)) {
return hdfs.delete(destPath,true);
}
return false;
}

public static void listAll(String dir) throws Exception {
Configuration config = new Configuration();
FileSystem.setDefaultUri(config, new URI(basePath));
FileSystem fs = FileSystem.get(config);
FileStatus[] stats = fs.listStatus(new Path(basePath,dir));
for (int i = 0; stats != null && i < stats.length; ++i) {
// System.out.println(ToStringBuilder.reflectionToString(stats[i]));
if (!stats[i].isDir()) {
// regular file
System.out.println("文件:"+stats[i].getPath().toString()+"===="+stats[i].getGroup());

} else if (stats[i].isDir()) {
// dir
System.out.println("文件夹:"+stats[i].getPath().toString()+"===="+stats[i].getGroup());
}  
}
fs.close();
}


public static void createDirectory(String directoryName) throws Exception {
Configuration config = new Configuration();
FileSystem.setDefaultUri(config, new URI(basePath));
FileSystem fs = FileSystem.get(config);
System.out.println(ToStringBuilder.reflectionToString(fs));
fs.mkdirs(new Path(fileRootPath, directoryName));
fs.close();
}


public static void deleteDirectory(String directoryName) throws Exception {
Configuration config = new Configuration();
FileSystem.setDefaultUri(config, new URI(basePath));
FileSystem fs = FileSystem.get(config);
fs.delete(new Path(fileRootPath, directoryName),true);
fs.close();
}


public static void main(String[] args) throws Exception {
String directoryName = "books";
// 【1】创建文件夹
createDirectory(directoryName);
// 【2】 删除文件夹
// deleteDirectory(directoryName);
//【3】 显示文件
//  listAll("/home/hadoop/data/hadoopdata/");
//【4】 文件上传
File file = new File("G:/apiqianyi/ap2last_tmp.txt");
Path destPath = new Path(fileRootPath,directoryName+"/"+file.getName());

// uploadLocalFileToHDFS(file, destPath);
//【5】文件下载
// downloadFileFromHDFS(destPath, new File("G:/"));
//【6】删除文件
boolean flag = deleteFile(destPath);
System.out.println(flag);


}


}

转载于:https://my.oschina.net/zuiwoxing/blog/83447

相关文章:

  • spring boot 发布成包所需插件
  • java学习笔记1017---多线程
  • [Phoenix] 七、如何使用自增ID
  • STM32L071CBTX操作ECC508
  • java中三种主流数据库数据库(sqlserver,db2,oracle)的jdbc连接总结
  • Java NIO(四)Buffer
  • 手机的网络模式
  • Java NIO(十) ServerSocketChannel
  • Cisco路由器进行ip限速
  • Android Runtime Stats
  • pair work结束
  • springboot定制启动标志
  • Windows2000/XP启动过程详解
  • slf4j-logback 日志以json格式导入ELK
  • jquery 笔记。。。——》摘自武方博
  • Druid 在有赞的实践
  • ECS应用管理最佳实践
  • es的写入过程
  • ES学习笔记(10)--ES6中的函数和数组补漏
  • Java 最常见的 200+ 面试题:面试必备
  • JavaScript设计模式之工厂模式
  • java架构面试锦集:开源框架+并发+数据结构+大企必备面试题
  • Laravel Telescope:优雅的应用调试工具
  • React+TypeScript入门
  • Vue组件定义
  • 浮现式设计
  • 为什么要用IPython/Jupyter?
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  •  一套莫尔斯电报听写、翻译系统
  • ​io --- 处理流的核心工具​
  • #### go map 底层结构 ####
  • #pragma once
  • #在线报价接单​再坚持一下 明天是真的周六.出现货 实单来谈
  • $ is not function   和JQUERY 命名 冲突的解说 Jquer问题 (
  • (11)MSP430F5529 定时器B
  • (2022 CVPR) Unbiased Teacher v2
  • (python)数据结构---字典
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (三)uboot源码分析
  • (十三)Flask之特殊装饰器详解
  • (算法设计与分析)第一章算法概述-习题
  • (一)基于IDEA的JAVA基础10
  • (转)【Hibernate总结系列】使用举例
  • (转)c++ std::pair 与 std::make
  • (转)winform之ListView
  • (转)程序员疫苗:代码注入
  • (轉貼) 蒼井そら挑戰筋肉擂台 (Misc)
  • (轉貼) 寄發紅帖基本原則(教育部禮儀司頒布) (雜項)
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .mkp勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .Net程序猿乐Android发展---(10)框架布局FrameLayout
  • .NET的数据绑定
  • @Transactional 详解
  • []指针
  • [2]十道算法题【Java实现】