Search in sources :

Example 46 with Ddl

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

the class CopyDbTest method emptyTables.

@Test
public void emptyTables() throws Exception {
    // spotless:off
    Ddl ddl = Ddl.builder().createTable("Users").column("first_name").string().max().endColumn().column("last_name").string().size(5).endColumn().column("age").int64().endColumn().primaryKey().asc("first_name").desc("last_name").end().endTable().createTable("AllTYPES").column("first_name").string().max().endColumn().column("last_name").string().size(5).endColumn().column("id").int64().notNull().endColumn().column("bool_field").bool().endColumn().column("int64_field").int64().endColumn().column("float64_field").float64().endColumn().column("string_field").string().max().endColumn().column("bytes_field").bytes().max().endColumn().column("timestamp_field").timestamp().endColumn().column("date_field").date().endColumn().column("arr_bool_field").type(Type.array(Type.bool())).endColumn().column("arr_int64_field").type(Type.array(Type.int64())).endColumn().column("arr_float64_field").type(Type.array(Type.float64())).endColumn().column("arr_string_field").type(Type.array(Type.string())).max().endColumn().column("arr_bytes_field").type(Type.array(Type.bytes())).max().endColumn().column("arr_timestamp_field").type(Type.array(Type.timestamp())).endColumn().column("arr_date_field").type(Type.array(Type.date())).endColumn().primaryKey().asc("first_name").desc("last_name").asc("id").end().interleaveInParent("Users").endTable().build();
    createAndPopulate(ddl, 10);
    // Add empty tables.
    Ddl emptyTables = Ddl.builder().createTable("empty_one").column("first").string().max().endColumn().column("second").string().size(5).endColumn().column("value").int64().endColumn().primaryKey().asc("first").desc("second").end().endTable().createTable("empty_two").column("first").string().max().endColumn().column("second").string().size(5).endColumn().column("value").int64().endColumn().column("another_value").int64().endColumn().primaryKey().asc("first").end().endTable().build();
    // spotless:on
    spannerServer.updateDatabase(sourceDb, emptyTables.createTableStatements());
    runTest();
}
Also used : Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Test(org.junit.Test)

Example 47 with Ddl

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

the class CopyDbTest method randomSchema.

@Test
public void randomSchema() throws Exception {
    Ddl ddl = RandomDdlGenerator.builder().build().generate();
    createAndPopulate(ddl, 100);
    runTest();
}
Also used : Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Test(org.junit.Test)

Example 48 with Ddl

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

the class CopyDbTest method databaseOptions.

@Test
public void databaseOptions() throws Exception {
    Ddl.Builder ddlBuilder = Ddl.builder();
    // Table Content
    // spotless:off
    ddlBuilder.createTable("Users").column("first_name").string().max().endColumn().column("last_name").string().size(5).endColumn().column("age").int64().endColumn().primaryKey().asc("first_name").desc("last_name").end().endTable().createTable("EmploymentData").column("first_name").string().max().endColumn().column("last_name").string().size(5).endColumn().column("id").int64().notNull().endColumn().column("age").int64().endColumn().column("address").string().max().endColumn().primaryKey().asc("first_name").desc("last_name").asc("id").end().interleaveInParent("Users").onDeleteCascade().endTable();
    // spotless:on
    // Allowed and well-formed database option
    List<Export.DatabaseOption> dbOptionList = new ArrayList<>();
    dbOptionList.add(Export.DatabaseOption.newBuilder().setOptionName("version_retention_period").setOptionValue("\"6d\"").build());
    // Disallowed database option
    dbOptionList.add(Export.DatabaseOption.newBuilder().setOptionName("optimizer_version").setOptionValue("1").build());
    // Misformed database option
    dbOptionList.add(Export.DatabaseOption.newBuilder().setOptionName("123version").setOptionValue("xyz").build());
    ddlBuilder.mergeDatabaseOptions(dbOptionList);
    Ddl ddl = ddlBuilder.build();
    createAndPopulate(ddl, 100);
    runTest();
    Ddl destinationDdl = readDdl(destinationDb, Dialect.GOOGLE_STANDARD_SQL);
    List<String> destDbOptions = destinationDdl.setOptionsStatements(destinationDb);
    assertThat(destDbOptions.size(), is(1));
    assertThat(destDbOptions.get(0), is("ALTER DATABASE `" + destinationDb + "` SET OPTIONS ( version_retention_period = \"6d\" )"));
}
Also used : ArrayList(java.util.ArrayList) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Test(org.junit.Test)

Example 49 with Ddl

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

the class CopySchemaTest method copyRandomSchema.

@Test
public void copyRandomSchema() {
    Ddl ddl = RandomDdlGenerator.builder().setMaxPkComponents(2).setMaxBranchPerLevel(new int[] { 5, 4, 3, 2, 2, 3, 3 }).setMaxViews(3).build().generate();
    try {
        ddl.prettyPrint(System.out);
    } catch (IOException e) {
        e.printStackTrace();
    }
    DdlToAvroSchemaConverter ddlToAvro = new DdlToAvroSchemaConverter("spanner", "test", false);
    AvroSchemaToDdlConverter avroToDdl = new AvroSchemaToDdlConverter();
    Collection<Schema> schemas = ddlToAvro.convert(ddl);
    Ddl copied = avroToDdl.toDdl(schemas);
    assertThat(copied.prettyPrint(), equalToCompressingWhiteSpace(ddl.prettyPrint()));
}
Also used : Schema(org.apache.avro.Schema) IOException(java.io.IOException) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Test(org.junit.Test)

Example 50 with Ddl

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

the class CopySchemaTest method pgCopyRandomSchema.

@Test
public void pgCopyRandomSchema() {
    Ddl ddl = RandomDdlGenerator.builder(Dialect.POSTGRESQL).setMaxPkComponents(2).setMaxBranchPerLevel(new int[] { 5, 4, 3, 2, 2, 3, 3 }).setMaxViews(3).build().generate();
    try {
        ddl.prettyPrint(System.out);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    DdlToAvroSchemaConverter ddlToAvro = new DdlToAvroSchemaConverter("spanner", "test", false);
    AvroSchemaToDdlConverter avroToDdl = new AvroSchemaToDdlConverter(Dialect.POSTGRESQL);
    Collection<Schema> schemas = ddlToAvro.convert(ddl);
    Ddl copied = avroToDdl.toDdl(schemas);
    assertThat(copied.prettyPrint(), equalToCompressingWhiteSpace(ddl.prettyPrint()));
}
Also used : Schema(org.apache.avro.Schema) IOException(java.io.IOException) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Test(org.junit.Test)

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