{"id":358,"date":"2019-09-10T22:13:25","date_gmt":"2019-09-10T13:13:25","guid":{"rendered":"http:\/\/localhost:8000\/?p=358"},"modified":"2021-01-17T22:16:28","modified_gmt":"2021-01-17T13:16:28","slug":"gcp-cli","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2019\/09\/gcp-cli.html","title":{"rendered":"GCP CLI\u306eTips"},"content":{"rendered":"

\u305f\u307e\u306b\u4f7f\u3046\u3068\u5fd8\u308c\u3066\u308b\u3053\u3068\u304c\u591a\u3044\u306e\u3067\u3001\u3088\u304f\u4f7f\u3046\u30b3\u30de\u30f3\u30c9\u3092\u30e1\u30e2\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n

gcloud<\/h2>\n

\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u8a2d\u5b9a<\/h3>\n
# \u30ea\u30b9\u30c8\u30a2\u30c3\u30d7\ngcloud auth list\n\n# \u30ad\u30fc\u30d5\u30a1\u30a4\u30eb\u3067\u6709\u52b9\u5316\ngcloud auth activate-service-account --key-file {key_file_json_path}\n\n# \u6709\u52b9\u5316\u3068\u30c7\u30d5\u30a9\u30eb\u30c8\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u8a2d\u5b9a\ngcloud auth activate-service-account --key-file {key_file_json_path} --project {project_name}\n\n# \u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u3092\u7121\u52b9\u5316\ngcloud auth activate-service-account {service_account_name}<\/code><\/pre>\n

\u30c7\u30d5\u30a9\u30eb\u30c8\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u8a2d\u5b9a<\/h3>\n
# \u8a2d\u5b9a\u78ba\u8a8d\ngcloud config list\n\n# \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u8a2d\u5b9a\ngcloud config set project hoge_project<\/code><\/pre>\n

bq<\/h2>\n

\u6a19\u6e96SQL\u5229\u7528<\/h3>\n
# \u5b9f\u884c\u6642\u306b\u6bce\u56de\u6307\u5b9a\nbq query --use_legacy_sql=false\n\n# \u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3067\u6307\u5b9a\nvi ~\/.bigqueryrc \n+ [query]\n+ --use_legacy_sql=false\n+ [mk]\n+ --use_legacy_sql=false<\/code><\/pre>\n

\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8<\/h3>\n
# \u4e00\u89a7\u8868\u793a\nbq ls\n\n# \u4f5c\u6210\nbq mk {dataset_name}\n\n# \u524a\u9664\nbq rm {dataset_name}\nbq rm --recursive {dataset_name} # \u30c6\u30fc\u30d6\u30eb\u304c\u5b58\u5728\u3057\u3066\u3082\u524a\u9664\u3059\u308b\n\n# \u5b9a\u7fa9\u78ba\u8a8d\nbq show {dataset_name}<\/code><\/pre>\n

\u30c6\u30fc\u30d6\u30eb<\/h3>\n
# \u4e00\u89a7\u8868\u793a\nbq ls {dataset_name}\n\n# \u4f5c\u6210\uff08bq mk\u30b3\u30de\u30f3\u30c9\u3092\u5229\u7528\uff09\nbq mk --table {dataset_name}.{table_name} {schema_definition}\nbq mk --table test_dataset.test_table "id:int64, text:string"\n  # \u305f\u3060\u3057\u3053\u306e\u65b9\u6cd5\u3060\u3068ARRAY<INT64>\u3084STRUCT\u306a\u3069\u306f\u6307\u5b9a\u3057\u3066\u3082\u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3046\n  # BigQuery error in mk operation: Invalid value for type: ARRAY<INT64> is not a valid value\n\n# \u4f5c\u6210\uff08SQL\u3092\u5229\u7528\uff09\nbq query "{create table\u6587}"\nbq query "create table test_dataset.test_table (id int64, text string, int_array array<int64>, string_array array<string>)"\n  # \u3053\u308c\u3060\u3068ARRAY<INT64>\u3068\u304b\u3082\u3044\u3051\u308b\n\n# \u524a\u9664\nbq rm {dataset_name}.{table_name}\n\u4f8b) bq rm test_dataset.test_table\n\n#\u5b9a\u7fa9\u78ba\u8a8d\nbq show dataset_name.table_name\n<\/code><\/pre>\n

\u30c7\u30fc\u30bf<\/h3>\n
# \u30c7\u30fc\u30bf\u78ba\u8a8d\n#   -c(\u4efb\u610f): \u4ef6\u6570\u3092\u6307\u5b9a\u3067\u304d\u308b\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u306f100\nbq head -c 50 test_dataset.test_table\n\n# \u30c7\u30fc\u30bf\u767b\u9332\uff08SQL\u3092\u5229\u7528\uff09\nbq query "{insert\u6587}"\nbq query "insert into test_dataset.test_table values \\\n  (1, 'xxx1', [11,21,31], ['aa1','bb1','cc1']), \\\n  (2, 'xxx2', [12,22,32], ['aa2','bb2','cc2'])"\nbq query "insert into test_dataset.test_table values select id, text, int_array, string_array from hoge_table"\n  # select\u7d50\u679c\u3092insert\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u308b\n\n# \u30c7\u30fc\u30bf\u524a\u9664\uff08SQL\u3092\u5229\u7528\uff09\nbq query "{delete\u6587}"\nbq query "delete from test_dataset.test_table where id in (1, 2)"\n<\/code><\/pre>\n

View<\/h3>\n
# VIEW\u4f5c\u6210\n# select\u6587\u306e\u4e2d\u3067\u306f\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u540d\u304b\u3089\u6307\u5b9a\u5fc5\u8981\nbq query "create view test_dataset.test_view as select * from  hoge_project.test_dataset.test_table"\n\n# VIEW\u5b9a\u7fa9\u78ba\u8a8d\nbq show --view test_dataset.test_view\n>                               Query                               \n> ----------------------------------------------------------------- \n>  select * from  hoge_project.test_dataset.test_table  \n\n# VIEW\u524a\u9664\nbq query "drop view test_dataset.test_view"\n<\/code><\/pre>\n

Function<\/h3>\n
# function\u4f5c\u6210\n## SQL\u95a2\u6570\u3092\u5229\u7528\u3059\u308b\u30b1\u30fc\u30b9\nbq query "\ncreate or replace function test_dataset.trim_upper(str string)\nreturns string\nas (\n    trim(upper(str))\n)\n"\n\n## JavaScript\u3092\u5229\u7528\u3059\u308b\u30b1\u30fc\u30b9\n## \u30a8\u30af\u30b9\u30af\u30e9\u30e1\u30fc\u30b7\u30e7\u30f3\u30de\u30fc\u30af\u304cBash\u3060\u3068\u30d2\u30b9\u30c8\u30ea\u5c55\u958b\u306b\u306a\u308b\u306e\u3067\u3001\u30d2\u30a2\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u5229\u7528\nbq query "\ncreate or replace function test_dataset.trim_upper(str string)\nreturns string\nlanguage js as '''\n    if (str === null) return null;\n    return str.toUpperCase().trim();\n'''\n"\n\n# function\u5b9a\u7fa9\u78ba\u8a8d\nbq show --routine test_dataset.trim_upper\nRoutine hoge_project:test_dataset.trim_upper\n\n      Id        Routine Type      Language          Signature                       Definition                 Creation Time    Last Modified Time  \n ------------ ----------------- ------------ ------------------------ -------------------------------------- ----------------- -------------------- \n  trim_upper   SCALAR_FUNCTION   JAVASCRIPT   (str STRING) -> STRING                                          05 Dec 19:27:35   05 Dec 19:27:35     \n                                                                           if (str === null) return null;                                                  \n                                                                           return str.toUpperCase().trim();     \n\n# function\u524a\u9664\nbq query "drop function test_dataset.trim_upper"\n<\/code><\/pre>\n

gsutil<\/h2>\n

\u4f7f\u3044\u65b9\u306e\u78ba\u8a8d<\/h3>\n
# \u30b5\u30d6\u30b3\u30de\u30f3\u30c9\u306e\u4e00\u89a7\ngsutil\nUsage: gsutil [-D] [-DD] [-h header]... [-m] [-o] [-q] [command [opts...] args...]\n> Available commands:\n>   acl              Get, set, or change bucket and\/or object ACLs\n>   bucketpolicyonly Configure Bucket Policy Only (Beta)\n>   cat              Concatenate object content to stdout\n>   compose          Concatenate a sequence of objects into a new composite object.\n>   config           Obtain credentials and create configuration file\n\n# \u30b5\u30d6\u30b3\u30de\u30f3\u30c9\u306e\u30d8\u30eb\u30d7\ngsutil help {subcommand}\ngsutil help cat\n> NAME\n>   cat - Concatenate object content to stdout\n> SYNOPSIS\n>   gsutil cat [-h] url...<\/code><\/pre>\n

\u30d5\u30a1\u30a4\u30eb\u64cd\u4f5c<\/h3>\n
# \u30d5\u30a1\u30a4\u30eb\u306e\u30ea\u30b9\u30c8\u30a2\u30c3\u30d7\ngsutil ls gs:\/\/{folder_path}\/\ngsutil ls gs:\/\/{folder_path}\/{file_name}\n\n# \u30d5\u30a1\u30a4\u30eb\u306e\u30b3\u30d4\u30fc\ngsutil cp {file_path} gs:\/\/{folder_path}\/\ngsutil cp gs:\/\/{folder_path}\/{file_name} {folder_path} \n\n# \u30d5\u30a9\u30eb\u30c0\u306e\u30b3\u30d4\u30fc\ngsutil cp -rf {folder_path} gs:\/\/{folder_path}\/\ngsutil cp -rf gs:\/\/{folder_path} {folder_path} \n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

\u305f\u307e\u306b\u4f7f\u3046\u3068\u5fd8\u308c\u3066\u308b\u3053\u3068\u304c\u591a\u3044\u306e\u3067\u3001\u3088\u304f\u4f7f\u3046\u30b3\u30de\u30f3\u30c9\u3092\u30e1\u30e2\u3063\u3066\u3044\u304d\u307e\u3059\u3002 gcloud \u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u8a2d\u5b9a # \u30ea\u30b9\u30c8\u30a2\u30c3\u30d7 gcloud auth list # \u30ad\u30fc\u30d5\u30a1\u30a4\u30eb\u3067\u6709\u52b9\u5316 gcloud auth activate-service-account –key-file {key_file_json_path} # \u6709\u52b9\u5316\u3068\u30c7\u30d5\u30a9\u30eb\u30c8\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u8a2d\u5b9a gcloud auth activate-service-account –key-file {key_file_json_path} –project {project_name} # \u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u3092\u7121\u52b9\u5316 gcloud auth activate-service-account {service_account_name} \u30c7\u30d5\u30a9\u30eb\u30c8\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u8a2d\u5b9a # \u8a2d\u5b9a\u78ba\u8a8d gcloud config list # \u30d7\u30ed\u30b8\u30a7 <\/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":[14],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/358"}],"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=358"}],"version-history":[{"count":0,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/358\/revisions"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=358"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}