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));
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM