![](/img/trans.png)
[英]Is it possible to avoid using "static" when call variable from another class?
[英]Using static variable of another class in Hadoop Cluster
我有如下的hadoop程序。 我放入了相關代碼段。 我傳遞了將main中的BiG_DATA讀為true的參數。 主要是“正在處理大數據”。 但是當談到RowPreMap類中的map方法時,BIG_DATA的值是其初始化值false。 不知道為什么會這樣。 我想念什么嗎? 當我在獨立的計算機上運行此代碼時,此方法有效,但當我在hadoop群集上執行此操作時,則無效。 作業由JobControl處理。 有線程嗎?
公共類UVDriver擴展了配置的工具Tool {
public static class RowMPreMap extends MapReduceBase implements
Mapper<LongWritable, Text, Text, Text> {
private Text keyText = new Text();
private Text valText = new Text();
public void map(LongWritable key, Text value,
OutputCollector<Text, Text> output, Reporter reporter)
throws IOException {
// Input: (lineNo, lineContent)
// Split each line using seperator based on the dataset.
String line[] = null;
if (Settings.BIG_DATA)
line = value.toString().split("::");
else
line = value.toString().split("\\s");
keyText.set(line[0]);
valText.set(line[1] + "," + line[2]);
// Output: (userid, "movieid,rating")
output.collect(keyText, valText);
}
}
public static class Settings {
public static boolean BIG_DATA = false;
public static int noOfUsers = 0;
public static int noOfMovies = 0;
public static final int noOfCommonFeatures = 10;
public static final int noOfIterationsRequired = 3;
public static final float INITIAL_VALUE = 0.1f;
public static final String NORMALIZE_DATA_PATH_TEMP = "normalize_temp";
public static final String NORMALIZE_DATA_PATH = "normalize";
public static String INPUT_PATH = "input";
public static String OUTPUT_PATH = "output";
public static String TEMP_PATH = "temp";
}
public static class Constants {
public static final int BIG_DATA_USERS = 71567;
public static final int BIG_DATA_MOVIES = 10681;
public static final int SMALL_DATA_USERS = 943;
public static final int SMALL_DATA_MOVIES = 1682;
public static final int M_Matrix = 1;
public static final int U_Matrix = 2;
public static final int V_Matrix = 3;
}
public int run(String[] args) throws Exception {
// 1. Pre-process the data.
// a) Normalize
// 2. Initialize the U, V Matrices
// a) Initialize U Matrix
// b) Initialize V Matrix
// 3. Iterate to update U and V.
// Write Job details for each of the above steps.
Settings.INPUT_PATH = args[0];
Settings.OUTPUT_PATH = args[1];
Settings.TEMP_PATH = args[2];
Settings.BIG_DATA = Boolean.parseBoolean(args[3]);
if (Settings.BIG_DATA) {
System.out.println("Working on BIG DATA.");
Settings.noOfUsers = Constants.BIG_DATA_USERS;
Settings.noOfMovies = Constants.BIG_DATA_MOVIES;
} else {
System.out.println("Working on Small DATA.");
Settings.noOfUsers = Constants.SMALL_DATA_USERS;
Settings.noOfMovies = Constants.SMALL_DATA_MOVIES;
}
// some code here
handleRun(control);
return 0;
}
public static void main(String args[]) throws Exception {
System.out.println("Program started");
if (args.length != 4) {
System.err
.println("Usage: UVDriver <input path> <output path> <fs path>");
System.exit(-1);
}
Configuration configuration = new Configuration();
String[] otherArgs = new GenericOptionsParser(configuration, args)
.getRemainingArgs();
ToolRunner.run(new UVDriver(), otherArgs);
System.out.println("Program complete.");
System.exit(0);
}
}
作業控制。
public static class JobRunner implements Runnable {
private JobControl control;
public JobRunner(JobControl _control) {
this.control = _control;
}
public void run() {
this.control.run();
}
}
public static void handleRun(JobControl control)
throws InterruptedException {
JobRunner runner = new JobRunner(control);
Thread t = new Thread(runner);
t.start();
int i = 0;
while (!control.allFinished()) {
if (i % 20 == 0) {
System.out
.println(new Date().toString() + ": Still running...");
System.out.println("Running jobs: "
+ control.getRunningJobs().toString());
System.out.println("Waiting jobs: "
+ control.getWaitingJobs().toString());
System.out.println("Successful jobs: "
+ control.getSuccessfulJobs().toString());
}
Thread.sleep(1000);
i++;
}
if (control.getFailedJobs() != null) {
System.out.println("Failed jobs: "
+ control.getFailedJobs().toString());
}
}
這是行不通的,因為static修飾符的范圍不會跨越JVM的多個實例(更不用說網絡了)。
映射任務始終在單獨的JVM中運行,即使它在工具運行程序本地運行也是如此。 映射器類僅使用類名實例化,而無權訪問在工具運行器中設置的信息。
這是配置框架存在的原因之一。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.