【问题标题】:how to read multiple files from a file in Apache Pig?如何从 Apache Pig 中的文件中读取多个文件?
【发布时间】:2015-04-28 23:37:14
【问题描述】:

我有一个名为“filelist.txt”的文件,这个文件的内容是一个列表文件,我想读入我的猪脚本。例如,它可以组织为:

file1.txt
file2.txt
...
filen.txt

一些解决方案正在尝试使用正则表达式,但是文件名中没有特定的格式,我们唯一能做的就是从 filelist.txt 中读取文件名

在每个文件中都是我要读取的实际数据。例如,在 file1 中,我们可以有:

value1
value2
value3

那么我应该如何在我的 pig 脚本中读取所有这些文件的值?

【问题讨论】:

    标签: hadoop apache-pig


    【解决方案1】:

    目前没有办法在纯猪中做到这一点。你可以在纯猪中做的最好的事情是使用它们的内置 globbing,你可以找到有关 here 的信息。它相当灵活,但听起来不足以满足您的目的。

    如果您可以在本地环境中获取该文件,我能想到的另一个解决方案是使用某种wrapper (I would recommend python)。在该脚本中,您可以读取该文件并生成一个 pig 脚本来读取这些行。以下是该逻辑的工作原理:

    def addLoads(filesToRead, schema, delim='\\t'):
    
        newLines = []
        with open(filesToRead, 'r') as infile:
    
            for n, f in enumerate(infile):
                newLines.append("input{} = LOAD '{}' USING PigStorage('{}') AS {};".format(n, f, delim, schema))
    
        to_union = [ 'input{}'.format(i) for i in range(1, len(newLines)+1) ]
    
        newLines.append('loaded_lines = UNION {} ;'.format(', '.join(to_union)))
    
        return '\n'.join(newLines)
    

    将 this 附加到您从磁盘加载的 pig 脚本的开头,并确保脚本的其余部分使用 loaded_lines 作为开头。

    【讨论】:

      【解决方案2】:

      你必须使用 pig load func 并覆盖 setlocation

       @Override
          public void setLocation(String location, Job job) throws IOException {
              //Read location where you have all the input file names and convert that into a comma seperated string.
              FileInputFormat.setInputPaths(job, [commaseperated list]);
          }
      

      位置将以逗号分隔的文件列表显示。

      【讨论】:

      • 我没听懂。看来你已经知道文件的格式了。但我想要的是读取文件名在另一个文件列表中的文件。我该怎么做?
      • 是的,我误读了早期版本。我觉得上面那个好看。不确定它是否能解决问题。
      猜你喜欢
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      相关资源
      最近更新 更多