Last active
October 29, 2017 14:33
-
-
Save shigemk2/ae9bcd7fdf8643c190136c58c0688ba8 to your computer and use it in GitHub Desktop.
PHPでBigQueryのテーブルにデータをアップロードするサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"php": ">=5.4", | |
"google/cloud-bigquery": "^0.2" | |
}, | |
"require-dev": { | |
"phpunit/phpunit": "~4" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "service_account", | |
"project_id": "project_id", | |
"private_key_id": "nxxxxxxxxxxxxxxxxxxxxxxxxxxx", | |
"private_key": "-----BEGIN PRIVATE KEY-----\nxxxxxxxxxxxxxxxxxxxxxxxxxxx\n-----END PRIVATE KEY-----\n", | |
"client_email": "nxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com", | |
"client_id": "nxxxxxxxxxxxxxxxxxxxxxxxxxxx", | |
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | |
"token_uri": "https://accounts.google.com/o/oauth2/token", | |
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | |
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/xxxxxxxxxxxxxxxxxxxxxxxxxxx-compute%40developer.gserviceaccount.com" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# https://github.com/GoogleCloudPlatform/php-docs-samples/blob/master/bigquery/quickstart/quickstart.php を再構築 | |
/** | |
* Copyright 2016 Google Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
# [START bigquery_quickstart] | |
# Includes the autoloader for libraries installed with composer | |
require __DIR__ . '/vendor/autoload.php'; | |
# Imports the Google Cloud client library | |
use Google\Cloud\BigQuery\BigQueryClient; | |
# Your Google Cloud Platform project ID | |
$projectId = 'project id'; | |
# Instantiates a client | |
$bigquery = new BigQueryClient([ | |
'keyFilePath' => 'コンソールの「認証情報」から作成したJSONファイルのフルパス' | |
]); | |
# The name for the new dataset | |
$datasetName = 'test_dataset'; | |
$options['jobConfig'] = array( | |
'sourceFormat' => 'CSV', // アップロードするファイルのフォーマット | |
'autodetect' => true, // スキーマの自動検出 | |
'skipLeadingRows' => 1 // 何行目までのデータをスキップするか | |
); | |
# dataset | |
$dataset = $bigquery->dataset($datasetName); // 事前にデータセットは作っておいて、データセットの情報を取得する | |
echo 'Dataset ' . $dataset->id() . ' created.'; | |
$table = $dataset->createTable('test_table'); // テーブル作成 | |
$job = $table->load(fopen('test.csv', 'r'), $options); | |
# [END bigquery_quickstart] | |
return $dataset; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id | name | |
---|---|---|
1 | bob | |
2 | alice | |
3 | clala |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment