|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hudi.sync.adb; |
| 20 | + |
| 21 | +import org.apache.hudi.common.fs.FSUtils; |
| 22 | +import org.apache.hudi.common.table.timeline.HoodieTimeline; |
| 23 | +import org.apache.hudi.common.util.StringUtils; |
| 24 | +import org.apache.hudi.exception.HoodieException; |
| 25 | +import org.apache.hudi.hive.PartitionValueExtractor; |
| 26 | +import org.apache.hudi.hive.SchemaDifference; |
| 27 | +import org.apache.hudi.sync.common.AbstractSyncHoodieClient; |
| 28 | + |
| 29 | +import org.apache.hadoop.fs.FileSystem; |
| 30 | +import org.apache.hadoop.fs.Path; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | + |
| 37 | +public abstract class AbstractAdbSyncHoodieClient extends AbstractSyncHoodieClient { |
| 38 | + protected AdbSyncConfig adbSyncConfig; |
| 39 | + protected PartitionValueExtractor partitionValueExtractor; |
| 40 | + protected HoodieTimeline activeTimeline; |
| 41 | + |
| 42 | + public AbstractAdbSyncHoodieClient(AdbSyncConfig syncConfig, FileSystem fs) { |
| 43 | + super(syncConfig.basePath, syncConfig.assumeDatePartitioning, |
| 44 | + syncConfig.useFileListingFromMetadata, false, fs); |
| 45 | + this.adbSyncConfig = syncConfig; |
| 46 | + final String clazz = adbSyncConfig.partitionValueExtractorClass; |
| 47 | + try { |
| 48 | + this.partitionValueExtractor = (PartitionValueExtractor) Class.forName(clazz).newInstance(); |
| 49 | + } catch (Exception e) { |
| 50 | + throw new HoodieException("Fail to init PartitionValueExtractor class " + clazz, e); |
| 51 | + } |
| 52 | + |
| 53 | + activeTimeline = metaClient.getActiveTimeline().getCommitsTimeline().filterCompletedInstants(); |
| 54 | + } |
| 55 | + |
| 56 | + public List<PartitionEvent> getPartitionEvents(Map<List<String>, String> tablePartitions, |
| 57 | + List<String> partitionStoragePartitions) { |
| 58 | + Map<String, String> paths = new HashMap<>(); |
| 59 | + |
| 60 | + for (Map.Entry<List<String>, String> entry : tablePartitions.entrySet()) { |
| 61 | + List<String> partitionValues = entry.getKey(); |
| 62 | + String fullTablePartitionPath = entry.getValue(); |
| 63 | + paths.put(String.join(", ", partitionValues), fullTablePartitionPath); |
| 64 | + } |
| 65 | + List<PartitionEvent> events = new ArrayList<>(); |
| 66 | + for (String storagePartition : partitionStoragePartitions) { |
| 67 | + Path storagePartitionPath = FSUtils.getPartitionPath(adbSyncConfig.basePath, storagePartition); |
| 68 | + String fullStoragePartitionPath = Path.getPathWithoutSchemeAndAuthority(storagePartitionPath).toUri().getPath(); |
| 69 | + // Check if the partition values or if hdfs path is the same |
| 70 | + List<String> storagePartitionValues = partitionValueExtractor.extractPartitionValuesInPath(storagePartition); |
| 71 | + if (adbSyncConfig.useHiveStylePartitioning) { |
| 72 | + String partition = String.join("/", storagePartitionValues); |
| 73 | + storagePartitionPath = FSUtils.getPartitionPath(adbSyncConfig.basePath, partition); |
| 74 | + fullStoragePartitionPath = Path.getPathWithoutSchemeAndAuthority(storagePartitionPath).toUri().getPath(); |
| 75 | + } |
| 76 | + if (!storagePartitionValues.isEmpty()) { |
| 77 | + String storageValue = String.join(", ", storagePartitionValues); |
| 78 | + if (!paths.containsKey(storageValue)) { |
| 79 | + events.add(PartitionEvent.newPartitionAddEvent(storagePartition)); |
| 80 | + } else if (!paths.get(storageValue).equals(fullStoragePartitionPath)) { |
| 81 | + events.add(PartitionEvent.newPartitionUpdateEvent(storagePartition)); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + return events; |
| 86 | + } |
| 87 | + |
| 88 | + public void close() { |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + public abstract Map<List<String>, String> scanTablePartitions(String tableName) throws Exception; |
| 93 | + |
| 94 | + public abstract void updateTableDefinition(String tableName, SchemaDifference schemaDiff) throws Exception; |
| 95 | + |
| 96 | + public abstract boolean databaseExists(String databaseName) throws Exception; |
| 97 | + |
| 98 | + public abstract void createDatabase(String databaseName) throws Exception; |
| 99 | + |
| 100 | + public abstract void dropTable(String tableName); |
| 101 | + |
| 102 | + protected String getDatabasePath() { |
| 103 | + String dbLocation = adbSyncConfig.dbLocation; |
| 104 | + Path dbLocationPath; |
| 105 | + if (StringUtils.isNullOrEmpty(dbLocation)) { |
| 106 | + if (new Path(adbSyncConfig.basePath).isRoot()) { |
| 107 | + dbLocationPath = new Path(adbSyncConfig.basePath); |
| 108 | + } else { |
| 109 | + dbLocationPath = new Path(adbSyncConfig.basePath).getParent(); |
| 110 | + } |
| 111 | + } else { |
| 112 | + dbLocationPath = new Path(dbLocation); |
| 113 | + } |
| 114 | + return generateAbsolutePathStr(dbLocationPath); |
| 115 | + } |
| 116 | + |
| 117 | + protected String generateAbsolutePathStr(Path path) { |
| 118 | + String absolutePathStr = path.toString(); |
| 119 | + if (path.toUri().getScheme() == null) { |
| 120 | + absolutePathStr = getDefaultFs() + absolutePathStr; |
| 121 | + } |
| 122 | + return absolutePathStr.endsWith("/") ? absolutePathStr : absolutePathStr + "/"; |
| 123 | + } |
| 124 | + |
| 125 | + protected String getDefaultFs() { |
| 126 | + return fs.getConf().get("fs.defaultFS"); |
| 127 | + } |
| 128 | +} |
0 commit comments