2014-09-17 1 views
4

Я хочу создать таблицу BigQuery из результата запроса или создать таблицу со схемой на PHP. Я обработка прямо сейчас эти предложения, но они прилагают пустое noschema таблицы:Bigquery PHP создать схему таблицы или создать таблицу из результата запроса

$postBody = array( 'tableReference' => 
    array(
     'projectId' => $project_id, 
     'datasetId' => $dataset, 
     'tableId' => 'josetest' 
    ) 
); 

$table = $service->tables->insert($project_id, $dataset, new Google_Service_Bigquery_Table($postBody)); 

я нашел, вероятно, питон решением, но может кто-нибудь перевести его на PHP? Это:

"configuration": { 
    "query": { 
    "query": "select count(*) from foo.bar", 
    "destinationTable": { 
     "projectId": "my_project", 
     "datasetId": "my_dataset", 
     "tableId": "my_table" 
    }, 
    "createDisposition": "CREATE_IF_NEEDED", 
    "writeDisposition": "WRITE_APPEND", 
    } 
} 

ответ

4
  1. проще всего использовать Google APIs Client Library for PHP

  2. см this post как экземпляр объекта Google_Client и аутентификации

  3. раздел из нескольких классов из собственного кода.

.

/** 
* @param Google_Client $client 
* @param string $project_id 
* @param string $dataset_id 
* @throws Google_Service_Exception 
* @return Google_Service_Bigquery_Table 
*/ 
public function BQ_Tables_Insert($client, $project_id, $dataset_id) { 
    $bq = new Google_Service_Bigquery($client); 
    $table_reference = new Google_Service_Bigquery_TableReference(); 
    $table_reference->setProjectId($project_id); 
    $table_reference->setDatasetId($dataset_id); 
    $table_reference->setTableId(static::tableId()); 
    $schema = new Google_Service_Bigquery_TableSchema(); 
    $schema->setFields(static::fields()); 
    $table = new Google_Service_Bigquery_Table(); 
    $table->setTableReference($table_reference); 
    $table->setSchema($schema); 

    try { 
     return $bq->tables->insert($project_id, $dataset_id, $table); 
    } catch (Google_Service_Exception $e) { 
     $this->setErrors($e->getErrors())->setErrorMessage($e->getMessage()); 
     throw $e; 
    } 
} 

где static::tableId() это имя таблицы, и static::fields() является представление массива таблицы

/** 
* @see https://developers.google.com/bigquery/docs/reference/v2/tables/insert 
*/ 
public static function fields() { 
    return 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') 
    ); 
} 
2

Спасибо так много, предыдущий ответ решить проблему ми.

$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') 
    ); 


    //$bq = $service 
    $table_reference = new Google_Service_Bigquery_TableReference(); 
    $table_reference->setProjectId($project_id); 
    $table_reference->setDatasetId($dataset); 
    $table_reference->setTableId("testsales"); 
    $schema = new Google_Service_Bigquery_TableSchema(); 
    $schema->setFields($fields); 
    $table = new Google_Service_Bigquery_Table(); 
    $table->setTableReference($table_reference); 
    $table->setSchema($schema); 

    try { 
     return $service->tables->insert($project_id, $dataset, $table); 
    } catch (Google_Service_Exception $e) { 
     $this->setErrors($e->getErrors())->setErrorMessage($e->getMessage()); 
     throw $e; 
    } 

Это создало таблицу Bigquery со схемой. Пока!

Смежные вопросы