gpt4 book ai didi

hadoop - 如何将系统属性传递给 hadoop 中的映射函数

转载 作者:可可西里 更新时间:2023-11-01 14:42:51 25 4
gpt4 key购买 nike

有没有一种方法可以将系统参数(例如 -Dmy_param=XXX)传递给 hadoop map reduce 框架中的映射函数。将作业提交到 hadoop 集群是通过 .setJarByClass() 完成的。在映射器中,我必须创建配置,所以我想让它可配置,所以我认为通过属性文件的标准方式就可以了。只是在设置属性的地方努力传递参数。另一种方法是将属性文件添加到提交的 jar。有人有解决这个问题的经验吗?

最佳答案

如果您还没有在您的工作中使用过它,您可以尝试使用 GenericOptionsParser、Tool 和 ToolRunner 来运行 Hadoop 作业。

注意:MyDriver 扩展 Configured 并实现 Tool。而且,运行你的工作使用这个

hadoop -jar somename.jar MyDriver -D your.property=value arg1 arg2

欲了解更多信息,check this link .

这是我为您准备的一些示例代码:

public class MyDriver extends Configured implements Tool {

public static class MyDriverMapper extends Mapper<LongWritable, Text, LongWritable, NullWritable> {

protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
// In the mapper you can retrieve any configuration you've set
// while starting the job from the terminal as shown below

Configuration conf = context.getConfiguration();
String yourPropertyValue = conf.get("your.property");
}
}

public static class MyDriverReducer extends Reducer<LongWritable, NullWritable, LongWritable, NullWritable> {

protected void reduce(LongWritable key, Iterable<NullWritable> values, Context context)
throws IOException, InterruptedException {
// --- some code ---
}
}

public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new MyDriver(), args);
System.exit(exitCode);
}

@Override
public int run(String[] args) throws Exception {
Configuration conf = getConf();
// if you want you can get/set to conf here too.
// your.property can also be file location and after
// you retrieve the properties and set them one by one to conf object.

// --other code--//
Job job = new Job(conf, "My Sample Job");
// --- other code ---//
return (job.waitForCompletion(true) ? 0 : 1);
}
}

关于hadoop - 如何将系统属性传递给 hadoop 中的映射函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17759826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com