• java操作HDFS相关demo(TDH,kerberos认证)


    public class Test {
        private static Configuration conf;
        private static FileSystem fs;
        //开启kerberos认证
        static {
            System.setProperty("java.security.krb5.conf", "D:\HDFS-test\krb5.conf");
            conf=new Configuration();
            conf.addResource(new Path("D:\HDFS-test\hdfs-site.xml"));
            conf.set("hadoop.security.authentication", "kerberos"); //配置认证方式
            conf.set("fs.default.name", "hdfs://172.20.237.112:8020");//namenode的地址和端口
            UserGroupInformation.setConfiguration(conf);
            try {
                UserGroupInformation.loginUserFromKeytab("hdfs/gz237-112", "D:\HDFS-test\hdfs.keytab");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    /**
    * 未开启安全
    */
    // static {
    //      conf = new Configuration();
    //      ip为Namenode master所在节点
    //      conf.set("fs.default.name", "hdfs://172.20.237.112:8020");
    //       FileSystem类在hadoop-hdfs包中
    //       conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
    // }
     
    public static void main(String[] args) throws Exception {
            // mkdirCatalog("/yfb");//在hdfs上创建目录
            putfile("D:\test.txt", "/yfb");//上传文件
            // createFile("/data.txt");//在hdfs上创建文件
            // deleteFile("/data.txt");//删除HDFS上的文件
            // downloadFile("/dddd1111.txt","D:\");//文件下载
    }
    //1、创建目录
    public static void mkdirCatalog(String path) throws Exception{
        //创建连接,使用开源的进行,连接报错
     
    Caused by: java.net.ConnectException: Connection refused: no further information
        at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
        at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
        at org.apache.hadoop.net.SocketIOWithTimeout.connect(SocketIOWithTimeout.java:206)

        // FileSystem fs = FileSystem.get(URI.create("hdfs://172.20.237.112:9000"),conf);

        //TDH的方式
        fs = FileSystem.get(conf);
        fs.mkdirs(new Path(path));
    }
    //上传文件到hdfs
    public static void putfile(String localfile,String hdfsfile) throws IOException {
        fs = FileSystem.get(conf);
        fs.copyFromLocalFile(new Path(localfile),new Path(hdfsfile));
    }
    //HDFS上创建文件
    public static void createFile(String path) throws Exception {
        fs = FileSystem.get(conf);
        fs.createNewFile(new Path(path));
    }
    //删除HDFS上的文件
    public static void deleteFile(String path) throws Exception {
        fs = FileSystem.get(conf);
        if(fs.exists(new Path(path))){
            fs.delete(new Path(path),true);
        }else {
            System.out.println("您所删除的文件不存在!");
        }
    }
    //下载文件到本地
    public static void downloadFile(String hdfsPath,String localPath) throws Exception {
        fs = FileSystem.get(conf);
        fs.copyToLocalFile(new Path(hdfsPath),new Path(localPath));
        }
    }
  • 相关阅读:
    python数字
    python字符串方法
    python操作符与流程控制
    网络基础和python(二)
    网络基础和python
    Ubuntu源更新
    make和makefile介绍
    JavaScript
    redis mac安装配置
    网络编程之socket(TCP,UDP)
  • 原文地址:https://www.cnblogs.com/yfb918/p/10483774.html
Copyright © 2020-2023  润新知