java 除了修改hadoop-env.sh之外,如何在hadoop中指定系统属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15490090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:46:59  来源:igfitidea点击:

How to specify system property in hadoop except modify hadoop-env.sh?

javahadoopsystem-properties

提问by zjffdu

I'd like to set system property (not hadoop property) when running hadoop jobs. I found that it's not easy to setting system property. Even I set the property in shell

我想在运行 hadoop 作业时设置系统属性(不是 hadoop 属性)。我发现设置系统属性并不容易。即使我在 shell 中设置了属性

export HADOOP_OPTS="$HADOOP_OPTS:-Dproperty=value"

It's still not working. The "-D" option of hadoop command line is only for Configuration, not system property. So the "-D" option also not working

它仍然无法正常工作。hadoop 命令行的“-D”选项仅用于配置,而不用于系统属性。所以“-D”选项也不起作用

Anyone has ideas ? thanks

任何人都有想法?谢谢

回答by Quetzalcoatl

Why not simply use -Dfoo.bar=examplein-line when starting the job via command line, like so:

为什么不在-Dfoo.bar=example通过命令行启动作业时简单地使用内联,如下所示:

hadoop jar example.jar com.example.ExampleTool -Dfoo.bar=example argument

hadoop jar example.jar com.example.ExampleTool -Dfoo.bar=example argument

In order to get at the property in code, use conf.get("foo.bar");

为了在代码中获取属性,请使用 conf.get("foo.bar");



Now if you genuinely need it to be set as a system property, you can set it at the start of your code using the value you got from Hadoop config like so:

现在,如果您真的需要将其设置为系统属性,则可以使用从 Hadoop 配置中获得的值在代码的开头进行设置,如下所示:

String property = conf.get("foo.bar");
System.setProperty("foo.bar", property);

回答by Jiacai Liu

The hadoopscript invoke java class like this

hadoop脚本调用Java类这样

exec "$JAVA" $JAVA_HEAP_MAX $HADOOP_OPTS $CLASS "$@"

so, we can pass system-wide property like this:

所以,我们可以像这样传递系统范围的属性:

export HADOOP_OPTS="$HADOOP_OPTS -Dfoo=bar"

回答by Praveen Sripati

Check the difference between system properties and the environment variables and how to set them here.

检查系统属性和环境变量之间的差异以及如何在此处设置它们。