2012-10-17 81 views
4

我需要读取hdfs中可用的.properties文件。我使用下面的代码,但它会引发运行时错误。使用java代码读取存储在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"))); 
+0

什么是完整的代码,包括进口?这似乎是你缺少一个'import java.io.ObjectInputStream' – Alex

+0

是的,这就是 – Chirag

回答

0

要么使用类的全名:

java.io.ObjectInputStream 

OR

使用以下行导入类

import java.io.ObjectInputStream; 
+1

这是代码中缺少的原因。但是现在它在行上提供了StreamCorruptedException:“ObjectInputStream f = new ObjectInputStream(fs.open(new Path(”/ user/cmahajan/app.properties“)));”。任何已知的原因? – Chirag

+0

我可能不知道确切原因,但这意味着您正在阅读的数据未按预期格式化。请参阅:http://www.javamex.com/tutorials/io/StreamCorruptedException.shtml – Azodious

1

对于装载属性文件形式HDFS:

  1. 确保UR 核心的site.xmlHDFS现场XML文件路径
  2. HDFS端口号(它会在核心提供现场.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();