Search in sources :

Example 11 with Partition

use of com.google.cloud.spanner.Partition in project google-cloud-java by GoogleCloudPlatform.

the class BatchClientSnippets method partitionReadUsingIndex.

void partitionReadUsingIndex() {
    // [START partition_read_using_index]
    final BatchReadOnlyTransaction txn = batchClient.batchReadOnlyTransaction(TimestampBound.strong());
    List<Partition> partitions = txn.partitionReadUsingIndex(PartitionOptions.getDefaultInstance(), "Singers", "SingerId", KeySet.all(), Arrays.asList("SingerId", "FirstName", "LastName"));
    for (Partition p : partitions) {
        try (ResultSet results = txn.execute(p)) {
            while (results.next()) {
                long singerId = results.getLong(0);
                String firstName = results.getString(1);
                String lastName = results.getString(2);
                System.out.println("[" + singerId + "] " + firstName + " " + lastName);
            }
        }
    }
// [END partition_read_using_index]
}
Also used : Partition(com.google.cloud.spanner.Partition) BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet)

Example 12 with Partition

use of com.google.cloud.spanner.Partition in project google-cloud-java by GoogleCloudPlatform.

the class BatchClientSnippets method partitionQuery.

void partitionQuery() {
    // [START partition_query]
    final BatchReadOnlyTransaction txn = batchClient.batchReadOnlyTransaction(TimestampBound.strong());
    List<Partition> partitions = txn.partitionQuery(PartitionOptions.getDefaultInstance(), Statement.of("SELECT SingerId, FirstName, LastName FROM Singers"));
    for (final Partition p : partitions) {
        try (ResultSet results = txn.execute(p)) {
            while (results.next()) {
                long singerId = results.getLong(0);
                String firstName = results.getString(1);
                String lastName = results.getString(2);
                System.out.println("[" + singerId + "] " + firstName + " " + lastName);
            }
        }
    }
// [END partition_query]
}
Also used : Partition(com.google.cloud.spanner.Partition) BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet)

Example 13 with Partition

use of com.google.cloud.spanner.Partition in project spanner-jdbc by olavloite.

the class CloudSpannerStatement method execute.

@Override
public boolean execute(String sql) throws SQLException {
    String[] sqlTokens = getTokens(sql);
    CustomDriverStatement custom = getCustomDriverStatement(sqlTokens);
    if (custom != null)
        return custom.execute(sqlTokens);
    Statement statement = null;
    boolean ddl = isDDLStatement(sqlTokens);
    if (!ddl) {
        try {
            statement = CCJSqlParserUtil.parse(sanitizeSQL(sql));
        } catch (JSQLParserException | TokenMgrException e) {
            throw new CloudSpannerSQLException("Error while parsing sql statement " + sql + ": " + e.getLocalizedMessage(), Code.INVALID_ARGUMENT, e);
        }
    }
    if (!ddl && statement instanceof Select) {
        determineForceSingleUseReadContext((Select) statement);
        if (!isForceSingleUseReadContext() && getConnection().isBatchReadOnly()) {
            List<Partition> partitions = partitionQuery(com.google.cloud.spanner.Statement.of(sql));
            currentResultSets = new ArrayList<>(partitions.size());
            for (Partition p : partitions) {
                currentResultSets.add(new CloudSpannerPartitionResultSet(this, getBatchReadOnlyTransaction(), p, sql));
            }
        } else {
            try (ReadContext context = getReadContext()) {
                com.google.cloud.spanner.ResultSet rs = context.executeQuery(com.google.cloud.spanner.Statement.of(sql));
                currentResultSets = Arrays.asList(new CloudSpannerResultSet(this, rs, sql));
                currentResultSetIndex = 0;
                lastUpdateCount = -1;
            }
        }
        return true;
    } else {
        lastUpdateCount = executeUpdate(sql);
        currentResultSetIndex = 0;
        currentResultSets = null;
        return false;
    }
}
Also used : Partition(com.google.cloud.spanner.Partition) PreparedStatement(java.sql.PreparedStatement) Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) CloudSpannerPartitionResultSet(nl.topicus.jdbc.resultset.CloudSpannerPartitionResultSet) TokenMgrException(net.sf.jsqlparser.parser.TokenMgrException) ReadContext(com.google.cloud.spanner.ReadContext) Select(net.sf.jsqlparser.statement.select.Select) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 14 with Partition

use of com.google.cloud.spanner.Partition in project spanner-jdbc by olavloite.

the class CloudSpannerPreparedStatement method execute.

@Override
public boolean execute() throws SQLException {
    CustomDriverStatement custom = getCustomDriverStatement(sqlTokens);
    if (custom != null)
        return custom.execute(sqlTokens);
    Statement statement = null;
    boolean ddl = isDDLStatement();
    if (!ddl) {
        try {
            statement = CCJSqlParserUtil.parse(sanitizeSQL(sql));
        } catch (JSQLParserException | TokenMgrException e) {
            throw new CloudSpannerSQLException(PARSE_ERROR + sql + ": " + e.getLocalizedMessage(), Code.INVALID_ARGUMENT, e);
        }
    }
    if (!ddl && statement instanceof Select) {
        determineForceSingleUseReadContext((Select) statement);
        com.google.cloud.spanner.Statement.Builder builder = createSelectBuilder(statement, sql);
        if (!isForceSingleUseReadContext() && getConnection().isBatchReadOnly()) {
            List<Partition> partitions = partitionQuery(builder.build());
            currentResultSets = new ArrayList<>(partitions.size());
            for (Partition p : partitions) {
                currentResultSets.add(new CloudSpannerPartitionResultSet(this, getBatchReadOnlyTransaction(), p, sql));
            }
        } else {
            try (ReadContext context = getReadContext()) {
                com.google.cloud.spanner.ResultSet rs = context.executeQuery(builder.build());
                currentResultSets = Arrays.asList(new CloudSpannerResultSet(this, rs, sql));
                currentResultSetIndex = 0;
                lastUpdateCount = -1;
            }
        }
        return true;
    } else {
        lastUpdateCount = executeUpdate();
        currentResultSets = null;
        currentResultSetIndex = 0;
        return false;
    }
}
Also used : Partition(com.google.cloud.spanner.Partition) PreparedStatement(java.sql.PreparedStatement) Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) CloudSpannerPartitionResultSet(nl.topicus.jdbc.resultset.CloudSpannerPartitionResultSet) TokenMgrException(net.sf.jsqlparser.parser.TokenMgrException) ReadContext(com.google.cloud.spanner.ReadContext) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) Select(net.sf.jsqlparser.statement.select.Select) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 15 with Partition

use of com.google.cloud.spanner.Partition in project spanner-jdbc by olavloite.

the class CloudSpannerPartitionResultSetTest method createSubject.

private CloudSpannerPartitionResultSet createSubject() {
    Partition partition = mock(Partition.class);
    BatchReadOnlyTransaction transaction = mock(BatchReadOnlyTransaction.class);
    ResultSet rs = CloudSpannerResultSetTest.getMockResultSet();
    when(transaction.execute(partition)).thenReturn(rs);
    return new CloudSpannerPartitionResultSet(mock(CloudSpannerStatement.class), transaction, partition, "SELECT * FROM FOO");
}
Also used : Partition(com.google.cloud.spanner.Partition) CloudSpannerStatement(nl.topicus.jdbc.statement.CloudSpannerStatement) BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet)

Aggregations

Partition (com.google.cloud.spanner.Partition)20 Timestamp (com.google.cloud.Timestamp)11 BatchTransactionId (com.google.cloud.spanner.BatchTransactionId)11 FakeBatchTransactionId (com.google.cloud.spanner.FakeBatchTransactionId)11 ReadQueryUpdateTransactionOption (com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption)11 PartitionOptions (com.google.cloud.spanner.PartitionOptions)11 TimestampBound (com.google.cloud.spanner.TimestampBound)11 Test (org.junit.Test)11 BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)8 Struct (com.google.cloud.spanner.Struct)7 ResultSet (com.google.cloud.spanner.ResultSet)5 PreparedStatement (java.sql.PreparedStatement)3 BatchClient (com.google.cloud.spanner.BatchClient)2 ReadContext (com.google.cloud.spanner.ReadContext)2 SpannerException (com.google.cloud.spanner.SpannerException)2 JSQLParserException (net.sf.jsqlparser.JSQLParserException)2 TokenMgrException (net.sf.jsqlparser.parser.TokenMgrException)2 Statement (net.sf.jsqlparser.statement.Statement)2 Select (net.sf.jsqlparser.statement.select.Select)2 CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)2