18
3章(4) 他サービスとの連携 @siburu 1

Document10

  • Upload
    siburu

  • View
    101

  • Download
    0

Embed Size (px)

DESCRIPTION

10

Citation preview

Page 1: Document10

3章(4) 他サービスとの連携@siburu

1

Page 2: Document10

目次

1. Google Cloud Storageのセットアップ

2. (本書で扱うサンプルコードの)開発環境

3. 3章のまとめ

2

Page 3: Document10

1.1 Google Cloud Storage

• GCSとは:

• GCPにおけるハードディスクの役割

• Free tierが無い=使用には課金有効化が必須

3

Page 4: Document10

<<メモ>> 課金システム• 4 criteria

!

!

!

!

• (出典) https://developers.google.com/storage/pricing

課金対象 課金基準

Storage 使用サイズ(GB)×使用時間(日)に比例

Network 出力量に比例、入力は無料。出力先地域によって差

Custom metadata MetadataサイズもStorage/Network課金に加算

Operations 操作回数に比例。操作によって差(Class A/B/Free)

4

Page 5: Document10

1.2 使い方• Enabling (in project)

• Developers console -> APIS&AUTH -> APIs

• Creating buckets

• Must choose globally unique IDs for buckets

• Reliable option = To use a project ID as a bucket ID

• Managing data

• Storage browser : web-based tool

• gsutil : command-line tool

5

Page 6: Document10

Enabling APIs

6

Page 7: Document10

Bucket with the same name as project ID

7

Page 8: Document10

File in the bucket

8

Page 9: Document10

9

Page 10: Document10

1.3 BigQueryとの連携

• Extract

• bq extract <dataset>:<table> <dst-url>!

• Load

• bq load <dataset>:<table> <src-url> <schema-json>

10

Page 11: Document10

11

Page 12: Document10

12

Page 13: Document10

2. 開発環境• サンプルコード

• http://storage.googleapis.com/bigquery-e2e/downloads/bigquery_e2e_samples.zip

• ツール

• Pythonライブラリ https://developers.google.com/api-client-library/python/start/installation または bigquery_e2e_samples/lib

• Javaライブラリhttps://developers.google.com/api-client-library/java/apis/bigquery/v2 サンプルに含まれていない気がする・・・(本書では扱わない?)

• AppEngine https://developers.google.com/appengine/download#Google_App_Engine_SDK_for_Python サンプルプロジェクト: bigquery_e2e_samples/ch08/sensors/cloud

• Androidhttp://www.android.comサンプルプロジェクト: bigquery_e2e_samples/ch08/sensors/client

• 「全てのツールを一度にインストールするよりも、必要になった時に本節に戻ってくる方が良い」

13

Page 14: Document10

<<Python例>>認証# bq_cred.dat : 初回起動時にOAuth2認証プロセスを経て作成される!# client_secret.json : Developer Consoleで作成!!storage = Storage(‘bq_cred.dat’)!cred = storage.get()!!if cred is None or cred.invalid:! from oauth2client.client import flow_from_clientsecrets! from oauth2client import tools! flow = flow_from_clientsecrets('client_secret.json',! scope=‘https://www.googleapis.com/auth/'! + ‘bigquery’)! cred = tools.run_flow(flow, storage,! tools.argparser.parse_args([]))!!http = httplib2.Http()!http = cred.authorize(http)

14

Page 15: Document10

<<Python例>>クエリ発行

service = build('bigquery', 'v2', http=http)!!query_stmt = {'query':! 'SELECT primary_city, COUNT(primary_city) '! + 'FROM [reference.zip_codes] '! + 'GROUP BY 1 '! + 'LIMIT 10'}!!query = service.jobs().query(! projectId=PROJECT_ID, body=query_stmt)!!query_response = query.execute()

15

Page 16: Document10

<<Python例>>結果の使用

for row in query_response['rows']:! result_row = []! for field in row['f']:! result_row.append(field['v'])! print (‘\t').join(result_row)!!!# rowsとかfとかvとかは、JSON API formatに由来(詳しくは以下)!# https://developers.google.com/bigquery/docs/reference/v2/

16

Page 17: Document10

3. 3章のまとめ• 本章では以下を扱った。

• Google Cloud Platformセットアップ

• アカウントとプロジェクト

• 対話ツール

• BigQuery Webクライアント

• bq : BigQueryコマンドラインクライアント

• gsutil : GCSコマンドラインクライアント

• 開発環境へのポインタ

• Pythonライブラリ, Javaライブラリ, AppEngine, Android

• 本章の内容は時間と共に変わる。オンラインリソースへのポインタを充実させたので、説明通りにいかなければそちらへ

17

Page 18: Document10

以上

18