项目作者: RealKinetic

项目描述 :
Codelab for Google Cloud (BigQuery, NLP, GAE)
高级语言: JavaScript
项目地址: git://github.com/RealKinetic/codelab.git
创建时间: 2017-03-08T19:11:51Z
项目社区:https://github.com/RealKinetic/codelab

开源协议:

下载


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.

  1. $ mkdir code && cd code
  2. $ git clone https://github.com/RealKinetic/codelab.git
  3. $ 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:

  1. $ make install

You should also install the MySQL Client Library:

  1. $ 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:

  1. 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.

  1. body = {
  2. 'document': {
  3. 'type': 'PLAIN_TEXT',
  4. 'content': text,
  5. },
  6. 'encoding_type': encoding,
  7. }

E. Configure the SQL Connection

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

  1. DSN = (
  2. 'mysql+mysqldb://USER_NAME:PASSWORD@/DATABASE_NAME?unix_socket=/cloudsql/'
  3. 'PROJECT_ID:REGION:DATABASE_NAME'
  4. )

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.

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