我想知道如何读取使用添加的 Hive 资源ADD FILE
来自乌德夫?
例如
Hive > add file /users/temp/key.jks
Java中的UDF可以读取这个文件吗?
在 Udf 中获取此文件的路径是什么?
谢谢
大卫
一旦资源被添加到会话中,使用ADD
命令,Hive 查询可以通过其名称(在 map/reduce/transform 子句中)引用它,并且该资源在整个 Hadoop 集群上执行时在本地可用。 Hive 使用 Hadoop 的分布式缓存在查询执行时将添加的资源分发到集群中的所有计算机。看这里:Hive资源 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Cli#LanguageManualCli-HiveResources
有的是in_file(string str, string filename)
Hive 中的函数 - 如果字符串 str 在文件名中作为整行出现,则返回 true。您可以使用 in_file 源代码作为示例:通用UDFInFile.java https://github.com/apache/hive/blob/master/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFInFile.java
源码中的几个方法:
private BufferedReader getReaderFor(String filePath) throws HiveException {
try {
Path fullFilePath = FileSystems.getDefault().getPath(filePath);
Path fileName = fullFilePath.getFileName();
if (Files.exists(fileName)) {
return Files.newBufferedReader(fileName, Charset.defaultCharset());
}
else
if (Files.exists(fullFilePath)) {
return Files.newBufferedReader(fullFilePath, Charset.defaultCharset());
}
else {
throw new HiveException("Could not find \"" + fileName + "\" or \"" + fullFilePath + "\" in IN_FILE() UDF.");
}
}
catch(IOException exception) {
throw new HiveException(exception);
}
}
private void loadFromFile(String filePath) throws HiveException {
set = new HashSet<String>();
BufferedReader reader = getReaderFor(filePath);
try {
String line;
while((line = reader.readLine()) != null) {
set.add(line);
}
} catch (Exception e) {
throw new HiveException(e);
}
finally {
IOUtils.closeStream(reader);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)