-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-5105] Add Call show_commit_extra_metadata for spark sql #7091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
...scala/org/apache/spark/sql/hudi/command/procedures/ShowCommitExtraMetadataProcedure.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.hudi.command.procedures | ||
|
|
||
| import org.apache.hudi.HoodieCLIUtils | ||
| import org.apache.hudi.common.model.{HoodieCommitMetadata, HoodieReplaceCommitMetadata} | ||
| import org.apache.hudi.common.table.HoodieTableMetaClient | ||
| import org.apache.hudi.common.table.timeline.{HoodieInstant, HoodieTimeline} | ||
| import org.apache.hudi.exception.HoodieException | ||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.types.{DataTypes, Metadata, StructField, StructType} | ||
|
|
||
| import java.util | ||
| import java.util.function.Supplier | ||
| import scala.collection.JavaConversions._ | ||
|
|
||
| class ShowCommitExtraMetadataProcedure() extends BaseProcedure with ProcedureBuilder { | ||
| private val PARAMETERS = Array[ProcedureParameter]( | ||
| ProcedureParameter.required(0, "table", DataTypes.StringType, None), | ||
| ProcedureParameter.optional(1, "limit", DataTypes.IntegerType, 100), | ||
| ProcedureParameter.optional(2, "instant_time", DataTypes.StringType, None), | ||
| ProcedureParameter.optional(3, "metadata_key", DataTypes.StringType, None) | ||
| ) | ||
|
|
||
| private val OUTPUT_TYPE = new StructType(Array[StructField]( | ||
| StructField("instant_time", DataTypes.StringType, nullable = true, Metadata.empty), | ||
| StructField("action", DataTypes.StringType, nullable = true, Metadata.empty), | ||
| StructField("metadata_key", DataTypes.StringType, nullable = true, Metadata.empty), | ||
| StructField("metadata_value", DataTypes.StringType, nullable = true, Metadata.empty) | ||
| )) | ||
|
|
||
| def parameters: Array[ProcedureParameter] = PARAMETERS | ||
|
|
||
| def outputType: StructType = OUTPUT_TYPE | ||
|
|
||
| override def call(args: ProcedureArgs): Seq[Row] = { | ||
| super.checkArgs(PARAMETERS, args) | ||
|
|
||
| val table = getArgValueOrDefault(args, PARAMETERS(0)).get.asInstanceOf[String] | ||
| val limit = getArgValueOrDefault(args, PARAMETERS(1)).get.asInstanceOf[Int] | ||
| val instantTime = getArgValueOrDefault(args, PARAMETERS(2)) | ||
| val metadataKey = getArgValueOrDefault(args, PARAMETERS(3)) | ||
|
|
||
| val hoodieCatalogTable = HoodieCLIUtils.getHoodieCatalogTable(sparkSession, table) | ||
| val basePath = hoodieCatalogTable.tableLocation | ||
| val metaClient = HoodieTableMetaClient.builder.setConf(jsc.hadoopConfiguration()).setBasePath(basePath).build | ||
| val activeTimeline = metaClient.getActiveTimeline | ||
| val timeline = activeTimeline.getCommitsTimeline.filterCompletedInstants | ||
|
|
||
| val hoodieInstantOption: Option[HoodieInstant] = if (instantTime.isEmpty) { | ||
| getCommitForLastInstant(timeline) | ||
| } else { | ||
| getCommitForInstant(timeline, instantTime.get.asInstanceOf[String]) | ||
| } | ||
|
|
||
| if (hoodieInstantOption.isEmpty) { | ||
| throw new HoodieException(s"Commit $instantTime not found in Commits $timeline.") | ||
| } | ||
|
|
||
| val commitMetadataOptional = getHoodieCommitMetadata(timeline, hoodieInstantOption) | ||
|
|
||
| if (commitMetadataOptional.isEmpty) { | ||
| throw new HoodieException(s"Commit $instantTime not found commitMetadata in Commits $timeline.") | ||
| } | ||
|
|
||
| val meta = commitMetadataOptional.get | ||
| val timestamp: String = hoodieInstantOption.get.getTimestamp | ||
| val action: String = hoodieInstantOption.get.getAction | ||
| val metadatas: util.Map[String, String] = if (metadataKey.isEmpty) { | ||
| meta.getExtraMetadata | ||
| } else { | ||
| meta.getExtraMetadata.filter(r => r._1.equals(metadataKey.get.asInstanceOf[String].trim)) | ||
| } | ||
|
|
||
| val rows = new util.ArrayList[Row] | ||
| metadatas.foreach(r => rows.add(Row(timestamp, action, r._1, r._2))) | ||
| rows.stream().limit(limit).toArray().map(r => r.asInstanceOf[Row]).toList | ||
| } | ||
|
|
||
| override def build: Procedure = new ShowCommitExtraMetadataProcedure() | ||
|
|
||
| private def getCommitForLastInstant(timeline: HoodieTimeline): Option[HoodieInstant] = { | ||
| val instantOptional = timeline.getReverseOrderedInstants | ||
| .findFirst | ||
| if (instantOptional.isPresent) { | ||
| Option.apply(instantOptional.get()) | ||
| } else { | ||
| Option.empty | ||
| } | ||
| } | ||
|
|
||
| private def getCommitForInstant(timeline: HoodieTimeline, instantTime: String): Option[HoodieInstant] = { | ||
| val instants: util.List[HoodieInstant] = util.Arrays.asList( | ||
| new HoodieInstant(false, HoodieTimeline.COMMIT_ACTION, instantTime), | ||
| new HoodieInstant(false, HoodieTimeline.REPLACE_COMMIT_ACTION, instantTime), | ||
| new HoodieInstant(false, HoodieTimeline.DELTA_COMMIT_ACTION, instantTime)) | ||
|
|
||
| val hoodieInstant: Option[HoodieInstant] = instants.find((i: HoodieInstant) => timeline.containsInstant(i)) | ||
| hoodieInstant | ||
| } | ||
|
|
||
| private def getHoodieCommitMetadata(timeline: HoodieTimeline, hoodieInstant: Option[HoodieInstant]): Option[HoodieCommitMetadata] = { | ||
| if (hoodieInstant.isDefined) { | ||
| if (hoodieInstant.get.getAction == HoodieTimeline.REPLACE_COMMIT_ACTION) { | ||
| Option(HoodieReplaceCommitMetadata.fromBytes(timeline.getInstantDetails(hoodieInstant.get).get, | ||
| classOf[HoodieReplaceCommitMetadata])) | ||
| } else { | ||
| Option(HoodieCommitMetadata.fromBytes(timeline.getInstantDetails(hoodieInstant.get).get, | ||
| classOf[HoodieCommitMetadata])) | ||
| } | ||
| } else { | ||
| Option.empty | ||
| } | ||
| } | ||
| } | ||
|
|
||
| object ShowCommitExtraMetadataProcedure { | ||
| val NAME = "show_commit_extra_metadata" | ||
|
|
||
| def builder: Supplier[ProcedureBuilder] = new Supplier[ProcedureBuilder] { | ||
| override def get() = new ShowCommitExtraMetadataProcedure() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi,Can we put the limit in the foreach statement?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be done, but the judgment logic will be more complicated, and the number of parameters in this will not be too many.