您可以直接在代码中配置 log4j。例如,您可以PropertyConfigurator.configure(properties);
在 mapper/reducer 设置方法中调用 eg。
这是存储在 hdfs 上的属性的示例:
InputStream is = fs.open(log4jPropertiesPath);
Properties properties = new Properties();
properties.load(is);
PropertyConfigurator.configure(properties);
其中 fs 是 FileSystem 对象,log4jPropertiesPath 是 hdfs 上的路径。
有了这个,您还可以将日志输出到带有 job_id 的目录。例如,您可以在调用 PropertyConfigurator.configure(properties); 之前修改我们的属性。
Enumeration propertiesNames = properties.propertyNames();
while (propertiesNames.hasMoreElements()) {
String propertyKey = (String) propertiesNames.nextElement();
String propertyValue = properties.getProperty(propertyKey);
if (propertyValue.indexOf(JOB_ID_PATTERN) != -1) {
properties.setProperty(propertyKey, propertyValue.replace(JOB_ID_PATTERN, context.getJobID().toString()));
}
}