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

lrpiutwd  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(348)

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

ppcbkaq51#

使用类的完全限定名:

java.io.ObjectInputStream


使用以下行导入类:

import java.io.ObjectInputStream;
91zkwejq

91zkwejq2#

对于从hdfs加载属性文件:
确保ur core site.xml、hdfs site xml文件路径
hdfs端口号(将在core site.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();

相关问题