Search in sources :

Example 31 with Ddl

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

the class TextImportTransformTest method readImportManifestGeneratedColumn.

@Test
public void readImportManifestGeneratedColumn() throws Exception {
    Path f31 = Files.createTempFile("table3-file", "1");
    Path manifestFile = Files.createTempFile("import-manifest", ".json");
    Charset charset = Charset.forName("UTF-8");
    try (BufferedWriter writer = Files.newBufferedWriter(manifestFile, charset)) {
        String jsonString = String.format("{\"tables\": [" + "{\"table_name\": \"table3\"," + "\"file_patterns\": [\"%s\"]," + "\"columns\": [{\"column_name\": \"int_col\", \"type_name\": \"INT64\"}]}" + "]}", f31.toString());
        writer.write(jsonString, 0, jsonString.length());
    } catch (IOException e) {
        e.printStackTrace();
    }
    ValueProvider<String> importManifest = ValueProvider.StaticValueProvider.of(manifestFile.toString());
    PCollectionView<Ddl> ddlView = pipeline.apply("ddl", Create.of(getTestDdl())).apply(View.asSingleton());
    PCollection<KV<String, String>> tableAndFiles = pipeline.apply("Read manifest file", new ReadImportManifest(importManifest)).apply("Resolve data files", new ResolveDataFiles(importManifest, ddlView));
    pipeline.run();
}
Also used : Path(java.nio.file.Path) ResolveDataFiles(com.google.cloud.teleport.spanner.TextImportTransform.ResolveDataFiles) ReadImportManifest(com.google.cloud.teleport.spanner.TextImportTransform.ReadImportManifest) Charset(java.nio.charset.Charset) IOException(java.io.IOException) KV(org.apache.beam.sdk.values.KV) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 32 with Ddl

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

the class TextImportTransformTest method readImportManifestUtfWithBOM.

@Test
public void readImportManifestUtfWithBOM() throws Exception {
    Path f11 = Files.createTempFile("table1-file", "1");
    String tempDir = f11.getParent().toString();
    Path manifestFile = Files.createTempFile("import-manifest", ".json");
    Charset charset = Charset.forName("UTF-8");
    try (BufferedWriter writer = Files.newBufferedWriter(manifestFile, charset)) {
        String jsonString = String.format("\uFEFF{\"tables\": [" + "{\"table_name\": \"table1\"," + "\"file_patterns\":[\"%s\"]}" + "]}", f11.toString());
        writer.write(jsonString, 0, jsonString.length());
    } catch (IOException e) {
        e.printStackTrace();
    }
    ValueProvider<String> importManifest = ValueProvider.StaticValueProvider.of(manifestFile.toString());
    PCollectionView<Ddl> ddlView = pipeline.apply("ddl", Create.of(getTestDdl())).apply(View.asSingleton());
    PCollection<KV<String, String>> tableAndFiles = pipeline.apply("Read manifest file", new ReadImportManifest(importManifest)).apply("Resolve data files", new ResolveDataFiles(importManifest, ddlView));
    PAssert.that(tableAndFiles).containsInAnyOrder(KV.of("table1", f11.toString()));
    pipeline.run();
}
Also used : Path(java.nio.file.Path) ResolveDataFiles(com.google.cloud.teleport.spanner.TextImportTransform.ResolveDataFiles) ReadImportManifest(com.google.cloud.teleport.spanner.TextImportTransform.ReadImportManifest) Charset(java.nio.charset.Charset) IOException(java.io.IOException) KV(org.apache.beam.sdk.values.KV) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 33 with Ddl

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

the class TextImportTransformTest method readImportManifestColumnListMustBeProvidedForGeneratedColumn.

@Test
public void readImportManifestColumnListMustBeProvidedForGeneratedColumn() throws Exception {
    Path f31 = Files.createTempFile("table3-file", "1");
    Path manifestFile = Files.createTempFile("import-manifest", ".json");
    Charset charset = Charset.forName("UTF-8");
    try (BufferedWriter writer = Files.newBufferedWriter(manifestFile, charset)) {
        String jsonString = String.format("{\"tables\": [" + "{\"table_name\": \"table3\"," + "\"file_patterns\": [\"%s\"]}" + "]}", f31.toString());
        writer.write(jsonString, 0, jsonString.length());
    } catch (IOException e) {
        e.printStackTrace();
    }
    ValueProvider<String> importManifest = ValueProvider.StaticValueProvider.of(manifestFile.toString());
    PCollectionView<Ddl> ddlView = pipeline.apply("ddl", Create.of(getTestDdl())).apply(View.asSingleton());
    PCollection<KV<String, String>> tableAndFiles = pipeline.apply("Read manifest file", new ReadImportManifest(importManifest)).apply("Resolve data files", new ResolveDataFiles(importManifest, ddlView));
    try {
        pipeline.run();
    } catch (PipelineExecutionException e) {
        assertThat(e.getMessage(), equalTo("java.lang.RuntimeException: DB table table3 has one or more generated columns. An" + " explicit column list that excludes the generated columns must be provided in" + " the manifest."));
    }
}
Also used : Path(java.nio.file.Path) Charset(java.nio.charset.Charset) IOException(java.io.IOException) KV(org.apache.beam.sdk.values.KV) Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) BufferedWriter(java.io.BufferedWriter) ResolveDataFiles(com.google.cloud.teleport.spanner.TextImportTransform.ResolveDataFiles) PipelineExecutionException(org.apache.beam.sdk.Pipeline.PipelineExecutionException) ReadImportManifest(com.google.cloud.teleport.spanner.TextImportTransform.ReadImportManifest) Test(org.junit.Test)

Example 34 with Ddl

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

the class SpannerToTextTest method runPgExportWithTsTest.

@Test
public void runPgExportWithTsTest() throws Exception {
    Ddl ddl = Ddl.builder(Dialect.POSTGRESQL).createTable(tableName).column("first_name").pgVarchar().max().endColumn().column("last_name").pgVarchar().size(5).endColumn().column("age").pgInt8().endColumn().primaryKey().asc("first_name").desc("last_name").end().endTable().build();
    /* Create initial table and populate
     * numBatches = 100
     */
    createAndPopulate(sourceDb, ddl, 100);
    // Export the database and note the timestamp ts1
    exportDbAtTime(sourceDb, destDbPrefix + chkpt1, chkpt1, "", exportPipeline1, tmpDir);
    // Save the timestamp directly after the export
    String chkPt1Ts = getCurrentTimestamp(Dialect.POSTGRESQL);
    // Add more records to the table, export the database and note the timestamp ts3
    spannerServer.populateRandomData(sourceDb, ddl, 100);
    // Export the table from the database using the saved timestamp
    exportDbAtTime(sourceDb, destDbPrefix + chkpt2, chkpt2, chkPt1Ts, exportPipeline2, tmpDir);
    File folder = new File(tmpDir + "/");
    // Store the contents of directory containing the exported CSVs into a List
    File[] files = folder.listFiles();
    List<String> oldData = readDbData(files, chkpt1);
    List<String> expectedOldData = readDbData(files, chkpt2);
    // Sort statements
    Collections.sort(oldData);
    Collections.sort(expectedOldData);
    assertEquals(oldData, expectedOldData);
}
Also used : Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) File(java.io.File) IntegrationTest(com.google.cloud.teleport.spanner.IntegrationTest) Test(org.junit.Test)

Example 35 with Ddl

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

the class SpannerToTextTest method runExportWithTsTest.

/* Validates behavior of database export without specifying timestamp
   * and with timestamp specified */
@Test
public void runExportWithTsTest() throws Exception {
    Ddl ddl = Ddl.builder().createTable(tableName).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().build();
    /* Create initial table and populate
     * numBatches = 100
     */
    createAndPopulate(sourceDb, ddl, 100);
    // Export the database and note the timestamp ts1
    exportDbAtTime(sourceDb, destDbPrefix + chkpt1, chkpt1, "", exportPipeline1, tmpDir);
    // Save the timestamp directly after the export
    String chkPt1Ts = getCurrentTimestamp(Dialect.GOOGLE_STANDARD_SQL);
    // Add more records to the table, export the database and note the timestamp ts3
    spannerServer.populateRandomData(sourceDb, ddl, 100);
    // Export the table from the database using the saved timestamp
    exportDbAtTime(sourceDb, destDbPrefix + chkpt2, chkpt2, chkPt1Ts, exportPipeline2, tmpDir);
    File folder = new File(tmpDir + "/");
    // Store the contents of directory containing the exported CSVs into a List
    File[] files = folder.listFiles();
    List<String> oldData = readDbData(files, chkpt1);
    List<String> expectedOldData = readDbData(files, chkpt2);
    // Sort statements
    Collections.sort(oldData);
    Collections.sort(expectedOldData);
    assertEquals(oldData, expectedOldData);
}
Also used : Ddl(com.google.cloud.teleport.spanner.ddl.Ddl) File(java.io.File) IntegrationTest(com.google.cloud.teleport.spanner.IntegrationTest) 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