2013-09-24 39 views
0

我有UDF豬拉丁文的問題。 我試圖實現一個系統,必須驗證存儲在本地的矩陣和存儲在hadoop存儲庫中的一組矩陣之間是否存在「映射」。 對於映射,我的意思是如果在hadoop中存在一個存儲矩陣的行和列的置換,該矩陣轉換矩陣等於存儲在本地的矩陣。 因爲矩陣可以有數百個元素,所以我想在hadoop上執行映射算法來使用並行性。 我正在尋找UDF拉丁豬,但我不明白如何「發送」本地矩陣到UDF函數。發送一個矩陣到udf豬拉丁文

public class Mapping extends EvalFunc<String> 
{ 
private int[][] matrixToMap; //The local matrix i want to map 

public String exec(Tuple input) throws IOException { //Here the tuple are the matrix stored in hadoop 
    if (input == null || input.size() == 0) 
     return null; 
    try{ 
     //HERE THE CODE FOR THE MAPPING 
    } 

    } 
    } 

}

我的問題是我怎麼能初始化matrixToMap考慮,我將使用此代碼屬性:

REGISTER /Users/myudfs.jar; 
//SOME CODE TO INITIALIZE ATTRIBUTE matrixToMap 
records = LOAD 'Sample7.txt' //the matrix stored in hadoop 
B = FOREACH records GENERATE myudfs.mapping(records); 

認爲豬腳本是在一個Java程序調用,並且局部矩陣存儲在一個Java矩陣中。所以Java程序看起來像:

int [][] localMatrix; 
pigServer.registerJar("/Users/myudfs.jar"); 
//Some code to make Mapping.matrixToMap = localMatrix 
pigServer.registerQuery("records = LOAD 'Sample7.txt';"); 
pigServer.registerQuery("B = FOREACH records GENERATE myudfs.Mapping(formula);"); 

你有什麼想法嗎? 感謝

回答

0

你可以在你的UDF的構造函數初始化類變量是:

public class Mapping extends EvalFunc<String> 
{ 
    private int[][] matrixToMap; //The local matrix i want to map 

    public Mapping(String filename) { 
    // Code to populate matrixToMap from the data in filename 
    } 

    public String exec(Tuple input) throws IOException { //Here the tuple are the matrix stored in hadoop 
    if (input == null || input.size() == 0) 
     return null; 
    try{ 
     //HERE THE CODE FOR THE MAPPING 
    } 

    } 
} 

在腳本中,使用以下行:

DEFINE Mapping myudfs.Mapping('/path/to/matrix/on/HDFS'); 

用這種方法你的矩陣必須是存儲在HDFS上,以便初始化並調用構造函數的映射器或簡化器將可以訪問數據。

+0

謝謝你的回答,我很感激。所以,如果我明白了,你建議:1)在hdfs上存儲本地矩陣2)DEFINE映射myudfs.Mapping('/ path/to/matrix/on/HDFS'); 3)pigServer.registerJar(「/ Users/myudfs.jar」); 4)pigServer.registerQuery(「records = LOAD'Sample7.txt';」); – user2811222

+0

我錯過了關於如何通過Java調用它的部分。在這種情況下,我不確定'DEFINE'語句是否能正常工作。但試試看吧。 –