0

我想在 php 脚本中使用 INSERT 命令在数据库中插入数千条记录,因为它更容易访问,但是https://developers.google.com/bigquery/docs/query-reference,这不显示 INSERT在 php 中查询时可以使用的命令,例如 $quert->setQuery("Insert into ... values...") ,已在 BigQuery Web 控制台的查询表中尝试过此操作,但似乎没有工作,无论如何使用 setQuery() 和其他命令来插入数据?

4

2 回答 2

1

除了 Jordan 的回答之外,还有一段代码可以帮助您开始使用 Google BigQuery API 和Google API PHP 客户端库以编程方式将您自己的数据加载到 BigQuery 中。请注意,此代码段只是将加载作业的原始 API 响应吐出,包括屏幕上的作业 ID - 您必须添加自己的轮询逻辑来检查加载作业状态。

(我们将添加有关加载您自己的数据的其他文档,以及更多 PHP 示例)。

<?php

require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_BigqueryService.php";

session_start();

$client = new Google_Client();

// Visit https://code.google.com/apis/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
$client->setScopes(array('https://www.googleapis.com/auth/bigquery'));
$client->setClientId('XXXXXXXXX.apps.googleusercontent.com');
$client->setClientSecret('XXXXXXXXX');
$client->setRedirectUri('http://YOURAPPLICATION/index.php');

// Instantiate a new BigQuery Client 
$bigqueryService = new Google_BigqueryService($client);

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

?>
<!doctype html>
<html>
<head>
  <title>BigQuery API Sample</title>
</head>
<body>
<div id='container'>
  <div id='top'><h1>BigQuery API Sample</h1></div>
  <div id='main'>
<?php

    if (isset($_GET['logout'])) {
      unset($_SESSION['token']);
    }

    if (isset($_GET['code'])) {
      $client->authenticate($_GET['code']);
      $_SESSION['token'] = $client->getAccessToken();
      header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
    }

    if (isset($_SESSION['token'])) {
      $client->setAccessToken($_SESSION['token']);
    }

    if ($client->getAccessToken()) {

        // Your project number, from the developers.google.com/console project you created
        // when signing up for BigQuery
        $project_number = 'XXXXXXXXXXXXXX';

        // Information about the destination table
        $destination_table = new Google_TableReference();
        $destination_table->setProjectId($project_number);
        $destination_table->setDatasetId('php_test');
        $destination_table->setTableId('my_new_table');

        // Information about the schema for your new table
        $schema_fields = array();
        $schema_fields[0] = new Google_TableFieldSchema();
        $schema_fields[0]->setName('first');
        $schema_fields[0]->setType('string');

        $schema_fields[1] = new Google_TableFieldSchema();
        $schema_fields[1]->setName('last');
        $schema_fields[1]->setType('string');        

        $destination_table_schema = new Google_TableSchema();
        $destination_table_schema->setFields($schema_fields);

        // Set the load configuration, including source file(s) and schema
        $load_configuration = new Google_JobConfigurationLoad();
        $load_configuration->setSourceUris(array('gs://YOUR_GOOGLE_CLOUD_STORAGE_BUCKET/file.csv'));
        $load_configuration->setDestinationTable($destination_table);
        $load_configuration->setSchema($destination_table_schema);


        $job_configuration = new Google_JobConfiguration();
        $job_configuration->setLoad($load_configuration);

        $load_job = new Google_Job();
        $load_job->setKind('load');        
        $load_job->setConfiguration($job_configuration);

        $jobs = $bigqueryService->jobs;
        $response = $jobs->insert($project_number, $load_job);

        echo '<pre>';
        print_r($response);
        echo '</pre>';

    $_SESSION['token'] = $client->getAccessToken();
    } else {
      $authUrl = $client->createAuthUrl();
      print "<a class='login' href='$authUrl'>Authorize Access to the BigQuery API</a>";
    }


?>
  </div>
</div>
</body>
</html>
于 2012-11-20T00:04:37.640 回答
1

BigQuery 不支持 INSERT 命令。您需要创建一个加载作业。有关详细信息,请参阅https://developers.google.com/bigquery/docs/import#localimport

于 2012-11-19T21:13:57.537 回答