• HDFS编程


    HDFS编程主要API

    Hadoop类 功能
    org.apache.hadoop.fs.FileSystem 一个通用文件系统的抽象基类,可以被分布式文件系统继承。所有的可能使用Hadoop文件系统的代码都要使用到这个类。
    org.apache.hadoop.fs.FileStatus 客户端可见的文件状态信息。
    org.apache.hadoop.fs.FSDataInputStream 文件输入流,用于读取Hadoop文件。
    org.apache.hadoop.fs.FSDataOutputStream 文件输出流,用于写Hadoop文件。
    org.apache.hadoop.fs.permission.FsPermission 文件或者目录的权限
    org.apache.hadoop.conf.Configuration 访问配置项。所有的配置项的值,如果没有专门配置,以core-default.xml为准;否则,以core-site.xml中的配置为准。

    对于Hadoop文件系统中的文件的访问是基于 InputStream 和 OutputStream 的流式访问

    import java.io.IOException;
    import java.net.URI;
    
    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;
    
    public class Operation {
    
        private static Configuration conf = new Configuration();
        
        public static void putMerge(String inputDir, String hdfsFile) throws IOException{
            FileSystem hdfs = FileSystem.get(conf);
            FileSystem local = FileSystem.getLocal(conf);
            Path inputPath = new Path(inputDir);
            Path hdfsPath = new Path(hdfsFile);
            FileStatus[] inputFiles = local.listStatus(inputPath);
            FSDataOutputStream out = hdfs.create(hdfsPath);
            System.out.println("inputFiles length -> " + inputFiles.length);
            for(FileStatus inputFile:inputFiles){
                System.out.println(inputFile.getPath().getName());
                FSDataInputStream in = local.open(inputFile.getPath());
                byte[] buffer = new byte[256];
                int read = -1;
                while((read = in.read(buffer))>0){
                    out.write(buffer,0,read);
                }
                in.close();
            }
        }
    
        
        public static void list(String hdfs) throws IOException{
            FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
            FileStatus fileList[] = fs.listStatus(new Path(hdfs));
            int FileNum = fileList.length;
            for(int fileCount = 0; fileCount < FileNum; fileCount++){
                System.out.println(fileList[fileCount].getPath().getName() + " : " + fileList[fileCount].getLen());
            }
        }
        
        public static void delete(String hdfs) throws IOException{
            FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
            fs.deleteOnExit(new Path(hdfs));
        }
        
        public static void main(String[] args) throws IOException {
            putMerge("/root/test", "hdfs://localhost:9000/user/root/test");
            list("hdfs://localhost:9000/user/root");
            delete( "hdfs://localhost:9000/user/root/test");
        }
        
    }
  • 相关阅读:
    开机自动挂载分区
    Wine安装
    ubuntu 将idea/vscode快捷方式加入到启动器中
    在Linux上安装Java
    httpclient
    shiro
    redis-随笔
    maven
    spring的aop
    spring事务知识梳理
  • 原文地址:https://www.cnblogs.com/xuekyo/p/3394483.html
Copyright © 2020-2023  润新知