【问题标题】:Where is the file added by Hive?Hive添加的文件在哪里?
【发布时间】:2017-05-05 09:38:52
【问题描述】:

我使用命令“添加文件”,该文件随后将由 UDF 加载。

但我在 HDFS(hdfs://namenode:8026/user/hdfs) 找不到 hive 添加的文件,我需要 udf 方法中的路径。

文件的路径是什么,如何通过udf使用?

【问题讨论】:

    标签: hive


    【解决方案1】:

    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;        
    }
    

    【讨论】:

      【解决方案2】:

      add file在 Hive 的分布式缓存中添加文件。

      【讨论】:

      • 感谢您的回复...我必须找到新的方法
      • 如果您提供详细要求,我会帮助您。我已经处理过这个场景。
      • 谢谢!我正在编写一个 UDTF 来通过从公司购买的 *.dat 文件解析 IP 地址。所以我需要在我的 UDTF 方法中指明 .dat 文件路径。我已将文件放在 dfs 上,但是当我运行该方法时,它总是说:'java.io.FileNotFoundException: hdfs:/192.168.7.20:8020/tmp/ip_20170204.dat (No such file or directory)'
      • 对了,我的UDTF Java代码细节可以看我之前的问题:stackoverflow.com/questions/43799688/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 2011-04-03
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      相关资源
      最近更新 更多