Skip to main content

Google Cloud Endpoints short tutorial - part 1

Here a short tutorial on Google Cloud Endpoints (briefly presented in the previous post (http://googlielmo.blogspot.it/2013/04/google-cloud-endpoints.html)). For this tutorial I am referring to Eclipse Indigo and the Google Eclipse Plugin with the App Engine SDK 1.7.6. A little knowledge of the Google Plugin and the Google App Engine is required to better understand this tutorial.

Project creation

Open Eclipse and then create a new Web Application Project through the Google Plugin button. Select a name and the default package for the project and uncheck the GWT usage: we don't need a front end. The project name chosen for this tutorial is endpointtutorial.

Coding

Suppose you have this simple entity:


public class MyEntity {
public String entityName;
public String entityDescr;
}


and the business logic to access this kind of object:


public class MyBusinessClass {
public MyEntity getMyEntity() {
MyEntity myEntity = new MyEntity();
myEntity.entityName = "Test Entity";
myEntity.entityDescr = "Entity to test Endpoints";

return myEntity;
 }
}


Now let's go to write our first Endpoint:


public class MyEndpoint {

public String getEntity() {
MyBusinessClass myBusinessClass = new MyBusinessClass();
MyEntity myEntity = myBusinessClass.getMyEntity();

  return myEntity;
}
}


You can notice that it's a standard Java class. It's just a wrapper for your business logic (implemented using whatever Java library you want). To turn it on a real Endpoint you have just to annotate the class itself and its methods with the Annotations provided by the App Engine SDK:


import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;

@Api(name = "myendpoint",
description = "This retrieves an instance of MyEntity.")
public class MyEndpoint {

public MyEntity getEntity() {
MyBusinessClass myBusinessClass = new MyBusinessClass();
MyEntity myEntity = myBusinessClass.getMyEntity();

return myEntity;
  }
}


The name parameter for the @Api annotation is the name for the new API.
This annotation operation causes an automatic update of the web.xml file of the project. The Google plugin should have added to it the following rows for the Endpoint mapping:


<servlet-name>SystemServiceServlet</servlet-name>
  <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
  <init-param>
   <param-name>services</param-name>
   <param-value>org.blogspot.endpointtutorial.endpoint.MyEndpoint</param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>SystemServiceServlet</servlet-name>
  <url-pattern>/_ah/spi/*</url-pattern>
 </servlet-mapping>


Local testing

Before proceeding you can test the new Endpoint on your local machine. Run (or debug) the project as Web Application, open a web browser and type the following URL:

http://localhost:8888/_ah/api/myendpoint/v1/myentity

Replace the local port if you use a different one than the default for the Google plugin Jetty instance. You should obtain a JSON output like this:


{
  "entityName" : "Test Entity",
  "entityDescr" : "Entity to test Endpoints"
}


The URL composition for the endpoint in this case is the following:

http://<hostname>:<port>/<endpoint_mapping>/<endpoint_name>/<endpoint_version>/<class_name_returned_in_lowercase>

Because it's possible to have more than one version of an endpoint, the plugin automatically sets the endpoint version to 1 (if not specified through the @Api annotation).
The endpoint of this example has just one method. But if you need to implement more than one method to be exposed you have only to annotate them through the @ApiMethod annotation:


@ApiMethod(httpMethod = "GET",
       name = "myentity.get", 
       path = "myentity/get")
public MyEntity getEntity() {
MyBusinessClass myBusinessClass = new MyBusinessClass();
MyEntity myEntity = myBusinessClass.getMyEntity();

return myEntity;
  }


You can also set the HTTP method, the name and the path for each single method.
Now to invoke locally this method by a web browser the URL changed this way:

http://localhost:8888/_ah/api/myendpoint/v1/myentity/get

Deploy to Google App Engine

If everything was fine, then you can proceed to deploy your application with the endpoints. First you have to create the application on your Google App Engine (GAE) space. Add the chosen name to the appengine-web.xml file of the project:

<application>endpointtutorial</application>

Check that the plugin already generated the descriptor for your APIs. You should find a file named myendpoint-v1.api into the war/WEB-INF folder of the project. Then right click on the project name and select Google -> Deploy to App Engine. The deploy process is the same as for all the applications to be deployed on GAE. If the deploy process went fine you could check the endpoint by a web browser:

https://endpointtutorial.appspot.com/_ah/api/myendpoint/v1/myentity/get

Don't forget to invoke it through HTTPS: otherwise you will obtain a 403 error.

Conclusions

To implement an Endpoint is a very simple process. Implement your business logic, implement a wrapper and then annotate it. The example shown in this tutorial is very simple, but I thought this is the best way to quickly understand this concept. I will try to go more in depth into other specific features (like security for example) in the future. In the second part of this tutorial (my next post) you will discover how the endpoints you have implemented can be easily and immediately ready to be consumed by an Android app. Stay tuned!

Comments

Popular posts from this blog

Streamsets Data Collector log shipping and analysis using ElasticSearch, Kibana and... the Streamsets Data Collector

One common use case scenario for the Streamsets Data Collector (SDC) is the log shipping to some system, like ElasticSearch, for real-time analysis. To build a pipeline for this particular purpose in SDC is really simple and fast and doesn't require coding at all. For this quick tutorial I will use the SDC logs as example. The log data will be shipped to Elasticsearch and then visualized through a Kibana dashboard. Basic knowledge of SDC, Elasticsearch and Kibana is required for a better understanding of this post. These are the releases I am referring to for each system involved in this tutorial: JDK 8 Streamsets Data Collector 1.4.0 ElasticSearch 2.3.3 Kibana 4.5.1 Elasticsearch and Kibana installation You should have your Elasticsearch cluster installed and configured and a Kibana instance pointing to that cluster in order to go on with this tutorial. Please refer to the official documentation for these two products in order to complete their installation (if you do

Exporting InfluxDB data to a CVS file

Sometimes you would need to export a sample of the data from an InfluxDB table to a CSV file (for example to allow a data scientist to do some offline analysis using a tool like Jupyter, Zeppelin or Spark Notebook). It is possible to perform this operation through the influx command line client. This is the general syntax: sudo /usr/bin/influx -database '<database_name>' -host '<hostname>' -username '<username>'  -password '<password>' -execute 'select_statement' -format '<format>' > <file_path>/<file_name>.csv where the format could be csv , json or column . Example: sudo /usr/bin/influx -database 'telegraf' -host 'localhost' -username 'admin'  -password '123456789' -execute 'select * from mem' -format 'csv' > /home/googlielmo/influxdb-export/mem-export.csv

Using Rapids cuDF in a Colab notebook

During last Spark+AI Summit Europe 2019 I had a chance to attend a talk from Miguel Martinez  who was presenting Rapids , the new Open Source framework from NVIDIA for GPU accelerated end-to-end Data Science and Analytics. Fig. 1 - Overview of the Rapids eco-system Rapids is a suite of Open Source libraries: cuDF cuML cuGraph cuXFilter I enjoied the presentation and liked the idea of this initiative, so I wanted to start playing with the Rapids libraries in Python on Colab , starting from cuDF, but the first attempt came with an issue that I eventually solved. So in this post I am going to share how I fixed it, with the hope it would be useful to someone else running into the same blocker. I am assuming here you are already familiar with Google Colab. I am using Python 3.x as Python 2 isn't supported by Rapids. Once you have created a new notebook in Colab, you need to check if the runtime for it is set to use Python 3 and uses a GPU as hardware accelerator. You