Skip to content

Commit 97b3c17

Browse files
authored
[b/343058608] Extract master keys from HMS (GoogleCloudPlatform#401)
Extract master keys and save them in the `master-keys.csv` file.
1 parent bccf064 commit 97b3c17

5 files changed

Lines changed: 52 additions & 1 deletion

File tree

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/connector/hive/HiveMetadataConnector.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.List;
4646
import java.util.function.Predicate;
4747
import javax.annotation.Nonnull;
48+
import org.apache.commons.csv.CSVFormat;
4849
import org.apache.commons.csv.CSVPrinter;
4950
import org.apache.commons.lang3.BooleanUtils;
5051
import org.slf4j.Logger;
@@ -370,6 +371,42 @@ protected String toCallDescription() {
370371
}
371372
}
372373

374+
private static class MasterKeysTask extends AbstractHiveMetadataTask {
375+
private static final CSVFormat MASTER_KEYS_FORMAT =
376+
FORMAT.builder().setHeader("MasterKey").build();
377+
378+
private MasterKeysTask() {
379+
super("master-keys.csv", unused -> true);
380+
}
381+
382+
@Override
383+
protected void run(@Nonnull Writer writer, @Nonnull ThriftClientHandle thriftClientHandle)
384+
throws Exception {
385+
try (HiveMetastoreThriftClient client =
386+
thriftClientHandle.newClient("master-keys-task-client");
387+
RecordProgressMonitor monitor =
388+
new RecordProgressMonitor("Writing Master Keys to " + getTargetPath())) {
389+
ImmutableList<String> masterKeys = client.getMasterKeys();
390+
try (CSVPrinter printer = MASTER_KEYS_FORMAT.print(writer)) {
391+
for (String masterKey : masterKeys) {
392+
monitor.count();
393+
printer.printRecord(masterKey);
394+
}
395+
}
396+
}
397+
}
398+
399+
@Override
400+
public TaskCategory getCategory() {
401+
return TaskCategory.OPTIONAL;
402+
}
403+
404+
@Override
405+
protected String toCallDescription() {
406+
return "get_master_keys()";
407+
}
408+
}
409+
373410
public HiveMetadataConnector() {
374411
super("hiveql");
375412
}
@@ -389,6 +426,7 @@ public void addTasksTo(List<? super Task<?>> out, @Nonnull ConnectorArguments ar
389426
arguments.getDefinitionOrDefault(HiveConnectorProperty.MIGRATION_METADATA))) {
390427
out.add(new CatalogsTask());
391428
out.add(new DatabasesJsonlTask());
429+
out.add(new MasterKeysTask());
392430
}
393431

394432
if (arguments.isAssessment()) {

dumper/app/src/test/java/com/google/edwmigration/dumper/application/dumper/connector/hive/HiveMetadataConnectorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void addTasksTo_taskExists_success(
102102

103103
@DataPoints("migrationMetadataTaskNames")
104104
public static final ImmutableList<String> EXPECTED_TASK_NAMES_WITH_MIGRATION_METADATA_ENABLED =
105-
ImmutableList.of("catalogs.jsonl", "databases.jsonl");
105+
ImmutableList.of("catalogs.jsonl", "databases.jsonl", "master-keys.csv");
106106

107107
@Theory
108108
public void addTasksTo_migrationMetadataTaskExists_success(

dumper/lib-ext-hive-metastore/src/main/java/com/google/edwmigration/dumper/ext/hive/metastore/HiveMetastoreThriftClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ public String getName() {
240240
@Nonnull
241241
public abstract ImmutableList<String> getDatabasesAsJsonl() throws Exception;
242242

243+
@Nonnull
244+
public abstract ImmutableList<String> getMasterKeys() throws Exception;
245+
243246
@Nonnull
244247
public abstract ImmutableList<String> getAllTableNamesInDatabase(@Nonnull String databaseName)
245248
throws Exception;

dumper/lib-ext-hive-metastore/src/main/java/com/google/edwmigration/dumper/ext/hive/metastore/HiveMetastoreThriftClient_Superset.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ public ImmutableList<String> getDatabasesAsJsonl() throws Exception {
130130
.collect(toImmutableList());
131131
}
132132

133+
@Override
134+
public ImmutableList<String> getMasterKeys() throws Exception {
135+
return ImmutableList.copyOf(client.get_master_keys());
136+
}
137+
133138
@Nonnull
134139
@Override
135140
public ImmutableList<String> getAllTableNamesInDatabase(@Nonnull String databaseName)

dumper/lib-ext-hive-metastore/src/main/java/com/google/edwmigration/dumper/ext/hive/metastore/HiveMetastoreThriftClient_v2_3_6.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public ImmutableList<String> getDatabasesAsJsonl() throws Exception {
127127
.collect(toImmutableList());
128128
}
129129

130+
@Override
131+
public ImmutableList<String> getMasterKeys() throws Exception {
132+
return ImmutableList.copyOf(client.get_master_keys());
133+
}
134+
130135
@Nonnull
131136
@Override
132137
public ImmutableList<String> getAllTableNamesInDatabase(@Nonnull String databaseName)

0 commit comments

Comments
 (0)