如何将小兽人文件组合或合并到较大的兽人文件中



so上的大多数问题/答案,网络讨论使用Hive将一堆小兽人文件组合到较大的文件中,但是,我的orc文件是日志文件,这些文件是按白天分开的。我需要将它们分开。我只想每天"汇总"兽人文件(这是HDFS中的目录(。

我最有可能在Java中编写解决方案,并且遇到了我需要使用的OrcfilemerGeoperator,但仍然为时过早。

解决此问题的最佳方法是什么?

您无需重新发明轮子。

ALTER TABLE table_name [PARTITION partition_spec] CONCATENATE可以用来将小兽人文件合并到较大的文件中,因为Hive 0.14.0.合并在条纹级别上发生,从而避免了解压缩和解码数据。它很快起作用。我建议创建一个按白天划分的外部表(分区是目录(,然后将它们合并,所有这些都将PARTITION (day_column)指定为分区规格。

请参阅此处:语言 ORC

这里有一个很好的答案,但是这些答案都不允许我经营cron工作,以便我可以每天进行卷起。我们每天都会写入HDFS的Journald日志文件,我不想每天都在Hive中运行查询。

我最终做的事情对我来说似乎更加简单。我写了一个Java程序,该程序使用兽人库在目录中扫描所有文件并创建这些文件列表。然后打开一个新的作者,该作者是"组合"文件(以"开头。然后,该程序在列表中打开每个文件,并读取内容并将其写入组合文件。读取所有文件后,它会删除文件。我还添加了在需要时在时间运行一个目录的功能。

注意:您将需要一个架构文件。JournalD日志可以在JSON" JournalCtl -O JSON"中输出,然后您可以使用Apache ORC工具生成模式文件,也可以手工执行一个。来自兽人的自动 - 天然是好的,但手动总是更好。

注意:要使用此代码,您将需要一个有效的键盘键并在classPath中添加-dkeytab =。

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.orc.OrcFile;
import org.apache.orc.Reader;
import org.apache.orc.RecordReader;
import org.apache.orc.TypeDescription;
import org.apache.orc.Writer;
import com.cloudera.org.joda.time.LocalDate;
public class OrcFileRollUp {
  private final static String SCHEMA = "journald.schema";
  private final static String UTF_8 = "UTF-8";
  private final static String HDFS_BASE_LOGS_DIR = "/<baseDir>/logs";
  private static final String keytabLocation = System.getProperty("keytab");
  private static final String kerberosUser = "<userName>";
  private static Writer writer;
  public static void main(String[] args) throws IOException {
    Configuration conf = new Configuration();
    conf.set("hadoop.security.authentication", "Kerberos");
    InetAddress myHost = InetAddress.getLocalHost();
    String kerberosPrincipal = String.format("%s/%s", kerberosUser, myHost.getHostName());
    UserGroupInformation.setConfiguration(conf);
    UserGroupInformation.loginUserFromKeytab(kerberosPrincipal, keytabLocation);
    int currentDay = LocalDate.now().getDayOfMonth();
    int currentMonth = LocalDate.now().getMonthOfYear();
    int currentYear = LocalDate.now().getYear();
    Path path = new Path(HDFS_BASE_LOGS_DIR);
    FileSystem fileSystem = path.getFileSystem(conf);
    System.out.println("The URI is: " + fileSystem.getUri());

    //Get Hosts:
    List<String> allHostsPath = getHosts(path, fileSystem);
    TypeDescription schema = TypeDescription.fromString(getSchema(SCHEMA)
        .replaceAll("n", ""));
    //Open each file for reading and write contents
    for(int i = 0; i < allHostsPath.size(); i++) {
      String outFile = "." + currentYear + "_" + currentMonth + "_" + currentDay + ".orc.working";            //filename:  .2018_04_24.orc.working
      //Create list of files from directory and today's date OR pass a directory in via the command line in format 
      //hdfs://<namenode>:8020/HDFS_BASE_LOGS_DIR/<hostname>/2018/4/24/
      String directory = "";
      Path outFilePath;
      Path argsPath;
      List<String> orcFiles;
      if(args.length == 0) {
        directory = currentYear + "/" + currentMonth + "/" + currentDay;
        outFilePath = new Path(allHostsPath.get(i) + "/" + directory + "/" + outFile);
        try {
          orcFiles = getAllFilePath(new Path(allHostsPath.get(i) + "/" + directory), fileSystem);
        } catch (Exception e) {
          continue;
        }
      } else {
        outFilePath = new Path(args[0] + "/" + outFile);
        argsPath = new Path(args[0]);
        try {
          orcFiles = getAllFilePath(argsPath, fileSystem);
        } catch (Exception e) {
          continue;
        }
      }
      //Create List of files in the directory
      FileSystem fs = outFilePath.getFileSystem(conf);
      //Writer MUST be below ^^ or the combination file will be deleted as well.
      if(fs.exists(outFilePath)) {
        System.out.println(outFilePath + " exists, delete before continuing.");
      } else {
       writer = OrcFile.createWriter(outFilePath, OrcFile.writerOptions(conf)
            .setSchema(schema));
      }
      for(int j = 0; j < orcFiles.size(); j++ ) { 
        Reader reader = OrcFile.createReader(new Path(orcFiles.get(j)), OrcFile.readerOptions(conf));
        VectorizedRowBatch batch = reader.getSchema().createRowBatch();
        RecordReader rows = reader.rows();
        while (rows.nextBatch(batch)) {
          if (batch != null) {
             writer.addRowBatch(batch);
          }
        }
        rows.close();
        fs.delete(new Path(orcFiles.get(j)), false);
      }
      //Close File
      writer.close();
      //Remove leading "." from ORC file to make visible to Hive
      outFile = fileSystem.getFileStatus(outFilePath)
                                      .getPath()
                                      .getName();
      if (outFile.startsWith(".")) {
        outFile = outFile.substring(1);
        int lastIndexOf = outFile.lastIndexOf(".working");
        outFile = outFile.substring(0, lastIndexOf);
      }
      Path parent = outFilePath.getParent();
      fileSystem.rename(outFilePath, new Path(parent, outFile));
      if(args.length != 0)
        break;
    }
  }
  private static String getSchema(String resource) throws IOException {
    try (InputStream input = OrcFileRollUp.class.getResourceAsStream("/" + resource)) {
      return IOUtils.toString(input, UTF_8);
    }
  }
  public static List<String> getHosts(Path filePath, FileSystem fs) throws FileNotFoundException, IOException {
    List<String> hostsList = new ArrayList<String>();
    FileStatus[] fileStatus = fs.listStatus(filePath);
    for (FileStatus fileStat : fileStatus) {
      hostsList.add(fileStat.getPath().toString());
    }
    return hostsList;
  }
  private static List<String> getAllFilePath(Path filePath, FileSystem fs) throws FileNotFoundException, IOException {
    List<String> fileList = new ArrayList<String>();
    FileStatus[] fileStatus = fs.listStatus(filePath);
    for (FileStatus fileStat : fileStatus) {
      if (fileStat.isDirectory()) {
        fileList.addAll(getAllFilePath(fileStat.getPath(), fs));
      } else {
        fileList.add(fileStat.getPath()
                             .toString());
      }
    }
    for(int i = 0; i< fileList.size(); i++) {
      if(!fileList.get(i).endsWith(".orc"))
        fileList.remove(i);
    }
    return fileList;
  }
}

这是python中的一个小脚本,使用pyorc将小兽人文件串在一起。我知道它不会直接回答您的问题,因为它不在Java中,但是我发现它比当前解决方案更简单,而不是使用Hive。

import pyorc
import argparse

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-o', '--output', type=argparse.FileType(mode='wb'))
    parser.add_argument('files', type=argparse.FileType(mode='rb'), nargs='+')
    args = parser.parse_args()
    schema = str(pyorc.Reader(args.files[0]).schema)
    with pyorc.Writer(args.output, schema) as writer:
        for i, f in enumerate(args.files):
            reader = pyorc.Reader(f)
            if str(reader.schema) != schema:
                raise RuntimeError(
                    "Inconsistent ORC schemas.n"
                    "tFirst file schema: {}n"
                    "tFile #{} schema: {}"
                    .format(schema, i, str(reader.schema))
                )
            for line in reader:
                writer.write(line)

if __name__ == '__main__':
    main()

最新更新