【问题标题】:loading an external properties file in udf在 udf 中加载外部属性文件
【发布时间】:2011-08-27 07:19:06
【问题描述】:

当写一个 UDF 让我们说一个 EvalFunc 时,是否可以传递一个配置文件

properties = new Properties();
properties.load(new FileInputStream("conf/config.properties"));

在 Hadoop 模式下运行时?

最好, 会

【问题讨论】:

    标签: hadoop user-defined-functions apache-pig


    【解决方案1】:

    这是来自http://wiki.apache.org/hadoop/HadoopDfsReadWriteExampleSimple Example to Read and Write files from Hadoop DFS

    也许你可以在其中找到一些有用的代码来完成你的工作。

    以下是我的代码,它在hadoop中成功加载了一个属性文件,我使用了Apache Commons Configurationhttp://commons.apache.org/configuration/

    public static void loadProperites(String path) throws ConfigurationException, IOException {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        Path inFile = new Path(path);
        FSDataInputStream in = fs.open(inFile);
    
        PropertiesConfiguration config = new PropertiesConfiguration();
        config.load(in);
    
        in.close();
    }
    

    【讨论】:

      【解决方案2】:

      使用 Apache Commons Configuration2 和 vfs2:

      
      Parameters params = new Parameters();
      FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
                          new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
                                  .configure(params.fileBased().setFileSystem(new VFSFileSystem())
                                          .setLocationStrategy(new FileSystemLocationStrategy())
                                          .setEncoding("UTF-8").setFileName(propertyPath));
      config = builder.getConfiguration();
      
      

      【讨论】:

        猜你喜欢
        • 2013-01-28
        • 1970-01-01
        • 2014-12-30
        • 1970-01-01
        • 2012-03-18
        • 2014-01-09
        • 2016-06-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多