HDFS初始化
功能简介
在使用HDFS提供的API之前,需要先进行HDFS初始化操作。过程为:
- 加载HDFS服务配置文件,并进行kerberos安全认证。
- 认证通过后,实例化Filesystem。
- 使用HDFS的API。
说明:
此处kerberos安全认证需要使用到的keytab文件,请提前准备。
配置文件介绍
登录HDFS时会使用到如表1所示的配置文件。这些文件均已导入到“hdfs-example-security”工程的“conf”目录。
表1 配置文件
文件名称 | 作用 | 获取地址 |
---|---|---|
core-site.xml | 配置HDFS详细参数。 | FusionInsight_V100R002C60U10_Services_ClientConfig\HDFS\config\core-site.xml |
hdfs-site.xml | 配置HDFS详细参数。 | FusionInsight_V100R002C60U10_Services_ClientConfig\HDFS\config\hdfs-site.xml |
smallfs-site.xml | 配置SmallFS详细参数。 | FusionInsight_V100R002C60U10_Services_ClientConfig\SmallFS\config\smallfs-site.xml |
user.keytab | 对于Kerberos安全认证提供HDFS用户信息。 | 您可以联系管理员获取相应帐号对应权限的keytab文件和krb5文件。 |
krb5.conf | Kerberos server配置信息。 | 您可以联系管理员获取相应帐号对应权限的keytab文件和krb5文件。 |
说明:
运行SmallFS小文件样例代码时需要包含配置文件“smallfs-site.xml”,运行HDFS样例代码时不需要包含。
不同集群的“user.keytab”、“krb5.conf”不能共用。
- “conf”目录下的“log4j.properties”文件客户根据自己的需要进行配置。
代码样例
如下是代码片段,详细代码请参考com.huawei.bigdata.hdfs.examples中的HdfsMain类。
在Linux客户端运行应用和在Windows环境下运行应用的初始化代码相同,代码样例如下所示。
/**
* 初始化,获取一个FileSystem实例
*
* @throws IOException
*/
private void init() throws IOException {
confLoad();
authentication();
instanceBuild();
}
/**
*
* 如果程序运行在Linux上,则需要core-site.xml、hdfs-site.xml的路径,
* 修改为在Linux下客户端文件的绝对路径。
*
*/
private void confLoad() throws IOException {
conf = new Configuration();
// conf file
conf.addResource(new Path(PATH_TO_HDFS_SITE_XML));
conf.addResource(new Path(PATH_TO_CORE_SITE_XML));
//conf.addResource(new Path(PATH_TO_SMALL_SITE_XML));
}
/**
* kerberos security authentication
* 如果程序运行在Linux上,则需要krb5.conf和keytab文件的路径,
* 修改为在Linux下客户端文件的绝对路径。并且需要将样例代码中的keytab文件和principle文件
* 分别修改为当前用户的keytab文件名和用户名。
*
*/
private void authentication() throws IOException {
// 安全模式
if ("kerberos".equalsIgnoreCase(conf.get("hadoop.security.authentication"))) {
System.setProperty("java.security.krb5.conf", PATH_TO_KRB5_CONF);
LoginUtil.login(PRNCIPAL_NAME, PATH_TO_KEYTAB, PATH_TO_KRB5_CONF, conf);
}
}
/**
* build HDFS instance
*/
private void instanceBuild() throws IOException {
// get filesystem
fSystem = FileSystem.get(conf);
}
说明:
示例代码中
//conf.addResource(new Path(PATH_TO_SMALL_SITE_XML));
为注释掉的SmallFS小文件样例代码。若需要运行SmallFS小文件,则需加入该代码。
在Windows环境和Linux环境下都需要运行login的代码样例,用于第一次登录使用,详细代码请参考com.huawei.hadoop.security中的LoginUtil类。
public synchronized static void login(String userPrincipal,
String userKeytabPath, String krb5ConfPath, Configuration conf)
throws IOException {
// 1.检查放入的参数
if ((userPrincipal == null) || (userPrincipal.length() <= 0)) {
LOG.error("input userPrincipal is invalid.");
throw new IOException("input userPrincipal is invalid.");
}
if ((userKeytabPath == null) || (userKeytabPath.length() <= 0)) {
LOG.error("input userKeytabPath is invalid.");
throw new IOException("input userKeytabPath is invalid.");
}
if ((krb5ConfPath == null) || (krb5ConfPath.length() <= 0)) {
LOG.error("input krb5ConfPath is invalid.");
throw new IOException("input krb5ConfPath is invalid.");
}
if ((conf == null)) {
LOG.error("input conf is invalid.");
throw new IOException("input conf is invalid.");
}
// 2.检查文件是否存在
File userKeytabFile = new File(userKeytabPath);
if (!userKeytabFile.exists()) {
LOG.error("userKeytabFile(" + userKeytabFile.getAbsolutePath()
+ ") does not exsit.");
throw new IOException("userKeytabFile("
+ userKeytabFile.getAbsolutePath() + ") does not exsit.");
}
if (!userKeytabFile.isFile()) {
LOG.error("userKeytabFile(" + userKeytabFile.getAbsolutePath()
+ ") is not a file.");
throw new IOException("userKeytabFile("
+ userKeytabFile.getAbsolutePath() + ") is not a file.");
}
File krb5ConfFile = new File(krb5ConfPath);
if (!krb5ConfFile.exists()) {
LOG.error("krb5ConfFile(" + krb5ConfFile.getAbsolutePath()
+ ") does not exsit.");
throw new IOException("krb5ConfFile(" + krb5ConfFile.getAbsolutePath()
+ ") does not exsit.");
}
if (!krb5ConfFile.isFile()) {
LOG.error("krb5ConfFile(" + krb5ConfFile.getAbsolutePath()
+ ") is not a file.");
throw new IOException("krb5ConfFile(" + krb5ConfFile.getAbsolutePath()
+ ") is not a file.");
}
// 3.设置并检查krb5config
setKrb5Config(krb5ConfFile.getAbsolutePath());
setConfiguration(conf);
// 4.检查是否需要登录
if (checkNeedLogin(userPrincipal)) {
// 5.登录hadoop并检查
loginHadoop(userPrincipal, userKeytabFile.getAbsolutePath());
}
// 6.检查重新登录
checkAuthenticateOverKrb();
System.out.println("Login success!!!!!!!!!!!!!!");
}