Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding java8 spanner sample #711

Merged
merged 3 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions appengine-java8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@
<module>multitenancy</module>
<module>oauth2</module>
<module>requests</module>
<module>search</module>
<module>sendgrid</module>

<module>remote-client</module>
<module>remote-server</module>

<module>search</module>

<module>sendgrid</module>

<module>spanner</module>

<module>static-files</module>

<module>taskqueues-deferred</module>
Expand Down
54 changes: 54 additions & 0 deletions appengine-java8/spanner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Google Cloud Spanner Sample

This sample demonstrates how to use [Google Cloud Spanner][spanner-docs]
from [Google App Engine standard environment][ae-docs].

[spanner-docs]: https://cloud.google.com/spanner/docs/
[ae-docs]: https://cloud.google.com/appengine/docs/java/


## Setup
- Install the [Google Cloud SDK](https://cloud.google.com/sdk/) and run:
```
gcloud init
```
If this is your first time creating an App engine application:
```
gcloud app create
```
- [Create a Spanner instance](https://cloud.google.com/spanner/docs/quickstart-console#create_an_instance).

- Update system properties in `[appengine-web.xml](src/main/webapp/WEB-INF/appengine-web.xml):
- Required : `SPANNER_INSTANCE`
- Optional : `SPANNER_DATABASE`,
A database will created using the `SPANNER_DATABASE` name if provided, else will be auto-generated.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One or the other???

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cleaned up to be just SPANNER_INSTANCE so its clearer.


## Endpoints
- `/run` : will run sample operations against the spanner instance in order. Individual tasks can be run
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/spanner ??

using the `task` query parameter. See [SpannerTasks](src/main/java/com/example/appengine/spanner/SpannerTasks.java)
for supported set of tasks.
Note : by default all the spanner example operations run in order, this operation may take a while to return.

## Running locally
-Authorize the local application:
```
gcloud auth application-default login
```
You may also [create and use service account credentials](https://cloud.google.com/docs/authentication/getting-started#creating_the_service_account).
- To run locally, we will be using the [Maven Jetty plugin](http://www.eclipse.org/jetty/documentation/9.4.x/jetty-maven-plugin.html)
```
mvn -DSPANNER_INSTANCE=my-spanner-instance jetty:run
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not tell them this - it's not really for J8 Std

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THIS IS TEMPORARY - explain why and the issue # they can follow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tracking issue.

```

To see the results of the local application, open
[http://localhost:8080/run](http://localhost:8080/run) in a web browser.
Note : by default all the spanner example operations run in order, this operation may take a while to show results.

## Deploying

$ mvn clean appengine:deploy

To see the results of the deployed sample application, open
`https://spanner-dot-PROJECTID.appspot.com/run` in a web browser.
Note : by default all the spanner example operations run in order, this operation may take a while to show results.

87 changes: 87 additions & 0 deletions appengine-java8/spanner/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2017 Google Inc.

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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.example.appengine</groupId>
<modelVersion>4.0.0</modelVersion>
<artifactId>appengine-spanner-j8</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

<parent>
<artifactId>appengine-java8-samples</artifactId>
<groupId>com.google.cloud</groupId>
<version>1.0.0</version>
<relativePath>..</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
<version>0.19.0-beta</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>1.9.53</version>
</dependency>
</dependencies>

<build>
<outputDirectory>
${project.build.directory}/${project.build.finalName}/WEB-INF/classes
</outputDirectory>

<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not have this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, just comment that this is hear due to a problem in the spanner library that we can't run locally under normal tooling -- GAE development server - GAE API's won't work here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

<version>9.4.6.v20170531</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>3.5.1</version>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<deploy.promote>true</deploy.promote>
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* Copyright 2017 Google Inc.
*
* <p>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
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>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.appengine.spanner;

import com.google.cloud.spanner.DatabaseAdminClient;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We like having comments about why this makes it so that we don't use web.xml

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

public class SpannerClient implements ServletContextListener {

private static String PROJECT_ID;
private static String INSTANCE_ID;
private static String DATABASE_ID;

// The initial connection can be an expensive operation -- We cache this Connection
// to speed things up. For this sample, keeping them here is a good idea, for
// your application, you may wish to keep this somewhere else.
private static Spanner spanner = null;
private static DatabaseAdminClient databaseAdminClient = null;
private static DatabaseClient databaseClient = null;

private static ServletContext sc;

private static void connect() throws IOException {
if (INSTANCE_ID == null) {
if (sc != null) {
sc.log("environment variable SPANNER_INSTANCE need to be defined.");
}
return;
}
SpannerOptions options = SpannerOptions.newBuilder().build();
PROJECT_ID = options.getProjectId();
spanner = options.getService();
databaseAdminClient = spanner.getDatabaseAdminClient();
}

static DatabaseAdminClient getDatabaseAdminClient() {
if (databaseAdminClient == null) {
try {
connect();
} catch (IOException e) {
if (sc != null) {
sc.log("getDatabaseAdminClient ", e);
}
}
}
if (databaseAdminClient == null) {
if (sc != null) {
sc.log("Spanner : Unable to connect");
}
}
return databaseAdminClient;
}

static DatabaseClient getDatabaseClient() {
if (databaseClient == null) {
databaseClient =
spanner.getDatabaseClient(DatabaseId.of(PROJECT_ID, INSTANCE_ID, DATABASE_ID));
}
return databaseClient;
}

@Override
public void contextInitialized(ServletContextEvent event) {
if (event != null) {
sc = event.getServletContext();
if (INSTANCE_ID == null) {
INSTANCE_ID = sc.getInitParameter("SPANNER_INSTANCE");
}
if (DATABASE_ID == null) {
DATABASE_ID = sc.getInitParameter("SPANNER_DATABASE");
}
}
//try system properties
if (INSTANCE_ID == null) {
INSTANCE_ID = System.getProperty("SPANNER_INSTANCE");
}
if (DATABASE_ID == null) {
DATABASE_ID = System.getProperty("SPANNER_DATABASE");
}

if (DATABASE_ID == null) {
DATABASE_ID = "db-" + UUID.randomUUID().toString().substring(0, 25);
}

try {
connect();
} catch (IOException e) {
if (sc != null) {
sc.log("SpannerConnection - connect ", e);
}
}
if (databaseAdminClient == null) {
if (sc != null) {
sc.log("SpannerConnection - No Connection");
}
}
if (sc != null) {
sc.log("ctx Initialized: " + INSTANCE_ID + " " + DATABASE_ID);
}
}

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
// App Engine does not currently invoke this method.
databaseAdminClient = null;
}

public static String getInstanceId() {
return INSTANCE_ID;
}

public static String getDatabaseId() {
return DATABASE_ID;
}
}
Loading