Java Code Examples for com.google.cloud.spanner.Struct#getString()

The following examples show how to use com.google.cloud.spanner.Struct#getString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatabaseClientSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of read only transaction. */
// [TARGET readOnlyTransaction()]
// [VARIABLE my_singer_id]
// [VARIABLE my_album_id]
public String readOnlyTransaction(long singerId, long albumId) {
  // [START readOnlyTransaction]
  String singerColumn = "FirstName";
  String albumColumn = "AlbumTitle";
  String albumTitle = null;
  // ReadOnlyTransaction should be closed to prevent resource leak.
  try (ReadOnlyTransaction txn = dbClient.readOnlyTransaction()) {
    Struct singerRow =
        txn.readRow("Singers", Key.of(singerId), Collections.singleton(singerColumn));
    Struct albumRow =
        txn.readRow("Albums", Key.of(singerId, albumId), Collections.singleton(albumColumn));
    singerRow.getString(singerColumn);
    albumTitle = albumRow.getString(albumColumn);
  }
  // [END readOnlyTransaction]
  return albumTitle;
}
 
Example 2
Source File: DatabaseClientSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of read only transaction with timestamp bound. */
// [TARGET readOnlyTransaction(TimestampBound)]
// [VARIABLE my_singer_id]
// [VARIABLE my_album_id]
public String readOnlyTransactionTimestamp(long singerId, long albumId) {
  // [START readOnlyTransactionTimestamp]
  String singerColumn = "FirstName";
  String albumColumn = "AlbumTitle";
  String albumTitle = null;
  // ReadOnlyTransaction should be closed to prevent resource leak.
  try (ReadOnlyTransaction txn =
      dbClient.readOnlyTransaction(TimestampBound.ofExactStaleness(10, TimeUnit.SECONDS))) {
    Struct singerRow =
        txn.readRow("Singers", Key.of(singerId), Collections.singleton(singerColumn));
    Struct albumRow =
        txn.readRow("Albums", Key.of(singerId, albumId), Collections.singleton(albumColumn));
    singerRow.getString(singerColumn);
    albumTitle = albumRow.getString(albumColumn);
  }
  // [END readOnlyTransactionTimestamp]
  return albumTitle;
}
 
Example 3
Source File: DatabaseClientSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of using {@link TransactionManager}. */
// [TARGET transactionManager()]
// [VARIABLE my_singer_id]
public void transactionManager(final long singerId) throws InterruptedException {
  // [START transactionManager]
  try (TransactionManager manager = dbClient.transactionManager()) {
    TransactionContext txn = manager.begin();
    while (true) {
      try {
        String column = "FirstName";
        Struct row = txn.readRow("Singers", Key.of(singerId), Collections.singleton(column));
        String name = row.getString(column);
        txn.buffer(
            Mutation.newUpdateBuilder("Singers").set(column).to(name.toUpperCase()).build());
        manager.commit();
        break;
      } catch (AbortedException e) {
        Thread.sleep(e.getRetryDelayInMillis() / 1000);
        txn = manager.resetForRetry();
      }
    }
  }
  // [END transactionManager]
}
 
Example 4
Source File: ExportTransform.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
@Override
public String getDestination(Struct element) {
  // The input is a PCollection of rows from all tables.
  // That has to be demultiplexed into a separate destination for each table.
  // This is done using the first element of each table that is the table name.
  return element.getString(0);
}
 
Example 5
Source File: DatabaseClientSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
/** Example of single use. */
// [TARGET singleUse()]
// [VARIABLE my_singer_id]
public String singleUse(long singerId) {
  // [START singleUse]
  String column = "FirstName";
  Struct row =
      dbClient.singleUse().readRow("Singers", Key.of(singerId), Collections.singleton(column));
  String firstName = row.getString(column);
  // [END singleUse]
  return firstName;
}
 
Example 6
Source File: DatabaseClientSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
/** Example of single use with timestamp bound. */
// [TARGET singleUse(TimestampBound)]
// [VARIABLE my_singer_id]
public String singleUseStale(long singerId) {
  // [START singleUseStale]
  String column = "FirstName";
  Struct row =
      dbClient
          .singleUse(TimestampBound.ofMaxStaleness(10, TimeUnit.SECONDS))
          .readRow("Singers", Key.of(singerId), Collections.singleton(column));
  String firstName = row.getString(column);
  // [END singleUseStale]
  return firstName;
}
 
Example 7
Source File: DatabaseClientSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
/** Example of single use read only transaction. */
// [TARGET singleUseReadOnlyTransaction()]
// [VARIABLE my_singer_id]
public Timestamp singleUseReadOnlyTransaction(long singerId) {
  // [START singleUseReadOnlyTransaction]
  String column = "FirstName";
  ReadOnlyTransaction txn = dbClient.singleUseReadOnlyTransaction();
  Struct row = txn.readRow("Singers", Key.of(singerId), Collections.singleton(column));
  row.getString(column);
  Timestamp timestamp = txn.getReadTimestamp();
  // [END singleUseReadOnlyTransaction]
  return timestamp;
}
 
Example 8
Source File: DatabaseClientSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
/** Example of single use read only transaction with timestamp bound. */
// [TARGET singleUseReadOnlyTransaction(TimestampBound)]
// [VARIABLE my_singer_id]
public Timestamp singleUseReadOnlyTransactionTimestamp(long singerId) {
  // [START singleUseReadOnlyTransactionTimestamp]
  String column = "FirstName";
  ReadOnlyTransaction txn =
      dbClient.singleUseReadOnlyTransaction(TimestampBound.ofMaxStaleness(10, TimeUnit.SECONDS));
  Struct row = txn.readRow("Singers", Key.of(singerId), Collections.singleton(column));
  row.getString(column);
  Timestamp timestamp = txn.getReadTimestamp();
  // [END singleUseReadOnlyTransactionTimestamp]
  return timestamp;
}
 
Example 9
Source File: Poller.java    From spanner-event-exporter with Apache License 2.0 4 votes vote down vote up
private void getSchema() {
  ResultSet resultSet;
  try (Scope ss =
      Tracing.getTracer()
          .spanBuilderWithExplicitParent(SAMPLE_SPAN, null)
          .setSampler(Samplers.alwaysSample())
          .startScopedSpan()) {
    resultSet = dbClient.readOnlyTransaction().executeQuery(schemaQuery);
  }

  final SchemaBuilder.FieldAssembler<Schema> avroSchemaBuilder =
      SchemaBuilder.record(tableName).namespace(avroNamespace).fields();
  log.debug("Getting Schema");

  log.debug("Processing  Schema");
  while (resultSet.next()) {
    log.debug("Making Avro Schema");
    final Struct currentRow = resultSet.getCurrentRowAsStruct();

    final String name = currentRow.getString(0);
    final String type = currentRow.getString(1);
    spannerSchema.put(name, type);
    log.debug("Binding Avro Schema");
    // TODO(JR): Need to strip out size from the type object and add it to the avro datatype i.e
    // STRING(MAX) vs STRING(1024)
    // Fixed length strings will be unsupported in the first release.
    switch (type) {
      case "ARRAY":
        log.debug("Made ARRAY");
        avroSchemaBuilder.name(name).type().array();
        break;
      case "BOOL":
        log.debug("Made BOOL");
        avroSchemaBuilder.name(name).type().booleanType().noDefault();
        break;
      case "BYTES":
        log.debug("Made BYTES");
        avroSchemaBuilder.name(name).type().bytesType().noDefault();
        break;
      case "DATE":
        // Date handled as String type
        log.debug("Made DATE");
        avroSchemaBuilder.name(name).type().stringType().noDefault();
        break;
      case "FLOAT64":
        log.debug("Made FLOAT64");
        avroSchemaBuilder.name(name).type().doubleType().noDefault();
        break;
      case "INT64":
        log.debug("Made INT64");
        avroSchemaBuilder.name(name).type().longType().noDefault();
        break;
      case "STRING(MAX)":
        log.debug("Made STRING");
        avroSchemaBuilder.name(name).type().stringType().noDefault();
        break;
      case "TIMESTAMP":
        log.debug("Made TIMESTAMP");
        avroSchemaBuilder.name(name).type().stringType().noDefault();
        break;
      default:
        log.error("Unknown Schema type when generating Avro Schema: " + type);
        stop();
        System.exit(1);
        break;
    }
  }

  log.debug("Ending Avro Record");
  avroSchema = avroSchemaBuilder.endRecord();

  log.debug("Made Avro Schema");

  final Set<String> keySet = spannerSchema.keySet();

  for (String k : keySet) {
    log.debug("-------------------------- ColName: " + k + " Type: " + spannerSchema.get(k));
  }

  log.debug("--------------------------- " + avroSchema.toString());
}
 
Example 10
Source File: ExportTransform.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
@Override
public GenericRecord formatRecord(Struct record) {
  String table = record.getString(0);
  Schema schema = sideInput(avroSchemas).get(table).get();
  return new SpannerRecordConverter(schema).convert(record);
}