-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: Bit Reversed Sequence support (#2559)
- Loading branch information
1 parent
c34d51e
commit 7468a14
Showing
8 changed files
with
667 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
samples/snippets/src/main/java/com/example/spanner/AlterSequenceSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_alter_sequence] | ||
import com.google.cloud.spanner.DatabaseAdminClient; | ||
import com.google.cloud.spanner.DatabaseClient; | ||
import com.google.cloud.spanner.DatabaseId; | ||
import com.google.cloud.spanner.ResultSet; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerExceptionFactory; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import com.google.cloud.spanner.Statement; | ||
import com.google.common.collect.ImmutableList; | ||
import java.util.Objects; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class AlterSequenceSample { | ||
static void alterSequence() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
final String projectId = "my-project"; | ||
final String instanceId = "my-instance"; | ||
final String databaseId = "my-database"; | ||
alterSequence(projectId, instanceId, databaseId); | ||
} | ||
|
||
static void alterSequence(String projectId, String instanceId, String databaseId) { | ||
try (Spanner spanner = | ||
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
final DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient(); | ||
|
||
dbAdminClient | ||
.updateDatabaseDdl( | ||
instanceId, | ||
databaseId, | ||
ImmutableList.of( | ||
"ALTER SEQUENCE Seq SET OPTIONS " | ||
+ "(skip_range_min = 1000, skip_range_max = 5000000)"), | ||
null) | ||
.get(5, TimeUnit.MINUTES); | ||
|
||
System.out.println( | ||
"Altered Seq sequence to skip an inclusive range between 1000 and 5000000"); | ||
|
||
final DatabaseClient dbClient = | ||
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); | ||
|
||
Long insertCount = | ||
dbClient | ||
.readWriteTransaction() | ||
.run( | ||
transaction -> { | ||
try (ResultSet rs = | ||
transaction.executeQuery( | ||
Statement.of( | ||
"INSERT INTO Customers (CustomerName) VALUES " | ||
+ "('Lea'), ('Catalina'), ('Smith') " | ||
+ "THEN RETURN CustomerId"))) { | ||
while (rs.next()) { | ||
System.out.printf( | ||
"Inserted customer record with CustomerId: %d\n", rs.getLong(0)); | ||
} | ||
return Objects.requireNonNull(rs.getStats()).getRowCountExact(); | ||
} | ||
}); | ||
System.out.printf("Number of customer records inserted is: %d\n", insertCount); | ||
} catch (ExecutionException e) { | ||
// If the operation failed during execution, expose the cause. | ||
throw SpannerExceptionFactory.asSpannerException(e.getCause()); | ||
} catch (InterruptedException e) { | ||
// Throw when a thread is waiting, sleeping, or otherwise occupied, | ||
// and the thread is interrupted, either before or during the activity. | ||
throw SpannerExceptionFactory.propagateInterrupt(e); | ||
} catch (TimeoutException e) { | ||
// If the operation timed out propagate the timeout | ||
throw SpannerExceptionFactory.propagateTimeout(e); | ||
} | ||
} | ||
} | ||
// [END spanner_alter_sequence] |
Oops, something went wrong.