簡體   English   中英

使用Java代碼讀取存儲在hdfs中的.properties文件

[英]reading a .properties file stored in hdfs using java code

我需要閱讀hdfs中可用的.properties文件。 我正在使用以下代碼,但會引發運行時錯誤。

FileSystem fs = FileSystem.get(config);

    Properties conf = wc.createConfiguration();
    Properties prop = new Properties();
    String appPath = "hdfs://clusterdb05.com:8020/user/cmahajan/" + version + "/apps/apps/";
    conf.setProperty(OozieClient.APP_PATH,appPath);
    FileInputStream f = new FileInputStream("hdfs://clusterdb05.com:8020/user/cmahajan/app.properties");
    ObjectInputStream f = new ObjectInputStream(fs.open(new Path("/user/cmahajan/app.properties")));

運行時錯誤是:

LaunchJob.java:28: cannot find symbol

symbol  : class ObjectInputStream
location: class LaunchJob
    ObjectInputStream f = new ObjectInputStream(fs.open(new Path("/user/cmahajan/app.properties")));
    ^
LaunchJob.java:28: cannot find symbol
symbol  : class ObjectInputStream
location: class LaunchJob
    ObjectInputStream f = new ObjectInputStream(fs.open(new Path("/user/cmahajan/app.properties")));

對於加載屬性文件形式的hdfs:

  1. 確保您的core-site.xmlhdfs-site xml文件路徑
  2. hdfs端口號(將在core-site.xml中提供
  3. 替換getProperty中的鍵。

      String CURRENCIES_DIM1 = null; String DATES_DIM2 = null; Configuration conf = new Configuration(); conf.addResource(new Path("/usr/local/hadoop/etc/hadoop/core-site.xml")); conf.addResource(new Path("/usr/local/hadoop/etc/hadoop/hdfs-site.xml")); String filePath = "hdfs://localhost:54310/user/CurrencyCache.properties"; Path path = new Path(filePath); FileSystem fs = path.getFileSystem(conf); try (FSDataInputStream currencyInputStream = fs.open(path)) { Properties currencyProp = new Properties(); currencyProp.load(currencyInputStream); CURRENCIES_DIM1= currencyProp.getProperty("key");//getting the 'CURRENCIES_DIM' file path from properties file DATES_DIM2= currencyProp.getProperty("key"); //getting the 'DATES_DIM' file path from properties file } catch (IOException e) { e.printStackTrace(); } fs.close(); 

可以使用類的完全限定名稱:

java.io.ObjectInputStream 

要么

使用以下行導入類:

import java.io.ObjectInputStream;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM