Java 访问kerberos认证的HDFS文件

1、创建Maven项目,导入以下依赖,必须:

		<dependency>
		    <groupId>org.apache.hadoop</groupId>
		    <artifactId>hadoop-common</artifactId>
		    <version>2.6.0</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.hadoop</groupId>
		    <artifactId>hadoop-hdfs</artifactId>
		    <version>2.6.0</version>
		</dependency>

2、获取客户端配置文件hdfs-site.xml及core-site.xml



文件目录:


3、测试程序

    public static void test1(String user, String keytab, String dir) throws Exception {
        Configuration conf = new Configuration();
    	conf.addResource(new Path("D:/cdh_10/hdfs-site.xml"));
	conf.addResource(new Path("D:/cdh_10/core-site.xml"));
        System.setProperty("java.security.krb5.conf", "D:/cdh_10/krb5.conf");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab(user, keytab);
        listDir(conf, dir);
    }

    public static void listDir(Configuration conf, String dir) throws IOException {
        FileSystem fs = FileSystem.get(conf);
        FileStatus files[] = fs.listStatus(new Path(dir));
        for (FileStatus file : files) {
            System.out.println(file.getPath());
        }
    }
    
    public static void main(String[] args) {
    	String user = "xxx";
	String keytab = "D:/cdh_10/xxx.keytab";
	String dir = "hdfs://ns1/data";
	try {
		test1(user, keytab, dir);
	} catch (Exception e) {
		e.printStackTrace();
	}
    }

可能遇到的异常:

1、缺少kerberos配置文件设置时,如下

System.setProperty("java.security.krb5.conf", "D:/cdh_10/krb5.conf");

抛出异常:

java.lang.IllegalArgumentException: Can't get Kerberos realm
		at org.apache.hadoop.security.HadoopKerberosName.setConfiguration(HadoopKerberosName.java:65)
		at org.apache.hadoop.security.UserGroupInformation.initialize(UserGroupInformation.java:263)
		at org.apache.hadoop.security.UserGroupInformation.setConfiguration(UserGroupInformation.java:299)
		at com.xxx.hdfs.CdhHdfsOpt.test1(CdhHdfsOpt.java:122)
		at com.xxx.hdfs.CdhHdfsOpt.main(CdhHdfsOpt.java:141)
	Caused by: java.lang.reflect.InvocationTargetException
		at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
		at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.lang.reflect.Method.invoke(Method.java:497)
		at org.apache.hadoop.security.authentication.util.KerberosUtil.getDefaultRealm(KerberosUtil.java:84)
		at org.apache.hadoop.security.HadoopKerberosName.setConfiguration(HadoopKerberosName.java:63)
		... 4 more
	Caused by: KrbException: Cannot locate default realm
		at sun.security.krb5.Config.getDefaultRealm(Config.java:1029)
		... 10 more
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值