简体   繁体   中英

reading a .properties file stored in hdfs using java code

i need to read .properties file which is available in the hdfs. i am using the below code but it throws a run time error.

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")));

the run time error is :

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")));

For loading properties file form hdfs:

  1. make sure ur core-site.xml , hdfs-site xml file path
  2. hdfs port no(it will available in core-site.xml )
  3. replace key in 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(); 

Either use fully qualified name of class:

java.io.ObjectInputStream 

OR

import the class using following line:

import java.io.ObjectInputStream;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM