0

我想在PHP腳本中使用INSERT命令,在數據庫中插入大量的記錄,因爲它會更容易訪問,但https://developers.google.com/bigquery/docs/query-reference,這不表明可以同時在PHP查詢中使用一樣, $ quert-> setQuery INSERT命令(「INSERT INTO ... ...值」),在查詢表的BigQuery的Web控制檯嘗試過這一點,但它不似乎無法正常工作,有沒有辦法使用setQuery()和其他命令來插入數據?INSERT BigQuery中的查詢部分,使用谷歌的API的PHP客戶端

回答

1

的BigQuery不支持INSERT命令。您需要創建一個加載作業。有關更多信息,請參見https://developers.google.com/bigquery/docs/import#localimport

+0

約旦,這將使用java/python,我不想使用它們中的任何一個。我想要一些php命令,這將類似於INSERT。 – Aps18

+0

PHP客戶端支持加載作業。邁克爾的回答如下。 –

1

除了喬丹的回答,這裏有一段代碼,應該讓您開始使用Google BigQuery API和Google API PHP client library以編程方式將自己的數據加載到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> 
+0

如果我有一個已經存在的表,我只想更新或追加我的數據到它,所以,使用從「設置負載配置」將工作正常嗎?或者我應該再次爲其設置架構? – Aps18

+1

如果您追加到現有表格,則不需要再次設置模式。 –

+0

@邁克爾,感謝您的幫助,現在工作正常。 – Aps18

相關問題