MAPREDUCE服务 MRS-初始化HDFS:代码样例

时间:2024-10-18 17:22:20

代码样例

如下是代码片段,详细代码请参考com.huawei.bigdata.hdfs.examples的HdfsExample类。

在Linux客户端运行应用和在Windows环境下运行应用的初始化代码相同,代码样例如下所示。
 // 完成初始化和认证
 confLoad();
 authentication();
 // 创建一个用例
 HdfsExample hdfs_examples = new HdfsExample("/user/hdfs-examples", "test.txt");


 /**
  * 
  * 如果程序运行在Linux上,则需要core-site.xml、hdfs-site.xml的路径修改为在Linux下客户端文件的绝对路径
  *
  * 
  */  
 private static 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));
 }

 /**
  *安全认证
  *
  */
 private static void authentication() throws IOException {
   // security mode
   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);
   }
 }
 /**
  *创建用例
  */
 public HdfsExample(String path, String fileName) throws IOException  {
   this.DEST_PATH = path;
   this.FILE_NAME = fileName;
   instanceBuild();
 }

 private void instanceBuild() throws IOException {
   fSystem = FileSystem.get(conf);
 }
support.huaweicloud.com/devg-lts-mrs/mrs_07_090013.html