Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Last active March 28, 2017 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save branflake2267/38836e5f74c5f993f6a621e58743db7a to your computer and use it in GitHub Desktop.
Save branflake2267/38836e5f74c5f993f6a621e58743db7a to your computer and use it in GitHub Desktop.
App Engine - Java - Cloud Spanner Basic Setup. Used in the video for setting up the Cloud Spanner.
package com.example.appengine.gettingstartedjava.helloworld;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.ExecutionException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Mutation;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.TransactionContext;
import com.google.cloud.spanner.TransactionRunner.TransactionCallable;
@SuppressWarnings("serial")
@WebServlet(name = "users", value = "/users" )
public class UsersServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
out.println("Users:");
Spanner spanner = createSpannerService();
DatabaseClient dbClient = createDbClient(spanner);
spannerWriteTest(dbClient);
spannerReadTest(dbClient, out);
// Closes the client which will free up the resources used
try {
spanner.closeAsync()
.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
private Spanner createSpannerService() {
SpannerOptions options = SpannerOptions.newBuilder()
.build();
Spanner spanner = options.getService();
return spanner;
}
private DatabaseClient createDbClient(Spanner spanner) {
SpannerOptions options = spanner.getOptions();
String instance = "donnelson-dev-testing";
String database = "donnelson-db";
// Creates a database client
DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), instance, database));
return dbClient;
}
private void spannerWriteTest(DatabaseClient dbClient) {
dbClient.readWriteTransaction()
.run(new TransactionCallable<Void>() {
@Override
public Void run(TransactionContext transaction) throws Exception {
transaction.buffer(Mutation.newUpdateBuilder("users")
.set("id")
.to(1)
.set("name")
.to("Landan")
.set("age")
.to(41)
.build());
return null;
}
});
}
private void spannerReadTest(DatabaseClient dbClient, PrintWriter out) {
// Queries the database
ResultSet resultSet = dbClient.singleUse()
.executeQuery(Statement.of("SELECT name from users;"));
// Prints the results
while (resultSet.next()) {
out.print("" + resultSet.getString(0));
}
}
}
@branflake2267
Copy link
Author

Video that covers the creation of this class.
https://www.youtube.com/watch?v=VAIXpjhjCtc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment