Search in sources :

Example 86 with Ddl

use of com.google.cloud.teleport.spanner.ddl.Ddl in project DataflowTemplates by GoogleCloudPlatform.

the class AvroSchemaToDdlConverterTest method changeStreams.

@Test
public void changeStreams() {
    String avroString1 = "{" + "  \"type\" : \"record\"," + "  \"name\" : \"ChangeStreamAll\"," + "  \"fields\" : []," + "  \"namespace\" : \"spannertest\"," + "  \"googleStorage\" : \"CloudSpanner\"," + "  \"googleFormatVersion\" : \"booleans\"," + "  \"spannerChangeStreamForClause\" : \"FOR ALL\"," + "  \"spannerOption_0\" : \"retention_period=\\\"7d\\\"\"," + "  \"spannerOption_1\" : \"value_capture_type=\\\"OLD_AND_NEW_VALUES\\\"\"" + "}";
    String avroString2 = "{" + "  \"type\" : \"record\"," + "  \"name\" : \"ChangeStreamEmpty\"," + "  \"fields\" : []," + "  \"namespace\" : \"spannertest\"," + "  \"googleStorage\" : \"CloudSpanner\"," + "  \"googleFormatVersion\" : \"booleans\"," + "  \"spannerChangeStreamForClause\" : \"\"" + "}";
    String avroString3 = "{" + "  \"type\" : \"record\"," + "  \"name\" : \"ChangeStreamTableColumns\"," + "  \"fields\" : []," + "  \"namespace\" : \"spannertest\"," + "  \"googleStorage\" : \"CloudSpanner\"," + "  \"googleFormatVersion\" : \"booleans\"," + "  \"spannerChangeStreamForClause\" : \"FOR `T1`, `T2`(`c1`, `c2`), `T3`()\"," + "  \"spannerOption_0\" : \"retention_period=\\\"24h\\\"\"" + "}";
    Collection<Schema> schemas = new ArrayList<>();
    Schema.Parser parser = new Schema.Parser();
    schemas.add(parser.parse(avroString1));
    schemas.add(parser.parse(avroString2));
    schemas.add(parser.parse(avroString3));
    AvroSchemaToDdlConverter converter = new AvroSchemaToDdlConverter();
    Ddl ddl = converter.toDdl(schemas);
    assertThat(ddl.changeStreams(), hasSize(3));
    assertThat(ddl.prettyPrint(), equalToCompressingWhiteSpace("CREATE CHANGE STREAM `ChangeStreamAll`" + " FOR ALL" + " OPTIONS (retention_period=\"7d\", value_capture_type=\"OLD_AND_NEW_VALUES\")" + " CREATE CHANGE STREAM `ChangeStreamEmpty`" + " CREATE CHANGE STREAM `ChangeStreamTableColumns`" + " FOR `T1`, `T2`(`c1`, `c2`), `T3`()" + " OPTIONS (retention_period=\"24h\")"));
}
Also used : Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Test(org.junit.Test)

Example 87 with Ddl

use of com.google.cloud.teleport.spanner.ddl.Ddl in project DataflowTemplates by GoogleCloudPlatform.

the class AvroSchemaToDdlConverterTest method emptySchema.

@Test
public void emptySchema() {
    AvroSchemaToDdlConverter converter = new AvroSchemaToDdlConverter();
    Ddl ddl = converter.toDdl(Collections.emptyList());
    assertThat(ddl.allTables(), empty());
}
Also used : Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Test(org.junit.Test)

Example 88 with Ddl

use of com.google.cloud.teleport.spanner.ddl.Ddl in project DataflowTemplates by GoogleCloudPlatform.

the class AvroSchemaToDdlConverterTest method simple.

@Test
public void simple() {
    String avroString = "{" + "  \"type\" : \"record\"," + "  \"name\" : \"Users\"," + "  \"namespace\" : \"spannertest\"," + "  \"fields\" : [ {" + "    \"name\" : \"id\"," + "    \"type\" : \"long\"," + "    \"sqlType\" : \"INT64\"" + "  }, {" + "    \"name\" : \"first_name\"," + "    \"type\" : [ \"null\", \"string\" ]," + "    \"sqlType\" : \"STRING(10)\"" + "  }, {" + "    \"name\" : \"last_name\"," + "    \"type\" : [ \"null\", \"string\" ]," + "    \"sqlType\" : \"STRING(MAX)\"" + "  }, {" + "    \"name\" : \"full_name\"," + "    \"type\" : \"null\"," + "    \"sqlType\" : \"STRING(MAX)\"," + "    \"notNull\" : \"false\"," + "    \"generationExpression\" : \"CONCAT(first_name, ' ', last_name)\"," + "    \"stored\" : \"true\"" + "  }, {" + "    \"name\" : \"numeric\"," + "    \"type\" : [\"null\", {\"type\":\"bytes\",\"logicalType\":\"decimal\"}]," + "    \"sqlType\" : \"NUMERIC\"" + "  }, {" + "    \"name\" : \"numeric2\"," + "    \"type\" : [\"null\", {\"type\":\"bytes\",\"logicalType\":\"decimal\"," + // Omitting sqlType
    "                \"precision\":38,\"scale\":9}]" + "  }, {" + "    \"name\" : \"notNumeric\"," + "    \"type\" : [\"null\", {\"type\":\"bytes\",\"logicalType\":\"decimal\"," + // Omitting sqlType
    "                \"precision\":38}]" + "  }, {" + "    \"name\" : \"notNumeric2\"," + "    \"type\" : [\"null\", {\"type\":\"bytes\",\"logicalType\":\"decimal\"," + // Omitting sqlType
    "                \"precision\":38,\"scale\":10}]" + "  }, {" + "    \"name\":\"numericArr\"," + "    \"type\": [\"null\",{\"type\":\"array\",\"items\":[\"null\",{\"type\":\"bytes\"," + "              \"logicalType\":\"decimal\",\"precision\":38,\"scale\":9}]}]" + // Omitting sqlType
    "  }, {" + "    \"name\":\"notNumericArr\"," + "    \"type\": [\"null\",{\"type\":\"array\",\"items\":[\"null\",{\"type\":\"bytes\"," + // Omitting sqlType
    "              \"logicalType\":\"decimal\",\"precision\":35}]}]" + "  }, {" + "    \"name\" : \"json\"," + "    \"type\" : [ \"null\", \"string\" ]," + "    \"sqlType\" : \"JSON\"" + "  }, {" + "    \"name\" : \"notJson\"," + // Omitting sqlType
    "    \"type\" : [ \"null\", \"string\" ]" + "  }, {" + "    \"name\":\"jsonArr\"," + "    \"type\":[\"null\",{\"type\":\"array\",\"items\":[\"null\",\"string\"]}]," + "    \"sqlType\":\"ARRAY<JSON>\"" + "  }, {" + "    \"name\":\"notJsonArr\"," + "    \"type\":[\"null\",{\"type\":\"array\",\"items\":[\"null\",\"string\"]}]" + // Omitting sqlType
    "  }]," + "  \"googleStorage\" : \"CloudSpanner\"," + "  \"spannerParent\" : \"\"," + "  \"googleFormatVersion\" : \"booleans\"," + "  \"spannerPrimaryKey_0\" : \"`id` ASC\"," + "  \"spannerPrimaryKey_1\" : \"`last_name` DESC\"," + "  \"spannerIndex_0\" : " + "  \"CREATE INDEX `UsersByFirstName` ON `Users` (`first_name`)\"," + "  \"spannerForeignKey_0\" : " + "  \"ALTER TABLE `Users` ADD CONSTRAINT `fk` FOREIGN KEY (`first_name`) " + "  REFERENCES `AllowedNames` (`first_name`)\"," + "  \"spannerCheckConstraint_0\" : " + "  \"CONSTRAINT `ck` CHECK(`first_name` != 'last_name')\"" + "}";
    Schema schema = new Schema.Parser().parse(avroString);
    AvroSchemaToDdlConverter converter = new AvroSchemaToDdlConverter();
    Ddl ddl = converter.toDdl(Collections.singleton(schema));
    assertThat(ddl.allTables(), hasSize(1));
    assertThat(ddl.views(), hasSize(0));
    assertThat(ddl.prettyPrint(), equalToCompressingWhiteSpace("CREATE TABLE `Users` (" + " `id`              INT64 NOT NULL," + " `first_name`      STRING(10)," + " `last_name`       STRING(MAX)," + " `full_name`       STRING(MAX) AS (CONCAT(first_name, ' ', last_name)) STORED," + " `numeric`         NUMERIC," + " `numeric2`        NUMERIC," + " `notNumeric`      BYTES(MAX)," + " `notNumeric2`     BYTES(MAX)," + " `numericArr`      ARRAY<NUMERIC>," + " `notNumericArr`   ARRAY<BYTES(MAX)>," + " `json`            JSON," + " `notJson`         STRING(MAX)," + " `jsonArr`         ARRAY<JSON>," + " `notJsonArr`      ARRAY<STRING(MAX)>," + " CONSTRAINT `ck` CHECK(`first_name` != 'last_name')," + " ) PRIMARY KEY (`id` ASC, `last_name` DESC)" + " CREATE INDEX `UsersByFirstName` ON `Users` (`first_name`)" + " ALTER TABLE `Users` ADD CONSTRAINT `fk`" + " FOREIGN KEY (`first_name`) REFERENCES `AllowedNames` (`first_name`)"));
}
Also used : Schema(org.apache.avro.Schema) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Test(org.junit.Test)

Example 89 with Ddl

use of com.google.cloud.teleport.spanner.ddl.Ddl in project DataflowTemplates by GoogleCloudPlatform.

the class AvroSchemaToDdlConverterTest method invokerRightsView.

@Test
public void invokerRightsView() {
    String avroString = "{" + "  \"type\" : \"record\"," + "  \"name\" : \"Names\"," + "  \"fields\" : []," + "  \"namespace\" : \"spannertest\"," + "  \"googleStorage\" : \"CloudSpanner\"," + "  \"googleFormatVersion\" : \"booleans\"," + "  \"spannerViewSecurity\" : \"INVOKER\"," + "  \"spannerViewQuery\" : \"SELECT first_name, last_name FROM Users\"" + "}";
    Schema schema = new Schema.Parser().parse(avroString);
    AvroSchemaToDdlConverter converter = new AvroSchemaToDdlConverter();
    Ddl ddl = converter.toDdl(Collections.singleton(schema));
    assertThat(ddl.views(), hasSize(1));
    assertThat(ddl.prettyPrint(), equalToCompressingWhiteSpace("CREATE VIEW `Names` SQL SECURITY INVOKER AS SELECT first_name, last_name FROM Users"));
}
Also used : Schema(org.apache.avro.Schema) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Test(org.junit.Test)

Example 90 with Ddl

use of com.google.cloud.teleport.spanner.ddl.Ddl in project DataflowTemplates by GoogleCloudPlatform.

the class AvroToDdlTool method main.

public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        System.out.println("Please specify the avro files");
        System.exit(1);
    }
    List<Schema> schemaList = new ArrayList<>();
    for (String filePath : args) {
        DatumReader<GenericRecord> datumReader = new GenericDatumReader<>();
        DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(new File(filePath), datumReader);
        Schema schema = dataFileReader.getSchema();
        System.out.println(schema.toString(true));
        schemaList.add(schema);
    }
    Ddl ddl = new AvroSchemaToDdlConverter().toDdl(schemaList);
    ddl.prettyPrint(System.out);
}
Also used : DataFileReader(org.apache.avro.file.DataFileReader) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) GenericRecord(org.apache.avro.generic.GenericRecord) File(java.io.File) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl)

Aggregations

Ddl (com.google.cloud.teleport.spanner.ddl.Ddl)109 Test (org.junit.Test)91 Schema (org.apache.avro.Schema)34 GenericRecord (org.apache.avro.generic.GenericRecord)19 List (java.util.List)18 Struct (com.google.cloud.spanner.Struct)14 Collectors (java.util.stream.Collectors)14 KV (org.apache.beam.sdk.values.KV)14 SpannerTableFilter.getFilteredTables (com.google.cloud.teleport.spanner.SpannerTableFilter.getFilteredTables)12 Type (com.google.cloud.teleport.spanner.common.Type)12 Path (java.nio.file.Path)12 Collections (java.util.Collections)12 ImmutableList (com.google.common.collect.ImmutableList)11 IOException (java.io.IOException)11 Assert.assertEquals (org.junit.Assert.assertEquals)11 ReadImportManifest (com.google.cloud.teleport.spanner.TextImportTransform.ReadImportManifest)10 ResolveDataFiles (com.google.cloud.teleport.spanner.TextImportTransform.ResolveDataFiles)10 BufferedWriter (java.io.BufferedWriter)10 Charset (java.nio.charset.Charset)10 RunWith (org.junit.runner.RunWith)9