1 pom.xml
<dependencies>
<!-- 这里的dependency可以只引用 `hadoop-client`,或者同时引用`hadoop-common`和`hadoop-hdfs` -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
2 配置文件
将core-site.xml 和 hdfs-site.xml 加入到resources 下 或者直接在配置中指定
3 demo
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.UserGroupInformation;
public class HdfsSimple {
. public static FileSystem getFS(String kerberosUser, String keytabPath) {
FileSystem fileSystem = null;
Configuration conf = new Configuration();
//conf.set("fs.hdfs.impl", org.apache.hadoop.hdfsDistributedFileSystem.class.getName());
//设置krb5.conf
if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
System.setProperty("java.security.krb5.conf", "C:/Windows/krbconf/bms/krb5.conf");
} else {
// linux系统可不设,其会自动去寻找 /etc/krb5.conf
System.setProperty("java.security.krb5.conf","/etc/krb5.conf");
}
//kerberos认证
conf.set("hadoop.security.authentication", "kerberos");
//设置HDFS的principal
conf.set("dfs.namenode.kerberos.principal.pattern",hdfs/*@BDBIZVIZ);
UserGroupInformation.setConfiguration(conf);
try {
UserGroupInformation.loginUserFromKeytab(kerberosUser, keytabPath);
} catch (Exception e) {
System.out.println("身份认证异常: " + e.getMessage());
e.printStackTrace();
}
//第一种方式不加xml文件则如下配置
/* conf.set("fs.defaultFS", "hdfs://slyang");
conf.set("dfs.nameservices", "slyang");
conf.set("dfs.ha.namenodes.slyang", "nn1,nn2");
conf.set("dfs.namenode.rpc-address.slyang.nn1", "192.168.190.11:8020");
conf.set("dfs.namenode.rpc-address.slyang.nn2", "192.168.190.12:8020");
conf.set("dfs.client.failover.proxy.provider.slyang","org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");*/
//第二种方式在src文件夹下添加core-site.xml和hdfs-site.xml
fileSystem = FileSystem.get(conf);
return fileSystem;
}
public static void main(String[] args) throws Exception {
HdfsSimple hdfsSimple = new HdfsSimple();
String kerberos_User = "slyang7/hdp39@SLYANG7.COM";
String keytabPath = "/slyang7/slyang7.keytab";
String URI = "hdfs://server39:8020";
FileSystem fs = hdfsSimple.getFS(user, keytabPath);
RemoteIterator<locatedfilestatus> iter = fs.listFiles(new Path("/slyang7/test"), true);
while (iter.hasNext()) {
LocatedFileStatus status = iter.next();
System.out.println(status.getPath().toUri().getPath());
}
fs.close();
}
}