我使用命令“添加文件”,然后文件将由 UDF 加载。
但是我在 HDFS(hdfs://namenode:8026/user/hdfs) 上找不到 hive 添加的文件,我需要 udf 方法中的路径。
文件的路径是什么以及如何通过udf使用它?
dfs 路径无法从 UDF/UDTF 访问,您需要在 UDF/UDTF 中提供本地路径。
我的做法:
检查文件是否存在于本地的“/tmp”。如果是并且具有非零文件长度,则使用它,否则将文件从 DFS:/shared 拉到 '/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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句