Search in sources :

Example 1 with Dialect

use of com.google.cloud.spanner.Dialect in project java-spanner by googleapis.

the class ITDatabaseAdminDialectAwareTest method testCreateDatabaseWithDialect.

@Test
public void testCreateDatabaseWithDialect() throws Exception {
    assumeFalse("emulator does not support different dialects", isUsingEmulator());
    final String projectId = testHelper.getInstanceId().getProject();
    final String instanceId = testHelper.getInstanceId().getInstance();
    final String databaseId = testHelper.getUniqueDatabaseId();
    final Database databaseToCreate = client.newDatabaseBuilder(DatabaseId.of(projectId, instanceId, databaseId)).setDialect(dialect).build();
    databases.add(databaseToCreate.getId());
    // Creates the database with the dialect set
    final Database createdDatabase = client.createDatabase(databaseToCreate, Collections.emptyList()).get(OPERATION_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
    assertEquals(dialect, createdDatabase.getDialect());
    // Test dialect in database retrieval
    final Database retrievedDatabase = client.getDatabase(instanceId, databaseId);
    assertEquals(dialect, retrievedDatabase.getDialect());
    // Test dialect database listing
    final Optional<Database> maybeListedDatabase = StreamSupport.stream(client.listDatabases(instanceId).iterateAll().spliterator(), false).filter(database -> database.getId().getDatabase().equals(databaseId)).findFirst();
    assertTrue("Expected to find database in list", maybeListedDatabase.isPresent());
    assertEquals(dialect, maybeListedDatabase.get().getDialect());
}
Also used : DatabaseId(com.google.cloud.spanner.DatabaseId) Dialect(com.google.cloud.spanner.Dialect) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) Assume.assumeFalse(org.junit.Assume.assumeFalse) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) RemoteSpannerHelper(com.google.cloud.spanner.testing.RemoteSpannerHelper) ArrayList(java.util.ArrayList) Duration(java.time.Duration) After(org.junit.After) StreamSupport(java.util.stream.StreamSupport) ClassRule(org.junit.ClassRule) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Database(com.google.cloud.spanner.Database) Parameter(org.junit.runners.Parameterized.Parameter) Assert.assertTrue(org.junit.Assert.assertTrue) EmulatorSpannerHelper.isUsingEmulator(com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) IntegrationTestEnv(com.google.cloud.spanner.IntegrationTestEnv) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Optional(java.util.Optional) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Database(com.google.cloud.spanner.Database) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test)

Example 2 with Dialect

use of com.google.cloud.spanner.Dialect in project beam by apache.

the class ReadSpannerSchema method processElement.

@ProcessElement
public void processElement(ProcessContext c) throws Exception {
    Dialect dialect = c.sideInput(dialectView);
    SpannerSchema.Builder builder = SpannerSchema.builder(dialect);
    DatabaseClient databaseClient = spannerAccessor.getDatabaseClient();
    try (ReadOnlyTransaction tx = databaseClient.readOnlyTransaction()) {
        ResultSet resultSet = readTableInfo(tx, dialect);
        while (resultSet.next()) {
            String tableName = resultSet.getString(0);
            String columnName = resultSet.getString(1);
            String type = resultSet.getString(2);
            long cellsMutated = resultSet.getLong(3);
            builder.addColumn(tableName, columnName, type, cellsMutated);
        }
        resultSet = readPrimaryKeyInfo(tx, dialect);
        while (resultSet.next()) {
            String tableName = resultSet.getString(0);
            String columnName = resultSet.getString(1);
            String ordering = resultSet.getString(2);
            builder.addKeyPart(tableName, columnName, "DESC".equalsIgnoreCase(ordering));
        }
    }
    c.output(builder.build());
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) Dialect(com.google.cloud.spanner.Dialect) ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet)

Example 3 with Dialect

use of com.google.cloud.spanner.Dialect in project beam by apache.

the class SpannerWriteIT method testWrite.

@Test
public void testWrite() throws Exception {
    int numRecords = 100;
    p.apply("Init", GenerateSequence.from(0).to(numRecords)).apply("Generate mu", ParDo.of(new GenerateMutations(options.getTable()))).apply("Write db", SpannerIO.write().withProjectId(project).withInstanceId(options.getInstanceId()).withDatabaseId(databaseName));
    PCollectionView<Dialect> dialectView = p.apply(Create.of(Dialect.POSTGRESQL)).apply(View.asSingleton());
    p.apply("PG init", GenerateSequence.from(0).to(numRecords)).apply("Generate PG mu", ParDo.of(new GenerateMutations(options.getTable()))).apply("Write PG db", SpannerIO.write().withProjectId(project).withInstanceId(options.getInstanceId()).withDatabaseId(pgDatabaseName).withDialectView(dialectView));
    PipelineResult result = p.run();
    result.waitUntilFinish();
    assertThat(result.getState(), is(PipelineResult.State.DONE));
    assertThat(countNumberOfRecords(databaseName), equalTo((long) numRecords));
    assertThat(countNumberOfRecords(pgDatabaseName), equalTo((long) numRecords));
}
Also used : Dialect(com.google.cloud.spanner.Dialect) PipelineResult(org.apache.beam.sdk.PipelineResult) Test(org.junit.Test)

Example 4 with Dialect

use of com.google.cloud.spanner.Dialect in project beam by apache.

the class SpannerWriteIT method testReportFailures.

@Test
public void testReportFailures() throws Exception {
    int numRecords = 100;
    p.apply("init", GenerateSequence.from(0).to(2 * numRecords)).apply("Generate mu", ParDo.of(new GenerateMutations(options.getTable(), new DivBy2()))).apply("Write db", SpannerIO.write().withProjectId(project).withInstanceId(options.getInstanceId()).withDatabaseId(databaseName).withFailureMode(SpannerIO.FailureMode.REPORT_FAILURES));
    PCollectionView<Dialect> dialectView = p.apply(Create.of(Dialect.POSTGRESQL)).apply(View.asSingleton());
    p.apply("pg init", GenerateSequence.from(0).to(2 * numRecords)).apply("Generate pg mu", ParDo.of(new GenerateMutations(options.getTable(), new DivBy2()))).apply("Write pg db", SpannerIO.write().withProjectId(project).withInstanceId(options.getInstanceId()).withDatabaseId(pgDatabaseName).withFailureMode(SpannerIO.FailureMode.REPORT_FAILURES).withDialectView(dialectView));
    PipelineResult result = p.run();
    result.waitUntilFinish();
    assertThat(result.getState(), is(PipelineResult.State.DONE));
    assertThat(countNumberOfRecords(databaseName), equalTo((long) numRecords));
    assertThat(countNumberOfRecords(pgDatabaseName), equalTo((long) numRecords));
}
Also used : Dialect(com.google.cloud.spanner.Dialect) PipelineResult(org.apache.beam.sdk.PipelineResult) Test(org.junit.Test)

Example 5 with Dialect

use of com.google.cloud.spanner.Dialect in project beam by apache.

the class ReadSpannerSchemaTest method simple.

@Test
public void simple() throws Exception {
    // Simplest schema: a table with int64 key
    ReadOnlyTransaction tx = mock(ReadOnlyTransaction.class);
    when(serviceFactory.mockDatabaseClient().readOnlyTransaction()).thenReturn(tx);
    preparePkMetadata(tx, Arrays.asList(pkMetadata("test", "key", "ASC")));
    prepareColumnMetadata(tx, Arrays.asList(columnMetadata("test", "key", "INT64")));
    SpannerConfig config = SpannerConfig.create().withProjectId("test-project").withInstanceId("test-instance").withDatabaseId("test-database").withServiceFactory(serviceFactory);
    PCollectionView<Dialect> dialectView = pipeline.apply(Create.of(Dialect.GOOGLE_STANDARD_SQL)).apply(View.asSingleton());
    pipeline.run();
    DoFnTester<Void, SpannerSchema> tester = DoFnTester.of(new ReadSpannerSchema(config, dialectView));
    tester.setSideInput(dialectView, GlobalWindow.INSTANCE, Dialect.GOOGLE_STANDARD_SQL);
    List<SpannerSchema> schemas = tester.processBundle(Arrays.asList((Void) null));
    assertEquals(1, schemas.size());
    SpannerSchema schema = schemas.get(0);
    assertEquals(1, schema.getTables().size());
    SpannerSchema.Column column = SpannerSchema.Column.create("key", Type.int64());
    SpannerSchema.KeyPart keyPart = SpannerSchema.KeyPart.create("key", false);
    assertThat(schema.getColumns("test"), contains(column));
    assertThat(schema.getKeyParts("test"), contains(keyPart));
}
Also used : ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) Dialect(com.google.cloud.spanner.Dialect) Test(org.junit.Test)

Aggregations

Dialect (com.google.cloud.spanner.Dialect)14 Test (org.junit.Test)10 List (java.util.List)6 ProtoDialect (com.google.cloud.teleport.spanner.ExportProtos.ProtoDialect)4 Ddl (com.google.cloud.teleport.spanner.ddl.Ddl)4 PipelineResult (org.apache.beam.sdk.PipelineResult)4 KV (org.apache.beam.sdk.values.KV)4 Mutation (com.google.cloud.spanner.Mutation)3 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)3 Export (com.google.cloud.teleport.spanner.ExportProtos.Export)3 ArrayList (java.util.ArrayList)3 TimestampBound (com.google.cloud.spanner.TimestampBound)2 TableManifest (com.google.cloud.teleport.spanner.ExportProtos.TableManifest)2 HashMultimap (com.google.common.collect.HashMultimap)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 Collections (java.util.Collections)2 Map (java.util.Map)2 Transaction (org.apache.beam.sdk.io.gcp.spanner.Transaction)2 DoFn (org.apache.beam.sdk.transforms.DoFn)2 Timestamp (com.google.cloud.Timestamp)1