Search in sources :

Example 21 with Ddl

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

the class SpannerRecordConverterTest method pgSimple.

@Test
public void pgSimple() {
    Ddl ddl = Ddl.builder(Dialect.POSTGRESQL).createTable("users").column("id").pgInt8().notNull().endColumn().column("email").pgVarchar().size(15).notNull().endColumn().column("name").pgVarchar().max().endColumn().primaryKey().asc("id").end().endTable().build();
    Schema schema = converter.convert(ddl).iterator().next();
    SpannerRecordConverter recordConverter = new SpannerRecordConverter(schema, Dialect.POSTGRESQL);
    Struct struct = Struct.newBuilder().set("id").to(1L).set("email").to("abc@google.com").set("name").to("John Doe").build();
    GenericRecord avroRecord = recordConverter.convert(struct);
    assertThat(avroRecord.get("id"), equalTo(1L));
    assertThat(avroRecord.get("email"), equalTo("abc@google.com"));
    assertThat(avroRecord.get("name"), equalTo("John Doe"));
}
Also used : Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Struct(com.google.cloud.spanner.Struct) Test(org.junit.Test)

Example 22 with Ddl

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

the class SpannerRecordConverterTest method pgDateTimestamptz.

@Test
public void pgDateTimestamptz() {
    Ddl ddl = Ddl.builder(Dialect.POSTGRESQL).createTable("users").column("id").pgInt8().notNull().endColumn().column("ts").pgTimestamptz().endColumn().column("date").pgDate().endColumn().primaryKey().asc("id").end().endTable().build();
    Schema schema = converter.convert(ddl).iterator().next();
    SpannerRecordConverter recordConverter = new SpannerRecordConverter(schema, Dialect.POSTGRESQL);
    Struct struct = Struct.newBuilder().set("id").to(1L).set("ts").to(Timestamp.ofTimeMicroseconds(10)).set("date").to(Date.fromYearMonthDay(2018, 2, 2)).build();
    GenericRecord avroRecord = recordConverter.convert(struct);
    assertThat(avroRecord.get("id"), equalTo(1L));
    assertThat(avroRecord.get("ts"), equalTo("1970-01-01T00:00:00.000010000Z"));
    assertThat(avroRecord.get("date"), equalTo("2018-02-02"));
}
Also used : Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Struct(com.google.cloud.spanner.Struct) Test(org.junit.Test)

Example 23 with Ddl

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

the class SpannerTableFilterTest method individualTableSelection_selectsOnlyChosenTable.

@Test
public void individualTableSelection_selectsOnlyChosenTable() throws Exception {
    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").onDeleteCascade().endTable().build();
    List<String> filteredTables = getFilteredTables(ddl, ImmutableList.of(usersTable)).stream().map(t -> t.name()).collect(Collectors.toList());
    List<String> expectedFilteredTables = ImmutableList.of(usersTable);
    assertEquals(expectedFilteredTables, filteredTables);
}
Also used : SpannerTableFilter.getFilteredTables(com.google.cloud.teleport.spanner.SpannerTableFilter.getFilteredTables) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Type(com.google.cloud.teleport.spanner.common.Type) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) Assert.assertEquals(org.junit.Assert.assertEquals) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Test(org.junit.Test)

Example 24 with Ddl

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

the class SpannerTableFilterTest method multipleForeignKeyTableFilterSelection_selectsChosenAndAllReferencedTables.

@Test
public void multipleForeignKeyTableFilterSelection_selectsChosenAndAllReferencedTables() throws Exception {
    Ddl ddl = Ddl.builder().createTable("Ref1").column("id1").int64().endColumn().column("id2").int64().endColumn().primaryKey().asc("id1").asc("id2").end().endTable().createTable("Ref2").column("id1").int64().endColumn().column("id2").int64().endColumn().primaryKey().asc("id1").asc("id2").end().endTable().createTable("Child").column("id1").int64().endColumn().column("id2").int64().endColumn().column("id3").int64().endColumn().primaryKey().asc("id1").asc("id2").asc("id3").end().endTable().build();
    // Add to referencedTable field (i.e. `Child` would have two foreign key constraints
    // referencing `Ref1` and `Ref2` )
    ddl.addNewReferencedTable("Child", "Ref1");
    ddl.addNewReferencedTable("Child", "Ref2");
    List<String> filteredTables = getFilteredTables(ddl, ImmutableList.of(childTable)).stream().map(t -> t.name()).collect(Collectors.toList());
    List<String> expectedFilteredTables = ImmutableList.of(childTable, refOneTable, refTwoTable);
    Collections.sort(filteredTables);
    assertEquals(expectedFilteredTables, filteredTables);
}
Also used : SpannerTableFilter.getFilteredTables(com.google.cloud.teleport.spanner.SpannerTableFilter.getFilteredTables) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Type(com.google.cloud.teleport.spanner.common.Type) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) Assert.assertEquals(org.junit.Assert.assertEquals) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Test(org.junit.Test)

Example 25 with Ddl

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

the class SpannerTableFilterTest method filterWithAllAncestorsSelection_selectsChosenTableWithAllParents.

@Test
public void filterWithAllAncestorsSelection_selectsChosenTableWithAllParents() throws Exception {
    Ddl ddl = Ddl.builder().createTable("table_c").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("table_b").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().primaryKey().asc("first_name").desc("last_name").asc("id").end().interleaveInParent("table_c").onDeleteCascade().endTable().createTable("table_a").column("first_name").string().max().endColumn().column("last_name").string().size(5).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("table_b").onDeleteCascade().endTable().build();
    List<String> filteredTables = getFilteredTables(ddl, ImmutableList.of(tableA)).stream().map(t -> t.name()).collect(Collectors.toList());
    List<String> expectedFilteredTables = ImmutableList.of(tableA, tableB, tableC);
    Collections.sort(filteredTables);
    assertEquals(expectedFilteredTables, filteredTables);
}
Also used : SpannerTableFilter.getFilteredTables(com.google.cloud.teleport.spanner.SpannerTableFilter.getFilteredTables) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Type(com.google.cloud.teleport.spanner.common.Type) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) Assert.assertEquals(org.junit.Assert.assertEquals) 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