使用java代码读取存储在hdfs中的.properties文件

使用java代码读取存储在hdfs中的.properties文件,java,hadoop,workflow,oozie,Java,Hadoop,Workflow,Oozie,我需要读取hdfs中可用的.properties文件。我正在使用下面的代码,但它抛出了一个运行时错误 FileSystem fs = FileSystem.get(config); Properties conf = wc.createConfiguration(); Properties prop = new Properties(); String appPath = "hdfs://clusterdb05.com:8020/user/cmahajan/" + ve

我需要读取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")));

使用类的完全限定名称:

java.io.ObjectInputStream 

使用以下行导入类:

import java.io.ObjectInputStream;

要从hdfs加载属性文件,请执行以下操作:

  • 确保ur核心站点.xmlhdfs站点xml文件路径
  • hdfs端口号(将在核心站点.xml中提供)
  • 替换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的
    。但现在它在第行给出了StreamCorruptedException:“ObjectInputStream f=newObjectInputStream(fs.open(new Path(“/user/cmahajan/app.properties”));”。有什么已知的原因吗?我可能不知道确切的原因,但这意味着您正在读取的数据没有按照预期的格式格式化。请参阅: