【问题标题】:Bigquery PHP create table schema or create table from query resultBigquery PHP 创建表模式或从查询结果创建表
【发布时间】:2014-11-11 17:25:51
【问题描述】:

我想从查询结果创建 BigQuery 表或在 PHP 中使用架构创建表。 我现在正在处理这些句子,但它们正在制作一个空的 noschema 表:

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

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

我可能找到了 python 解决方案,但任何人都可以将它翻译成 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",
  }
}

【问题讨论】:

    标签: php google-bigquery schema google-api-php-client


    【解决方案1】:

    非常感谢,之前的回答解决了我的问题。

     $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 表。 再见!

    【讨论】:

      【解决方案2】:
      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')
          );
      }
      

      【讨论】:

        猜你喜欢
        • 2019-03-18
        • 2013-01-15
        • 1970-01-01
        • 1970-01-01
        • 2018-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多