예제 #1
0
 private function createSchema($schema_array)
 {
     $fields = array();
     foreach ($schema_array as $field) {
         $table_field_schema = new Google_Service_Bigquery_TableFieldSchema();
         $table_field_schema->setName($field["name"]);
         $table_field_schema->setType($field["type"]);
         $fields[] = $table_field_schema;
     }
     // Create a tableschema
     $schema = new Google_Service_Bigquery_TableSchema();
     $schema->setFields($fields);
     return $schema;
 }
define("TABLE_ID", "DESIRED TABLE ID");
define("KEY_FILE", 'KEY FILE NAME');
define("SERVICE_ACCOUNT_NAME", 'SERVICE ACCOUNT NAME');
define("CLIENT_ID", 'CLIENT ID');
$key = file_get_contents(KEY_FILE);
$client = new Google_Client();
$client->setAssertionCredentials(new Google_Auth_AssertionCredentials(SERVICE_ACCOUNT_NAME, array('https://www.googleapis.com/auth/bigquery'), $key));
$client->setClientId(CLIENT_ID);
// Instantiate a new BigQuery Client
$service = new Google_Service_Bigquery($client);
$fields = array(array('name' => 'user_id', 'type' => 'integer', 'mode' => 'required'), array('name' => 'order_id', 'type' => 'integer', 'mode' => 'required'), array('name' => 'status', 'type' => 'integer', 'mode' => 'nullable'), array('name' => 'timestamp', 'type' => 'timestamp', 'mode' => 'nullable'));
$table_reference = new Google_Service_Bigquery_TableReference();
$table_reference->setProjectId(PROJECT_ID);
$table_reference->setDatasetId(DATASET_ID);
$table_reference->setTableId(TABLE_ID);
$schema = new Google_Service_Bigquery_TableSchema();
$schema->setFields($fields);
$table = new Google_Service_Bigquery_Table();
$table->setTableReference($table_reference);
$table->setSchema($schema);
try {
    $result = $service->tables->insert(PROJECT_ID, DATASET_ID, $table);
    print_r($result);
} catch (Google_Service_Exception $e) {
    echo $e->getMessage();
}
?>
</pre>
<p><span style="color: #339966;"><em><strong>Understanding the code</strong></em></span></p>
<p><strong>define("KEY_FILE", 'KEY FILE NAME');</strong></p>
<p>You will have to put your downloaded key file in the directory same as your php file. And provide a name to your key file here. </p>
예제 #3
0
 /**
  * Create a new table
  *
  * @param string $tableName
  * @param string $description
  * @param array  $fields Array of fields where each field is in this format:
  *                       ['name' => 'fieldName', 'type' => 'integer', 'mode' => 'required']
  *
  * @throws \Exception
  */
 public function createTable($tableName, $description, $fields)
 {
     $tableReference = $this->getTableReference($tableName);
     $this->_debug("Creating table " . $this->getDataSet() . '.' . $tableName);
     $service = $this->getService();
     $tableSchema = new \Google_Service_Bigquery_TableSchema();
     $tableSchema->setFields($fields);
     $bqTable = new \Google_Service_Bigquery_Table();
     $bqTable->setDescription($description);
     $bqTable->setTableReference($tableReference);
     $bqTable->setSchema($tableSchema);
     $options = [];
     try {
         $service->tables->insert($this->bigQueryProject(), $this->getDataSet(), $bqTable, $options);
     } catch (\Exception $e) {
         if (!stristr($e->getMessage(), 'Already Exists: Table')) {
             throw $e;
         }
     }
 }