Skip to content

Commit

Permalink
[b/343069385] Extract foreign keys from HMS (#415)
Browse files Browse the repository at this point in the history
Extracted foreign keys are added to the existing file `tables-raw.jsonl` as a new field `foreignKeys`.
  • Loading branch information
dawidxc authored Jun 6, 2024
1 parent c0e8809 commit 27d24a0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,15 @@ private void dumpTable(
Table table = thriftClient.getTable(databaseName, tableName);
TBase<?, ?> rawTableThriftObject = table.getRawThriftObject();
ImmutableList<? extends TBase<?, ?>> primaryKeys = table.getRawPrimaryKeys();
ImmutableList<? extends TBase<?, ?>> foreignKeys = table.getRawForeignKeys();
ThriftJsonSerializer jsonSerializer = new ThriftJsonSerializer();
synchronized (writer) {
writer.write("{\"table\":");
writer.write(jsonSerializer.serialize(rawTableThriftObject));
writer.write(",\"primaryKeys\":");
jsonSerializer.serialize(primaryKeys, writer);
writer.write(",\"foreignKeys\":");
jsonSerializer.serialize(foreignKeys, writer);
writer.write("}");
writer.write('\n');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.edwmigration.dumper.ext.hive.metastore.thrift.api.superset.FieldSchema;
import com.google.edwmigration.dumper.ext.hive.metastore.thrift.api.superset.ForeignKeysRequest;
import com.google.edwmigration.dumper.ext.hive.metastore.thrift.api.superset.GetCatalogRequest;
import com.google.edwmigration.dumper.ext.hive.metastore.thrift.api.superset.GetCatalogsResponse;
import com.google.edwmigration.dumper.ext.hive.metastore.thrift.api.superset.PrimaryKeysRequest;
Expand Down Expand Up @@ -482,6 +483,19 @@ public Boolean isCompressed() {
.get_primary_keys(new PrimaryKeysRequest(databaseName, tableName))
.getPrimaryKeys());
}

@Override
public ImmutableList<? extends TBase<?, ?>> getRawForeignKeys() throws Exception {
return ImmutableList.copyOf(
client
.get_foreign_keys(
new ForeignKeysRequest(
/*parent_db_name=*/ null,
/*parent_tbl_name=*/ null,
databaseName,
tableName))
.getForeignKeys());
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.edwmigration.dumper.ext.hive.metastore.thrift.api.v2_3_6.FieldSchema;
import com.google.edwmigration.dumper.ext.hive.metastore.thrift.api.v2_3_6.ForeignKeysRequest;
import com.google.edwmigration.dumper.ext.hive.metastore.thrift.api.v2_3_6.PrimaryKeysRequest;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -478,6 +479,19 @@ public Boolean isCompressed() {
.get_primary_keys(new PrimaryKeysRequest(databaseName, tableName))
.getPrimaryKeys());
}

@Override
public ImmutableList<? extends TBase<?, ?>> getRawForeignKeys() throws Exception {
return ImmutableList.copyOf(
client
.get_foreign_keys(
new ForeignKeysRequest(
/*parent_db_name=*/ null,
/*parent_tbl_name=*/ null,
databaseName,
tableName))
.getForeignKeys());
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,6 @@ public interface Table {
TBase<?, ?> getRawThriftObject();

ImmutableList<? extends TBase<?, ?>> getRawPrimaryKeys() throws Exception;

ImmutableList<? extends TBase<?, ?>> getRawForeignKeys() throws Exception;
}

0 comments on commit 27d24a0

Please sign in to comment.