in v1/src/main/java/com/google/cloud/teleport/spanner/AvroSchemaToDdlConverter.java [147:377]
public PropertyGraph toPropertyGraph(String propertyGraphName, Schema schema) {
PropertyGraph.Builder propertyGraphBuilder = PropertyGraph.builder();
if (propertyGraphName == null) {
propertyGraphName = getSpannerObjectName(schema);
}
LOG.debug("Converting to Ddl propertyGraphName {}", propertyGraphName);
propertyGraphBuilder.name(propertyGraphName);
// Deserialize nodeTables
int nodeTableCount = 0;
while (schema.getProp(SPANNER_NODE_TABLE + "_" + nodeTableCount + "_NAME") != null) {
GraphElementTable.Builder nodeTableBuilder = GraphElementTable.builder();
nodeTableBuilder.name(schema.getProp(SPANNER_NODE_TABLE + "_" + nodeTableCount + "_NAME"));
nodeTableBuilder.baseTableName(
schema.getProp(SPANNER_NODE_TABLE + "_" + nodeTableCount + "_BASE_TABLE_NAME"));
nodeTableBuilder.kind(Kind.NODE);
// Deserialize keyColumns
String[] keyColumns =
schema
.getProp(SPANNER_NODE_TABLE + "_" + nodeTableCount + "_KEY_COLUMNS")
.trim()
.split(",");
nodeTableBuilder.keyColumns(ImmutableList.copyOf(keyColumns));
// Deserialize labelToPropertyDefinitions
int labelCount = 0;
ImmutableList.Builder<GraphElementTable.LabelToPropertyDefinitions> labelsBuilder =
ImmutableList.builder();
while (schema.getProp(
SPANNER_NODE_TABLE + "_" + nodeTableCount + "_LABEL_" + labelCount + "_NAME")
!= null) {
String labelName =
schema.getProp(
SPANNER_NODE_TABLE + "_" + nodeTableCount + "_LABEL_" + labelCount + "_NAME");
ImmutableList.Builder<GraphElementTable.PropertyDefinition> propertyDefinitionsBuilder =
ImmutableList.builder();
int propertyCount = 0;
while (schema.getProp(
SPANNER_NODE_TABLE
+ "_"
+ nodeTableCount
+ "_LABEL_"
+ labelCount
+ "_PROPERTY_"
+ propertyCount
+ "_NAME")
!= null) {
String propertyName =
schema.getProp(
SPANNER_NODE_TABLE
+ "_"
+ nodeTableCount
+ "_LABEL_"
+ labelCount
+ "_PROPERTY_"
+ propertyCount
+ "_NAME");
String propertyValue =
schema.getProp(
SPANNER_NODE_TABLE
+ "_"
+ nodeTableCount
+ "_LABEL_"
+ labelCount
+ "_PROPERTY_"
+ propertyCount
+ "_VALUE");
propertyDefinitionsBuilder.add(
new GraphElementTable.PropertyDefinition(propertyName, propertyValue));
propertyCount++;
}
labelsBuilder.add(
new GraphElementTable.LabelToPropertyDefinitions(
labelName, propertyDefinitionsBuilder.build()));
labelCount++;
}
nodeTableBuilder.labelToPropertyDefinitions(labelsBuilder.build());
propertyGraphBuilder.addNodeTable(nodeTableBuilder.autoBuild());
nodeTableCount++;
}
// Deserialize edgeTables (similar logic to nodeTables)
int edgeTableCount = 0;
while (schema.getProp(SPANNER_EDGE_TABLE + "_" + edgeTableCount + "_NAME") != null) {
GraphElementTable.Builder edgeTableBuilder = GraphElementTable.builder();
edgeTableBuilder.name(schema.getProp(SPANNER_EDGE_TABLE + "_" + edgeTableCount + "_NAME"));
edgeTableBuilder.baseTableName(
schema.getProp(SPANNER_EDGE_TABLE + "_" + edgeTableCount + "_BASE_TABLE_NAME"));
edgeTableBuilder.kind(Kind.EDGE);
// Deserialize keyColumns
String[] keyColumns =
schema
.getProp(SPANNER_EDGE_TABLE + "_" + edgeTableCount + "_KEY_COLUMNS")
.trim()
.split(",");
edgeTableBuilder.keyColumns(ImmutableList.copyOf(keyColumns));
// Deserialize labelToPropertyDefinitions
int labelCount = 0;
ImmutableList.Builder<GraphElementTable.LabelToPropertyDefinitions> labelsBuilder =
ImmutableList.builder();
while (schema.getProp(
SPANNER_EDGE_TABLE + "_" + edgeTableCount + "_LABEL_" + labelCount + "_NAME")
!= null) {
String labelName =
schema.getProp(
SPANNER_EDGE_TABLE + "_" + edgeTableCount + "_LABEL_" + labelCount + "_NAME");
ImmutableList.Builder<GraphElementTable.PropertyDefinition> propertyDefinitionsBuilder =
ImmutableList.builder();
int propertyCount = 0;
while (schema.getProp(
SPANNER_EDGE_TABLE
+ "_"
+ edgeTableCount
+ "_LABEL_"
+ labelCount
+ "_PROPERTY_"
+ propertyCount
+ "_NAME")
!= null) {
String propertyName =
schema.getProp(
SPANNER_EDGE_TABLE
+ "_"
+ edgeTableCount
+ "_LABEL_"
+ labelCount
+ "_PROPERTY_"
+ propertyCount
+ "_NAME");
String propertyValue =
schema.getProp(
SPANNER_EDGE_TABLE
+ "_"
+ edgeTableCount
+ "_LABEL_"
+ labelCount
+ "_PROPERTY_"
+ propertyCount
+ "_VALUE");
propertyDefinitionsBuilder.add(
new GraphElementTable.PropertyDefinition(propertyName, propertyValue));
propertyCount++;
}
labelsBuilder.add(
new GraphElementTable.LabelToPropertyDefinitions(
labelName, propertyDefinitionsBuilder.build()));
labelCount++;
}
edgeTableBuilder.labelToPropertyDefinitions(labelsBuilder.build());
// Deserialize sourceNodeTable and targetNodeTable (always present for edges)
String sourceNodeTableName =
schema.getProp(SPANNER_EDGE_TABLE + "_" + edgeTableCount + "_SOURCE_NODE_TABLE_NAME");
String[] sourceNodeKeyColumns =
schema
.getProp(SPANNER_EDGE_TABLE + "_" + edgeTableCount + "_SOURCE_NODE_KEY_COLUMNS")
.trim()
.split(",");
String[] sourceEdgeKeyColumns =
schema
.getProp(SPANNER_EDGE_TABLE + "_" + edgeTableCount + "_SOURCE_EDGE_KEY_COLUMNS")
.trim()
.split(",");
edgeTableBuilder.sourceNodeTable(
new GraphElementTable.GraphNodeTableReference(
sourceNodeTableName,
ImmutableList.copyOf(sourceNodeKeyColumns),
ImmutableList.copyOf(sourceEdgeKeyColumns)));
String targetNodeTableName =
schema.getProp(SPANNER_EDGE_TABLE + "_" + edgeTableCount + "_TARGET_NODE_TABLE_NAME");
String[] targetNodeKeyColumns =
schema
.getProp(SPANNER_EDGE_TABLE + "_" + edgeTableCount + "_TARGET_NODE_KEY_COLUMNS")
.trim()
.split(",");
String[] targetEdgeKeyColumns =
schema
.getProp(SPANNER_EDGE_TABLE + "_" + edgeTableCount + "_TARGET_EDGE_KEY_COLUMNS")
.trim()
.split(",");
edgeTableBuilder.targetNodeTable(
new GraphElementTable.GraphNodeTableReference(
targetNodeTableName,
ImmutableList.copyOf(targetNodeKeyColumns),
ImmutableList.copyOf(targetEdgeKeyColumns)));
propertyGraphBuilder.addEdgeTable(edgeTableBuilder.autoBuild());
edgeTableCount++;
}
// Deserialize propertyDeclarations
int propertyDeclCount = 0;
ImmutableList.Builder<PropertyGraph.PropertyDeclaration> propertyDeclsBuilder =
ImmutableList.builder();
while (schema.getProp(SPANNER_PROPERTY_DECLARATION + "_" + propertyDeclCount + "_NAME")
!= null) {
String propertyName =
schema.getProp(SPANNER_PROPERTY_DECLARATION + "_" + propertyDeclCount + "_NAME");
String propertyType =
schema.getProp(SPANNER_PROPERTY_DECLARATION + "_" + propertyDeclCount + "_TYPE");
propertyGraphBuilder.addPropertyDeclaration(
new PropertyDeclaration(propertyName, propertyType));
propertyDeclCount++;
}
// Deserialize labels
int labelCount = 0;
ImmutableList.Builder<PropertyGraph.GraphElementLabel> labelsBuilder = ImmutableList.builder();
while (schema.getProp(SPANNER_LABEL + "_" + labelCount + "_NAME") != null) {
String labelName = schema.getProp(SPANNER_LABEL + "_" + labelCount + "_NAME");
ImmutableList.Builder<String> labelPropertiesBuilder = ImmutableList.builder();
int propertyCount = 0;
while (schema.getProp(SPANNER_LABEL + "_" + labelCount + "_PROPERTY_" + propertyCount)
!= null) {
labelPropertiesBuilder.add(
schema.getProp(SPANNER_LABEL + "_" + labelCount + "_PROPERTY_" + propertyCount));
propertyCount++;
}
propertyGraphBuilder.addLabel(
new PropertyGraph.GraphElementLabel(labelName, labelPropertiesBuilder.build()));
labelCount++;
}
return propertyGraphBuilder.build();
}