-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add a metadata table showing commits in Hudi connector #16181
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
2 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
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
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
84 changes: 84 additions & 0 deletions
84
plugin/trino-hudi/src/main/java/io/trino/plugin/hudi/HudiTableName.java
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,84 @@ | ||
| /* | ||
| * Licensed 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 io.trino.plugin.hudi; | ||
|
|
||
| import io.trino.spi.TrinoException; | ||
|
|
||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED; | ||
| import static java.lang.String.format; | ||
| import static java.util.Locale.ENGLISH; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class HudiTableName | ||
| { | ||
| private static final Pattern TABLE_PATTERN = Pattern.compile("" + | ||
| "(?<table>[^$@]+)" + | ||
| "(?:\\$(?<type>[^@]+))?"); | ||
|
|
||
| private final String tableName; | ||
| private final TableType tableType; | ||
|
|
||
| public HudiTableName(String tableName, TableType tableType) | ||
| { | ||
| this.tableName = requireNonNull(tableName, "tableName is null"); | ||
| this.tableType = requireNonNull(tableType, "tableType is null"); | ||
| } | ||
|
|
||
| public String getTableName() | ||
| { | ||
| return tableName; | ||
| } | ||
|
|
||
| public TableType getTableType() | ||
| { | ||
| return tableType; | ||
| } | ||
|
|
||
| public String getTableNameWithType() | ||
| { | ||
| return tableName + "$" + tableType.name().toLowerCase(ENGLISH); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() | ||
| { | ||
| return getTableNameWithType(); | ||
| } | ||
|
|
||
| public static HudiTableName from(String name) | ||
| { | ||
| Matcher match = TABLE_PATTERN.matcher(name); | ||
| if (!match.matches()) { | ||
| throw new TrinoException(NOT_SUPPORTED, "Invalid Hudi table name: " + name); | ||
| } | ||
|
|
||
| String table = match.group("table"); | ||
| String typeString = match.group("type"); | ||
|
|
||
| TableType type = TableType.DATA; | ||
| if (typeString != null) { | ||
| try { | ||
| type = TableType.valueOf(typeString.toUpperCase(ENGLISH)); | ||
| } | ||
| catch (IllegalArgumentException e) { | ||
| throw new TrinoException(NOT_SUPPORTED, format("Invalid Hudi table name (unknown type '%s'): %s", typeString, name)); | ||
| } | ||
| } | ||
|
|
||
| return new HudiTableName(table, type); | ||
| } | ||
| } |
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
20 changes: 20 additions & 0 deletions
20
plugin/trino-hudi/src/main/java/io/trino/plugin/hudi/TableType.java
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,20 @@ | ||
| /* | ||
| * Licensed 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 io.trino.plugin.hudi; | ||
|
|
||
| public enum TableType | ||
| { | ||
| DATA, | ||
| TIMELINE; | ||
| } |
91 changes: 91 additions & 0 deletions
91
plugin/trino-hudi/src/main/java/io/trino/plugin/hudi/TimelineTable.java
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,91 @@ | ||
| /* | ||
| * Licensed 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 io.trino.plugin.hudi; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import io.trino.plugin.hive.metastore.Table; | ||
| import io.trino.spi.connector.ColumnMetadata; | ||
| import io.trino.spi.connector.ConnectorSession; | ||
| import io.trino.spi.connector.ConnectorTableMetadata; | ||
| import io.trino.spi.connector.ConnectorTransactionHandle; | ||
| import io.trino.spi.connector.InMemoryRecordSet; | ||
| import io.trino.spi.connector.RecordCursor; | ||
| import io.trino.spi.connector.SchemaTableName; | ||
| import io.trino.spi.connector.SystemTable; | ||
| import io.trino.spi.predicate.TupleDomain; | ||
| import io.trino.spi.type.Type; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hudi.common.table.HoodieTableMetaClient; | ||
| import org.apache.hudi.common.table.timeline.HoodieInstant; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
| import static io.trino.plugin.hudi.HudiUtil.buildTableMetaClient; | ||
| import static io.trino.spi.type.VarcharType.VARCHAR; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class TimelineTable | ||
| implements SystemTable | ||
| { | ||
| private final ConnectorTableMetadata tableMetadata; | ||
| private final List<Type> types; | ||
| private final Configuration configuration; | ||
| private final String location; | ||
|
|
||
| public TimelineTable(Configuration configuration, SchemaTableName tableName, Table hudiTable) | ||
| { | ||
| this.tableMetadata = new ConnectorTableMetadata(requireNonNull(tableName, "tableName is null"), | ||
| ImmutableList.<ColumnMetadata>builder() | ||
| .add(new ColumnMetadata("timestamp", VARCHAR)) | ||
| .add(new ColumnMetadata("action", VARCHAR)) | ||
| .add(new ColumnMetadata("state", VARCHAR)) | ||
| .build()); | ||
| this.types = tableMetadata.getColumns().stream().map(ColumnMetadata::getType).collect(toImmutableList()); | ||
| this.configuration = requireNonNull(configuration, "configuration is null"); | ||
| this.location = requireNonNull(hudiTable.getStorage().getLocation(), "location is null"); | ||
| } | ||
|
|
||
| @Override | ||
| public Distribution getDistribution() | ||
| { | ||
| return Distribution.SINGLE_COORDINATOR; | ||
| } | ||
|
|
||
| @Override | ||
| public ConnectorTableMetadata getTableMetadata() | ||
| { | ||
| return tableMetadata; | ||
| } | ||
|
|
||
| @Override | ||
| public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint) | ||
| { | ||
| HoodieTableMetaClient metaClient = buildTableMetaClient(configuration, location); | ||
| Iterable<List<Object>> records = () -> metaClient.getCommitsTimeline().getInstants().map(this::getRecord).iterator(); | ||
| return new InMemoryRecordSet(types, records).cursor(); | ||
| } | ||
|
|
||
| private List<Object> getRecord(HoodieInstant hudiInstant) | ||
| { | ||
| List<Object> columns = new ArrayList<>(); | ||
| columns.add(hudiInstant.getTimestamp()); | ||
| columns.add(hudiInstant.getAction()); | ||
| columns.add(hudiInstant.getState().toString()); | ||
| checkArgument(columns.size() == types.size(), "Expected %s types in row, but got %s values", types.size(), columns.size()); | ||
| return columns; | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.