Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Last active October 29, 2017 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/ae9bcd7fdf8643c190136c58c0688ba8 to your computer and use it in GitHub Desktop.
Save shigemk2/ae9bcd7fdf8643c190136c58c0688ba8 to your computer and use it in GitHub Desktop.
PHPでBigQueryのテーブルにデータをアップロードするサンプル
{
"require": {
"php": ">=5.4",
"google/cloud-bigquery": "^0.2"
},
"require-dev": {
"phpunit/phpunit": "~4"
}
}
{
"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"
}
<?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;
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