Skip to content

RealKinetic/codelab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Google Code Lab

About

In this codelab, we will be building an application that combines several GCP services to build a fully functioning application. The application we will build utilizes Google App Engine for our user traffic and backend processing tasks, Google Cloud SQL for storage, and Google Cloud NL for sentiment analysis.

This codelab builds upon some of Google's Codelabs:

How to Use this Codelab

This codelab may be followed along by cloning this repository, then following this README.

This codelab should be completed using the Google Cloud Shell. This is what all of the Google provided codelabs recommend using as well. Cloud Shell is accessed from the Google Cloud Console, by clicking the button to the right of the search box:

Activate Google Cloud Shell

I recommend working through all exercises in the Google Codelabs, and I suggest selecting Novice. Your settings for each codelab should look something like this:

Google Codelab Settings

You may use an existing Cloud Project or create a new Cloud Project for use during this codelab. Please note, if you use Qwiklabs your project will be reset after each codelab - for this reason I recommend using your own Cloud Project.

NOTE: This codelab might cost between $1 and $3 in compute resources.

STEP 0: Getting Started with Google App Engine

We will use Google App Engine to serve user requests, perform backend processing, and to host our static resources.

Work through Google's Getting Started with App Engine (Python) codelab, then move on to STEP 1.

STEP 1: Getting Started with Google Cloud Natural Language API

We will use Google Cloud Natural Language API to conduct sentiment analysis on entities we fetch from Hacker News. In this step, we will introduce you to the basics of the APIs.

Work through Google's Entity and Sentiment Analysis with the Natural Language API codelab, then move on to STEP 2.

NOTE: Do not forget to enable the Cloud Natural Language API. To do this, under API Manager, goto Library, then find Cloud Natural Language and click it. Click enable.

STEP 2: Getting Started with Google Cloud SQL MySQL

We will use Google Cloud SQL to store the results of our sentiment analysis.

Work through Google's Create a Managed MySQL database with Cloud SQL codelab, then move on to STEP 3.

NOTE: Select Second Generation when creating your instance.

NOTE: When creating your instance, be sure to set a root password.

NOTE: Do NOT delete your instance yet, we will use it in STEP 3.

STEP 3: Putting the Pieces Together

In this step, we will build on the prior steps and integrate App Engine, SQL, and Natural Language API.

All steps are meant to be run within the Cloud Shell you used in the prior steps.

A. Clone the Codelab Repo

Clone the codelab repo - where you are reading this.

$ mkdir code && cd code
$ git clone https://github.com/RealKinetic/codelab.git
$ cd codelab

When deploying a Python app to App Engine, you should bundle your dependencies. We have included a helper to do this for you:

$ make install

You should also install the MySQL Client Library:

$ sudo apt-get install libmysqlclient-dev

B. Review app.yaml, and handler setup

This app uses Flask, a simple Python framework.

Open app.yaml, note that we map a static route to / and that we use a "catch-all" route (.*) to handle all other requests.

Our catch-all is mapped to app which is exposed from src/handler/__init__.py. Here, we map all of our endpoints to request handlers. Take a moment and review these handlers.

C. Deploy Your Application

Deploy your application to App Engine:

gcloud app deploy app.yaml

Then verify that you can access it by viewing http://<project-id>.appspot.com in your browser. You should see the following:

Initial Deployed App

D. Implement Cloud Natural Language Call

You will need to write the payload for analyze entities in src/api/natural_language.py, the content is in the text variable.

    body = {
        'document': {
            'type': 'PLAIN_TEXT',
            'content': text,
        },
        'encoding_type': encoding,
    }

E. Configure the SQL Connection

Configure the connection to Cloud SQL in src/config/envs/deployed.py.

DSN = (
    'mysql+mysqldb://USER_NAME:PASSWORD@/DATABASE_NAME?unix_socket=/cloudsql/'
    'PROJECT_ID:REGION:DATABASE_NAME'
)

You can find the variables in the SQL tab of cloud console.

F. Review SQL Usage

You can review the usage of Cloud SQL in src/api/highest_seen.py. In this project, we are using SQL Alchemy.

G. Try it Out

If you now click Get Rank, a request to Hacker News will be made, then the NL API used to populate a table within Cloud SQL. You can view the data in Cloud SQL in the HackerNews Avg tab of the app.

If everything went correctly, you should see some results:

Results

General Notes

Debugging

To debug your deployed app, use the log viewer:

Log Viewer

Setup the DB.

If you followed the Google Cloud SQL codelab, you can skip this step.

You will need to create a MySQL Database named codelab.

$ mysql -u root -p
> CREATE DATABASE codelab;