Search in sources :

Example 71 with Ddl

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

the class SpannerRecordConverterTest method pgTimestamptzLogical.

@Test
public void pgTimestamptzLogical() {
    Ddl ddl = Ddl.builder(Dialect.POSTGRESQL).createTable("users").column("id").pgInt8().notNull().endColumn().column("ts1").pgTimestamptz().endColumn().column("ts2").pgTimestamptz().endColumn().column("ts3").pgTimestamptz().endColumn().column("ts4").pgTimestamptz().endColumn().column("ts5").pgTimestamptz().endColumn().primaryKey().asc("id").end().endTable().build();
    Schema schema = logicalTypeConverter.convert(ddl).iterator().next();
    SpannerRecordConverter recordConverter = new SpannerRecordConverter(schema, Dialect.POSTGRESQL);
    Struct struct = Struct.newBuilder().set("id").to(1L).set("ts1").to(Timestamp.ofTimeMicroseconds(10)).set("ts2").to(Timestamp.ofTimeSecondsAndNanos(10000, 100000)).set("ts3").to(Timestamp.parseTimestamp("1970-01-01T00:00:00Z")).set("ts4").to(Timestamp.MIN_VALUE).set("ts5").to(Timestamp.MAX_VALUE).build();
    GenericRecord avroRecord = recordConverter.convert(struct);
    assertThat(avroRecord.get("id"), equalTo(1L));
    assertThat(avroRecord.get("ts1"), equalTo(10L));
    assertThat(avroRecord.get("ts2"), equalTo(10000000100L));
    assertThat(avroRecord.get("ts3"), equalTo(0L));
    assertThat(avroRecord.get("ts4"), equalTo(-62135596800000000L));
    assertThat(avroRecord.get("ts5"), equalTo(253402300799999999L));
}
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 72 with Ddl

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

the class SpannerRecordConverterTest method json.

@Test
public void json() {
    Ddl ddl = Ddl.builder().createTable("jsontable").column("id").int64().notNull().endColumn().column("json").type(Type.json()).endColumn().column("json_arr").type(Type.array(Type.json())).endColumn().primaryKey().asc("id").end().endTable().build();
    Schema schema = converter.convert(ddl).iterator().next();
    SpannerRecordConverter recordConverter = new SpannerRecordConverter(schema);
    String[] jsonArrValues = { null, "[1,null,true,2.2523,\"hello\"]", null, "{\"a\":{\"a\":2.5},\"b\":null}" };
    Struct struct = Struct.newBuilder().set("id").to(1L).set("json").to("\"hello my friend\"").set("json_arr").toStringArray(Lists.newArrayList(jsonArrValues)).build();
    GenericRecord avroRecord = recordConverter.convert(struct);
    assertThat(avroRecord.get("id"), equalTo(1L));
    assertThat(avroRecord.get("json"), equalTo("\"hello my friend\""));
    assertThat(avroRecord.get("json_arr"), equalTo(Arrays.asList(null, "[1,null,true,2.2523,\"hello\"]", null, "{\"a\":{\"a\":2.5},\"b\":null}")));
}
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 73 with Ddl

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

the class SpannerRecordConverterTest method dateTimestamp.

@Test
public void dateTimestamp() {
    Ddl ddl = Ddl.builder().createTable("users").column("id").int64().notNull().endColumn().column("date").date().endColumn().column("ts").timestamp().endColumn().primaryKey().asc("id").end().endTable().build();
    Schema schema = converter.convert(ddl).iterator().next();
    SpannerRecordConverter recordConverter = new SpannerRecordConverter(schema);
    Struct struct = Struct.newBuilder().set("id").to(1L).set("date").to(Date.fromYearMonthDay(2018, 2, 2)).set("ts").to(Timestamp.ofTimeMicroseconds(10)).build();
    GenericRecord avroRecord = recordConverter.convert(struct);
    assertThat(avroRecord.get("id"), equalTo(1L));
    assertThat(avroRecord.get("date"), equalTo("2018-02-02"));
    assertThat(avroRecord.get("ts"), equalTo("1970-01-01T00:00:00.000010000Z"));
}
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 74 with Ddl

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

the class SpannerRecordConverterTest method timestampLogical.

@Test
public void timestampLogical() {
    Ddl ddl = Ddl.builder().createTable("users").column("id").int64().notNull().endColumn().column("date").date().endColumn().column("ts").timestamp().endColumn().column("ts_array").type(Type.array(Type.timestamp())).endColumn().primaryKey().asc("id").end().endTable().build();
    Schema schema = logicalTypeConverter.convert(ddl).iterator().next();
    SpannerRecordConverter recordConverter = new SpannerRecordConverter(schema);
    Struct struct = Struct.newBuilder().set("id").to(1L).set("date").to(Date.fromYearMonthDay(2018, 2, 2)).set("ts").to(Timestamp.ofTimeMicroseconds(10)).set("ts_array").toTimestampArray(Lists.newArrayList(null, null, Timestamp.ofTimeMicroseconds(10L), Timestamp.ofTimeSecondsAndNanos(10000, 100000), Timestamp.parseTimestamp("1970-01-01T00:00:00Z"), Timestamp.MIN_VALUE, Timestamp.MAX_VALUE)).build();
    GenericRecord avroRecord = recordConverter.convert(struct);
    assertThat(avroRecord.get("id"), equalTo(1L));
    assertThat(avroRecord.get("date"), equalTo("2018-02-02"));
    assertThat(avroRecord.get("ts"), equalTo(10L));
    assertThat(avroRecord.get("ts_array"), equalTo(Arrays.asList(null, null, 10L, 10000000100L, 0L, -62135596800000000L, 253402300799999999L)));
}
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 75 with Ddl

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

the class SpannerRecordConverterTest method simple.

@Test
public void simple() {
    Ddl ddl = Ddl.builder().createTable("users").column("id").int64().notNull().endColumn().column("email").string().size(15).notNull().endColumn().column("name").string().max().endColumn().primaryKey().asc("id").end().endTable().build();
    Schema schema = converter.convert(ddl).iterator().next();
    SpannerRecordConverter recordConverter = new SpannerRecordConverter(schema);
    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)

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