Apache spark 从Spark数据集中获取配置单元分区

Apache spark 从Spark数据集中获取配置单元分区,apache-spark,hive,Apache Spark,Hive,我正在做一件事,在写入S3之后,它会自动将表和分区注册到配置单元元存储中 在注册所有分区之前,我需要知道所有分区值。现在我正在做ds.select(partitionColumn.distinct().collectAsList()以获取所有分区值 有没有更好的方法从我的数据集中获取分区值 阅读Spark源代码后,特别是org.apache.Spark.sql.execution.command.ddl.scala中的AlterTableRecoverPartitionsCommand,它是Al

我正在做一件事,在写入S3之后,它会自动将表和分区注册到配置单元元存储中

在注册所有分区之前,我需要知道所有分区值。现在我正在做
ds.select(partitionColumn.distinct().collectAsList()
以获取所有分区值


有没有更好的方法从我的数据集中获取分区值

阅读Spark源代码后,特别是
org.apache.Spark.sql.execution.command.ddl.scala
中的
AlterTableRecoverPartitionsCommand
,它是
AlterTable RECOVER PARTITIONS
的Spark实现。扫描所有分区,然后注册它们

所以,这里是相同的想法,从我们刚刚写入的位置扫描所有分区

从中获取密钥名,然后从中提取分区名/值

下面是获取路径的代码片段

String location = "s3n://somebucket/somefolder/dateid=20171010/";
Path root = new Path(location);

Configuration hadoopConf = sparkSession.sessionState().newHadoopConf();
FileSystem fs = root.getFileSystem(hadoopConf);

JobConf jobConf = new JobConf(hadoopConf, this.getClass());
final PathFilter pathFilter = FileInputFormat.getInputPathFilter(jobConf);

FileStatus[] fileStatuses = fs.listStatus(root, path -> {
    String name = path.getName();
    if (name != "_SUCCESS" && name != "_temporary" && !name.startsWith(".")) {
        return pathFilter == null || pathFilter.accept(path);
    } else {
        return false;
    }
});

for(FileStatus fileStatus: fileStatuses) {
    System.out.println(fileStatus.getPath().getName());
}

AWS胶水已经为你做了。我不知道有更好的解决方案,我也是这样做的it@ThiagoBaldim我们已经看过AWS胶水,但它似乎不允许我们将其用作外部产品的元存储服务。像画面、数据记录等…@RaphaelRoth yep,它很有效。但如果数据集很大,则需要一段时间才能完成。我想知道,因为我首先调用了
ds.write.partitionBy.save
,它已经将数据写入了所有分区。不过,我找到了一个方法。这是真的,这可以让你轻松地使用电子病历。但是如果你需要从中获取信息,你可以尝试在Boto之上构建一些东西。基于这种方法,我们可以扩展当前的过滤器来完成额外的工作。折衷的办法是,如果SaveMode没有被覆盖,那么我们返回的路径与我们写入的路径并不完全相同。在我的例子中,我现在只将其用于覆盖模式。