对HDFS进行文件操作的若干代码
来源:互联网 发布:买家淘宝退货率 编辑:程序博客网 时间:2024/06/05 09:31
import java.net.URI;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.BlockLocation;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;public class Optation { /** * HDFS 文件访问API */ public static String hdfsUrl="hdfs://localhost:9000"; /** * create HDFS folder 创建一个文件夹 * @param dirPath * @return */ public static void createDir(String dirPath) throws Exception{ Configuration conf = new Configuration(); FileSystem hdfs =FileSystem.get(URI.create(hdfsUrl),conf); Path path = new Path(dirPath); hdfs.mkdirs(path); hdfs.close(); } /** * delete HDFS folder 删除一个文件夹 * @param dirPath * @return */ public static void deleteDir(String dirPath) throws Exception{ Configuration conf = new Configuration(); FileSystem hdfs =FileSystem.get(URI.create(hdfsUrl),conf); Path path = new Path(dirPath); hdfs.delete(path); hdfs.close(); } /** * create a file 创建一个文件 * @param filePath * @return */ public static void createFile(String filePath) throws Exception{ Configuration conf = new Configuration(); FileSystem hdfs =FileSystem.get(URI.create(hdfsUrl),conf); Path path = new Path(filePath); FSDataOutputStream out =hdfs.create(path); out.close(); hdfs.close(); } /** * rename a file 重命名一个文件 * @param oldPath * @param newPath * @return */ public static void renameFile(String oldPath,String newPath) throws Exception{ Configuration conf = new Configuration(); FileSystem hdfs =FileSystem.get(URI.create(hdfsUrl),conf); Path path = new Path(oldPath); Path nPath = new Path(newPath); hdfs.rename(path, nPath); hdfs.close(); } /** * delete a file 删除一个文件 * @param hadoopFile * @return isDeleted */ public static boolean deleteFile(String hadoopFile) throws Exception{ Configuration conf = new Configuration(); FileSystem hdfs =FileSystem.get(URI.create(hdfsUrl),conf); Path path = new Path(hadoopFile); boolean isDeleted =hdfs.delete(path); hdfs.close(); return isDeleted; } /** * upload a local file 上传文件 * @param localPath * @param hadoopPath * @return */ public static void uploadLocalFile(String localPath,String hadoopPath) throws Exception{ Configuration conf = new Configuration(); FileSystem hdfs =FileSystem.get(URI.create(hdfsUrl),conf); Path src = new Path(localPath); Path dst =new Path(hadoopPath); hdfs.copyFromLocalFile(src, dst); hdfs.close(); } /** * 读取文件于字节缓冲数组 * @param hadoopFile * @return buffer */ public static byte[] readFile(String hadoopFile) throws Exception{ Configuration conf = new Configuration(); FileSystem hdfs =FileSystem.get(URI.create(hdfsUrl),conf); Path path = new Path(hadoopFile); if(hdfs.exists(path)){ FSDataInputStream in = hdfs.open(path); FileStatus stat = hdfs.getFileStatus(path); byte[] buffer = new byte[Integer.parseInt(String.valueOf(stat.getBlockSize()))]; in.readFully(0,buffer); in.close(); hdfs.close(); return buffer; } else{ throw new Exception("the file is not found."); } } /** * list files under folder 列出文件夹中所有文件 * @param hadoopPath * @return fileString */ public static String listFiles(String hadoopPath) throws Exception{ Configuration conf = new Configuration(); FileSystem hdfs =FileSystem.get(URI.create(hdfsUrl),conf); Path dst = new Path(hadoopPath); FileStatus[] files = hdfs.listStatus(dst); String fileString = ""; for(FileStatus file:files){ System.out.println(file.getPath().toString()); fileString +=file.getPath().toString(); } hdfs.close(); return fileString; } /** * list block info of file 查找文件所在的数据块 * @param hadoopPath * @return blockString */ public static String getBlockInfo(String hadoopPath) throws Exception{ Configuration conf = new Configuration(); FileSystem hdfs =FileSystem.get(URI.create(hdfsUrl),conf); Path dst = new Path(hadoopPath); FileStatus fileStatus = hdfs.getFileStatus(dst); BlockLocation[] blkloc =hdfs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen()); String blockString = ""; for(BlockLocation loc:blkloc){ for(int i=0;i<loc.getHosts().length;i++){ System.out.println(loc.getHosts()[i]); // blockString +=loc.getHosts()[i]+" "; } hdfs.close(); return blockString; } }}
2 0
- 对HDFS进行文件操作的若干代码
- 调用JAVA API对HDFS文件进行文件的读写、上传下载、删除等操作代码详解
- Hadoop - HDFS API 对文件进行操作
- eclipse通过hdfs提供的api对hdfs进行文件操作
- python实现对HDFS的文件操作
- 调用JAVA API 对 HDFS 进行文件的读取、写入、上传、下载、删除等操作
- Hadoop HDFS文件操作的Java代码
- Hadoop HDFS文件操作的Java代码
- Hadoop HDFS文件操作的Java代码
- HDFS的API对Hadoop文件系统的文件进行写入
- 用程序对hdfs进行操作。
- 对HDFS进行操作——笔记
- 利用hdfs的java api进行文件操作注意事项
- HDFS的文件操作
- 对HDFS文件系统文件的读、写、删操作
- 对HDFS文件系统文件的读、写、删操作
- 对HDFS文件系统文件的读、写、删操作 .
- hadoop之HDFS对文件的相关操作
- 最大波动-CCF往届题
- 几道链表的题目
- Mac - 查看端口占用
- Make interesting things with react!
- vim 宏学习 实现批量处理
- 对HDFS进行文件操作的若干代码
- 线程相关错误总结
- 【java】pta18周练习题
- 大神解答:如何实现域账号免登陆流程平台的功能
- 安卓自定义View-Canvas之图片文字
- LoadRunner函数中文翻译(全)
- matplotlib 坐标原点移到左上角
- Material Design系列-- BottomNavigation(翻译篇)
- 递归函数及其R实现