cdh kerberosed hdfs java demo

cdh启用kerberos后,通过java直接操作hdfs需要额外的连接配置,具体代码如下:

System.setProperty("java.security.krb5.conf", "/Users/zhouxiang/develop/test/krb5.conf");
Class.forName("org.apache.hive.jdbc.HiveDriver").newInstance();
Configuration conf = new Configuration();
conf.set("hadoop.security.authentication", "kerberos");
conf.set("fs.defaultFS", "hdfs://test01:8020");
UserGroupInformation.setConfiguration(conf);
String principal = System.getProperty("kerberosPrincipal", "hdfs@TEST.COM");
String keytabLocation = System.getProperty("kerberosKeytab","/Users/zhouxiang/develop/test/hdfs.keytab"); UserGroupInformation.loginUserFromKeytab(principal, keytabLocation);
FileSystem fileSystem = FileSystem.get(conf); System.out.println("Connecting to -- "+conf.get("fs.defaultFS")); Path localPath = new Path("/Users/zhouxiang/Downloads/hadoop-common-2.6.0-cdh5.13.1.pom");
Path remotePath = new Path("/tmp/");
fileSystem.copyFromLocalFile(localPath, remotePath); FileStatus[] status = fileSystem.listStatus(remotePath); for(FileStatus stat: status) { System.out.println(stat.getPath()); }

发表评论

电子邮件地址不会被公开。 必填项已用*标注