{"id":302,"date":"2019-12-09T12:35:11","date_gmt":"2019-12-09T03:35:11","guid":{"rendered":"http:\/\/localhost:8000\/?p=302"},"modified":"2021-01-17T12:38:48","modified_gmt":"2021-01-17T03:38:48","slug":"bigquery-python","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2019\/12\/bigquery-python.html","title":{"rendered":"BigQuery\u306epython client\uff08google-cloud-bigquery\uff09\u306e\u4f7f\u3044\u65b9"},"content":{"rendered":"
BigQuery\u306epython client\uff08google-cloud-bigquery\uff09\u306e\u4f7f\u3044\u65b9\u3092\u30e1\u30e2\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n
export GOOGLE_APPLICATION_CREDENTIALS="\/hoge\/fuga\/key_file.json"<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n- \u30e9\u30a4\u30d6\u30e9\u30ea\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\n
pip install google-cloud-bigquery<\/code><\/pre>\n<\/li>\n- \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u751f\u6210\n
from google.cloud.bigquery import Client\nclient: Client = Client() # \u74b0\u5883\u5909\u6570 GOOGLE_APPLICATION_CREDENTIALS\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u524d\u63d0\u3002\u5f15\u6570\u3067\u6e21\u3059\u3053\u3068\u3082\u3067\u304d\u308b<\/code><\/pre>\n<\/li>\n<\/ul>\n\u30c7\u30fc\u30bf\u306e\u53d6\u5f97<\/h2>\n\u30af\u30a8\u30ea\u3092\u4f7f\u3046<\/h3>\nfrom google.cloud.bigquery.table import RowIterator, Row\nfrom google.cloud.bigquery.job import QueryJob\nimport itertools\n\n# \u30af\u30a8\u30ea\u5b9a\u7fa9\nquery: str = "SELECT item1, item2 FROM HOGE;"\n# JOB\u5b9f\u884c\nquery_job: QueryJob = client.query(query)\nrows: RowIterator = query_job.result() # API\u30ea\u30af\u30a8\u30b9\u30c8\u767a\u751f\uff0b\u7d50\u679c\u5f85\u3061\u5408\u308f\u305b\n# \u5168\u4ef6\u5229\u7528\nprint(f"total_rows: {rows.num_results}")\nfor row in rows:\n print(f"item1: {row.item1}, item2: {row.item2}")\n# 1\u4ef6\u3060\u3051\u5b9f\u884c\uff08\u5fc5\u305a\u3057\u3082\u3053\u3046\u3059\u308b\u5fc5\u8981\u306f\u5168\u304f\u306a\u3044\u3051\u3069\uff09\nrow: Optional[table.Row] = next(itertools.islice(rows, 1), None)\nif row is not None:\n print(f"item1: {item1}, item2: {item2}")<\/code><\/pre>\n\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210<\/h2>\n\u30af\u30a8\u30ea\u3092\u4f7f\u3046<\/h3>\n# \u30af\u30a8\u30ea\u5b9a\u7fa9\uff08SELECT\u3067\u65e2\u5b58\u30c6\u30fc\u30d6\u30eb\u3092\u5143\u306b\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\uff09\nquery: str = """\nCREATE OR REPLACE TABLE dataset_name.hoge\nOPTIONS (expiration_timestamp=TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 3 HOUR)) -- 3\u6642\u9593\u3067\u524a\u9664\uff08\u30c6\u30f3\u30dd\u30e9\u30ea\u30c6\u30fc\u30d6\u30eb\u6271\u3044\uff09\nAS\nSELECT\n f.*\n , CAST(NULL AS INT64) AS item3 -- \u7a7a\u306e\u30ab\u30e9\u30e0\u3092\u8ffd\u52a0\u3057\u305f\u3044\u5834\u5408\u306fCAST\u304c\u5fc5\u8981\nFROM\n dataset_name.fuga f\n-- \u30c7\u30fc\u30bf\u306a\u3057\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u308a\u305f\u3051\u308c\u3070\u3001\u4ee5\u4e0b\u3092\u8ffd\u52a0\n-- LIMIT 0\n"""\n# \u30af\u30a8\u30ea\u5b9a\u7fa9\uff08\u30b9\u30ad\u30fc\u30de\u5b9a\u7fa9\u3092\u3057\u3066\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\uff09\nquery: str = """\nCREATE OR REPLACE TABLE dataset_name.hoge (\n item1 STRING NOT NULL\n , item2 STRING NOT NULL\n , item3 INT64\n)\nOPTIONS (expiration_timestamp=TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 3 HOUR)) -- 3\u6642\u9593\u3067\u524a\u9664\uff08\u30c6\u30f3\u30dd\u30e9\u30ea\u30c6\u30fc\u30d6\u30eb\u6271\u3044\uff09\n"""\n# \u30af\u30a8\u30ea\u5b9f\u884c\nquery_job: QueryJob = client.query(query)\nquery_job.result() # API\u30ea\u30af\u30a8\u30b9\u30c8<\/code><\/pre>\n\u30c7\u30fc\u30bf\u306e\u4e00\u62ec\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9<\/h2>\nclient.insert_rows\u3092\u4f7f\u3046<\/h3>\n
\u3053\u308c\u306f\u3001\u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u30a4\u30f3\u30b5\u30fc\u30c8\u3092\u884c\u3046\u95a2\u6570\u306a\u306e\u3067\u3059\u304c\u3001\u3053\u3061\u3089<\/a>\u306b\u3042\u308b\u3088\u3046\u306b\u3001\u30c7\u30fc\u30bf\u3092\u5229\u7528\u53ef\u80fd\u306b\u306a\u308b\u307e\u3067\u6570\u79d2\u7a0b\u5ea6\u306e\u9045\u5ef6\u304c\u767a\u751f\u3057\u307e\u3059\uff08\u5b9f\u969b\u306b\u306f\u3082\u3063\u3068\u9045\u5ef6\u304c\u767a\u751f\u3059\u308b\u3053\u3068\u3082\u3042\u308a\u307e\u3059\uff09\u3002\u305d\u306e\u305f\u3081\u3001\u30a4\u30f3\u30b5\u30fc\u30c8\u7d50\u679c\u3092\u6b21\u306e\u51e6\u7406\u3067\u3059\u3050\u306b\u4f7f\u3046\u3088\u3046\u306a\u30b1\u30fc\u30b9\u3067\u306f\u5229\u7528\u3067\u304d\u307e\u305b\u3093\u3002\u306a\u304a\u3001\u9045\u5ef6\u304c\u767a\u751f\u3057\u3066\u3044\u308b\u5834\u5408\u3067\u3082\u30a8\u30e9\u30fc\u306b\u306f\u306a\u308a\u307e\u305b\u3093\u3002<\/p>\nfrom more_itertools import chunked\nfrom typing import Tuple, List\nfrom google.cloud.bigquery.table import Table\n\ntable: Table = client.get_table("dataset_name.hoge") # API\u3067\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9\u3092\u53d6\u5f97\nrows: List[Tuple[str, str, str]] = [("aa1", "bb1", "cc1"), ("aa2", "bb2", "cc2")]\n\n# client.insert_rows\u3067\u4e00\u5ea6\u306b\u51e6\u7406\u3067\u304d\u308b\u6700\u5927\u4ef6\u6570\u304c1\u4e07\u4ef6\u306a\u306e\u3067\u30011\u4e07\u4ef6\u305a\u3064\u51e6\u7406\nfor chuncked_rows in list(chunked(rows, 10000)):\n errors = client.insert_rows(table, chuncked_rows) # API\u30ea\u30af\u30a8\u30b9\u30c8\n print(f"errors: {errors}")<\/code><\/pre>\nclient.load_table_from_dataframe\u3092\u4f7f\u3046<\/h3>\n
\u3053\u3061\u3089\u306f\u9045\u5ef6\u306f\u767a\u751f\u305b\u305a\u3001\u540c\u671f\u7684\u306b\u30c7\u30fc\u30bf\u30ed\u30fc\u30c9\u3055\u308c\u308b\u305f\u3081\u3001\u5b89\u5fc3\u3067\u3059\u3002
\n\u3057\u304b\u3057\u3001NULL(None)<\/code>\u3092insert\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u3001pyarrow.lib.ArrowTypeError: Expected a string or bytes object, got a 'int' object<\/code>\u304c\u767a\u751f\u3059\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u308c\u306e\u56de\u907f\u65b9\u6cd5\u304c\u5206\u304b\u3089\u305a\u56f0\u3063\u3066\u3044\u307e\u3059\uff08LoadJobConfig<\/code>\u306enull_marker<\/code>\u306fCSV\u3067\u3057\u304b\u4f7f\u3048\u306a\u3044\u3057\u3001\u3053\u3061\u3089<\/a>\u3092\u898b\u308b\u3068\u73fe\u72b6\u3067\u306fDataFrame\u5074\u306e\u8a2d\u5b9a\u3067\u3082\u30c0\u30e1\u305d\u3046\u3067\u3059\uff09\u3002\u306a\u306e\u3067\u3001\u3069\u3046\u3057\u3066\u3082NULL<\/code>\u3092insert\u3057\u305f\u3044\u5834\u5408\u306f\u3001client.load_table_from_csv<\/code>\u3092\u4f7f\u3046\u307b\u3046\u304c\u3044\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002
\n\u306a\u304a\u3001import\u4e0d\u8981\u3067\u3059\u304c\u3001\u4e8b\u524d\u306bpip install pyarrow<\/code>\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\nimport pandas as pd\nfrom pandas.core.frame import DataFrame\nfrom google.cloud.bigquery.table import Table\nfrom google.cloud.bigquery.job import LoadJob\nfrom google.cloud.bigquery import LoadJobConfig\nfrom typing import List\n\n# \u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9\u53d6\u5f97\ntable: Table = client.get_table("dataset_name.hoge") # API\u30ea\u30af\u30a8\u30b9\u30c8\n\n# DataFrame\u751f\u6210\ncolumns: List[str] = list(map(lambda field: field.name, table.schema))\ndf: DataFrame = pd.DataFrame(columns=columns)\nfor row in [["aa1", "bb1", "cc1"], ["aa2", "bb2", "cc2"]]:\n df = df.append(pd.Series(row, index=df.columns), ignore_index=True)\n\n# BigQuery\u306b\u30ed\u30fc\u30c9\njob: LoadJob = client.load_table_from_dataframe(df, table, job_config=LoadJobConfig(schema=table.schema))\njob.result() # API\u30ea\u30af\u30a8\u30b9\u30c8<\/code><\/pre>\nclient. load_table_from_json\u3092\u4f7f\u3046\uff08STRUCT\u3092\u542b\u3080\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\u3059\u308b\u5834\u5408\u306f\u3053\u308c\u4e00\u629e\uff09<\/h3>\n
\u4e0a\u8a18\u306eclient.load_table_from_dataframe<\/code>\u3092\u4f7f\u3063\u3066STRUCT\u3084\u914d\u5217\u3092\u542b\u3080\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\u3057\u3088\u3046\u3068\u3057\u305f\u3068\u3053\u308d\u3001\u4ee5\u4e0b\u306e\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u3066\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\nValueError: Uploading dataframes with struct (record) column types is not supported. See: https:\/\/github.com\/googleapis\/google-cloud-python\/issues\/8191<\/code><\/pre>\n\u3053\u3061\u3089<\/a>\u3092\u898b\u308b\u3068\u3001<\/p>\n\ngoogle-cloud-bigquery
\nDataFrame \u3092 Parquet \u5f62\u5f0f\u306b\u5909\u63db\u3057\u3066 API \u306b\u9001\u4fe1\u3057\u307e\u3059\u3002\u30cd\u30b9\u30c8\u3057\u305f\u5024\u3084\u914d\u5217\u5024\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u307e\u3059\u3002DataFrame \u3092\u30c6\u30fc\u30d6\u30eb\u306b\u8aad\u307f\u8fbc\u3080\u306b\u306f\u3001pyarrow\uff08DataFrame \u306e\u30c7\u30fc\u30bf\u3092 BigQuery API \u3078\u9001\u4fe1\u3059\u308b\u306e\u306b\u4f7f\u3046 parquet \u30a8\u30f3\u30b8\u30f3\uff09\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<\/blockquote>\n
\u3068\u66f8\u3044\u3066\u3042\u308b\u306e\u306b\u3072\u3069\u3044\u3067\u3059\u3002\u306a\u306e\u3067\u305d\u306e\u3088\u3046\u306a\u30b1\u30fc\u30b9\u3067\u306f\u3001load_table_from_json<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\nfrom typing import List, Dict, Any\nfrom google.cloud.bigquery import Client, LoadJobConfig\nfrom google.cloud.bigquery.job import QueryJob, LoadJob\nfrom google.cloud.bigquery.table import Table\n\n# \u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\nquery: str = """\n CREATE OR REPLACE TABLE dataset_name.hoge (\n id STRING NOT NULL\n , vector ARRAY<STRUCT<\n id INT64 NOT NULL,\n value FLOAT64 NOT NULL\n >>\n , keywords ARRAY<STRING>\n )\n"""\nclient: Client = Client()\njob: QueryJob = client.query(query)\njob.result()\n\n# \u5bfe\u8c61\u30c6\u30fc\u30d6\u30eb\u3092\u53d6\u5f97\ntable: Table = client.get_table("dataset_name.hoge") # API\u3067\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9\u3092\u53d6\u5f97\n\n# \u6295\u5165\u30c7\u30fc\u30bf\u3092\u8a2d\u5b9a\njson_rows: List[Dict[str, Any]] = [\n {\n 'id': 1,\n 'vector': [{'id': 1, 'value': 0.13458}, {'id': 2, 'value': 0.981345}, {'id': 3, 'value': 0.013473}],\n 'keywords': ['this', 'pen']\n },\n {\n 'id': 2,\n 'vector': [{'id': 1, 'value': 0.234956}, {'id': 2, 'value': 0.62164}, {'id': 3, 'value': 0.856725}],\n 'keywords': ['that', 'window']\n }\n]\n\n# \u30c7\u30fc\u30bf\u30ed\u30fc\u30c9\njob: LoadJob = client.load_table_from_json(\n json_rows, table, job_config=LoadJobConfig(schema=table.schema))\njob.result()<\/code><\/pre>\n\u30e6\u30fc\u30b6\u5b9a\u7fa9\u95a2\u6570\uff08UDF\uff09<\/h2>\n
\u5f8c\u3067\u66f8\u304f\u304b\u3082<\/p>\n","protected":false},"excerpt":{"rendered":"
BigQuery\u306epython client\uff08google-cloud-bigquery\uff09\u306e\u4f7f\u3044\u65b9\u3092\u30e1\u30e2\u3063\u3066\u3044\u304d\u307e\u3059\u3002 \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb \u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u30ad\u30fc\u30d5\u30a1\u30a4\u30eb\u3092\u74b0\u5883\u5909\u6570\u306b\u8a2d\u5b9a \u3053\u3061\u3089\u3092\u53c2\u8003\u306b\u3001\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\uff0b\u30ad\u30fc\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3001\u305d\u306e\u30ad\u30fc\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3092\u74b0\u5883\u5909\u6570\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002 export GOOGLE_APPLICATION_CREDENTIALS="\/hoge\/fuga\/key_file.json" \u30e9\u30a4\u30d6\u30e9\u30ea\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb pip install google-cloud-bigquery \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u751f\u6210 from google.cloud.bigquery import Client client: Client = Client() # \u74b0\u5883\u5909\u6570 GOOGLE_APPLICATION_CREDENTIALS\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u524d\u63d0\u3002\u5f15\u6570\u3067\u6e21\u3059\u3053\u3068\u3082\u3067\u304d\u308b <\/span>Continue Reading<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[32,14],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/302"}],"collection":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/comments?post=302"}],"version-history":[{"count":1,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/302\/revisions"}],"predecessor-version":[{"id":303,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/302\/revisions\/303"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}