【发布时间】:2017-05-05 09:38:52
【问题描述】:
我使用命令“添加文件”,该文件随后将由 UDF 加载。
但我在 HDFS(hdfs://namenode:8026/user/hdfs) 找不到 hive 添加的文件,我需要 udf 方法中的路径。
文件的路径是什么,如何通过udf使用?
【问题讨论】:
标签: hive
我使用命令“添加文件”,该文件随后将由 UDF 加载。
但我在 HDFS(hdfs://namenode:8026/user/hdfs) 找不到 hive 添加的文件,我需要 udf 方法中的路径。
文件的路径是什么,如何通过udf使用?
【问题讨论】:
标签: hive
UDF/UDTF 无法访问 dfs 路径,您需要在 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;
}
【讨论】:
add file在 Hive 的分布式缓存中添加文件。
【讨论】: