我使用命令'add file',隨後文件將由UDF加載。Hive添加的文件在哪裏?
但我找不到HDFS(hdfs:// namenode:8026/user/hdfs)配置單元添加的文件,我需要udf方法中的路徑。
什麼是文件的路徑,以及如何通過UDF使用它?
我使用命令'add file',隨後文件將由UDF加載。Hive添加的文件在哪裏?
但我找不到HDFS(hdfs:// namenode:8026/user/hdfs)配置單元添加的文件,我需要udf方法中的路徑。
什麼是文件的路徑,以及如何通過UDF使用它?
DFS路徑是不是從UDF/UDTF訪問,你需要提供UDF/UDTF本地路徑。
我的方法:
檢查該文件存在於本地的「/ tmp目錄」與否。 如果是的話,有非零文件長度,然後用它,否則拉離DFS文件:/共享「/ tmp和繼續。
public static boolean readFile(){
BufferedReader br=null;
try {
File f = new File("/tmp/" + fileName);
if (! f.exists() || f.length() == 0){
// Pull fresh file from dfs:/xyz.
String cmd = "hadoop fs -get /xyz/" + fileName + " /tmp/";
Runtime run = Runtime.getRuntime();
System.err.println("Pulling Mapping file from HDFS. Running: " + cmd);
Process pr = run.exec(cmd);
try {
// Waiting for the job to complete.
pr.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//open file for reading
br = new BufferedReader(new FileReader("/tmp/" + fileName));
String line= br.readLine();
while(line != null){
System.out.println(line);
//read next line
line = br.readLine();
}
br.close();
}catch (FileNotFoundException e){
System.err.println("File not found - "+fileName);
return false;
}catch(IOException e){
System.err.println("Error while reading from the preset file");
return false;
}
return true;
}
哇〜Linux的CMD可以在UDF被執行~~我學會了......謝謝! – superz