在 UDF 中加载外部属性文件



在编写UDF时,假设EvalFunc,是否可以传递

配置文件
properties = new Properties();
properties.load(new FileInputStream("conf/config.properties"));

在Hadoop模式下运行时?

最好将

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

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

以下是我的代码,它成功地在hadoop中加载了一个属性文件,我使用了Apache Commons Configuration http://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();
}

使用 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();

最新更新