设为首页 加入收藏

TOP

Hadoop API通过Kerberos认证读取HDFS(windows)
2019-02-12 12:37:11 】 浏览:35
Tags:Hadoop API 通过 Kerberos 认证 读取 HDFS windows
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/scgaliguodong123_/article/details/80541914

源数据

数据

代码

package com.lgd.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.SecurityUtil;
import org.apache.hadoop.security.UserGroupInformation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>Project: bigdata-frames</p>
 * <p>Package: com.lgd.hadoop.hdfs</p>
 * <p>Title: </p>
 * <p>Description: </p>
 *
 * @author guodong.li
 * @version 1.0.0
 * @date 2018/6/1
 */




public class HdfsKerberos {
    public static final String KEYTAB_FILE_KEY = "hdfs.keytab.file";
    public static final String USER_NAME_KEY = "hdfs.kerberos.principal";
    private static Configuration conf = new Configuration();


    public static void main(String[] args) throws IOException {
        System.setProperty("hadoop.home.dir", "D:\\hadoop-2.6.5");
        //设置系统的Kerberos配置信息
        System.setProperty("java.security.krb5.conf","D:/impala/krb5.conf");

        //keytab文件的路径
        conf.set(KEYTAB_FILE_KEY, "D:/impala/finance.keytab");
        //principal
        conf.set(USER_NAME_KEY, "finance");

        login(conf);


        System.out.println(loadHdfsFile("/user/hive/warehouse/ai_data.db/test_hive/part-m-00000"));
    }

    public static List<String> loadHdfsFile(String filePath){
        List<String> resultList = new ArrayList<>();
        FileSystem fileSystem = null;
        try {
            fileSystem = FileSystem.get(conf);
            FSDataInputStream fs = fileSystem.open(new Path(filePath));
            BufferedReader bis = new BufferedReader(new InputStreamReader(fs,"UTF-8"));
            String line;
            while ((line = bis.readLine()) != null) {
                resultList.add(line);
            }
            fileSystem.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultList;
    }

    public static void login(Configuration hdfsConfig) throws IOException {
        boolean securityEnabled = UserGroupInformation.isSecurityEnabled();
        if (securityEnabled) {
            String keytab = conf.get(KEYTAB_FILE_KEY);
            if (keytab != null) {
                hdfsConfig.set(KEYTAB_FILE_KEY, keytab);
            }
            String userName = conf.get(USER_NAME_KEY);
            if (userName != null) {
                hdfsConfig.set(USER_NAME_KEY, userName);
            }
            SecurityUtil.login(hdfsConfig, KEYTAB_FILE_KEY, USER_NAME_KEY);
        }
    }

结果:

访问


编程开发网
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇hadoop搭建时为什么最好重新编译.. 下一篇hadoop集群负载均衡以及配置方法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(214) }