BigQuery PHP API send json that isn't in Google Cloud Storage

余生长醉 提交于 2020-01-15 11:54:09

问题


I want to update an existing table I have in BigQuery with a single new row.

I found an example how this could be done if I have that json in a file in Google Cloud Storage:

$confLoad->setSourceUris(array(
    "gs://json_file_bucket/some_name.json"
));
$conf = new \Google_JobConfiguration;
$conf->setLoad($confLoad);
$job->setConfiguration($conf);
$service = new \Google_BigQueryService($this->client);
$running = $service->jobs->insert($this->projectId, $job);

But I don't understand how this can be achieved without an external json file.


回答1:


If you're adding just a single row, your best option is to use the TableData.insertAll() method, which lets you import a single row or a few rows at a time. More information is here.

Here is the code in python (I realize you're using PHP, but it should be somewhat similar):

body = {"rows":[
    {"json": {"column_name":7.7,}}
    ]}
response = bigquery.tabledata().insertAll(
    projectId=PROJECT_ID,
    datasetId=DATASET_ID,
    tableId=TABLE_ID,
    body=body).execute()

The alternative is to use 'media upload'. This is as simple as adding a media_body parameter to the job configuration, where media_body contains the data you are loading (and you don't need to specify sourceUris. For example:

job = {
    'configuration': {
      'load': load_config # specify destination table here
    }
  }
result = jobs.insert(
  projectId=project_id,
  body=job,
  media_body=media_body).execute()

There is more information about the media upload option in PHP here.



来源:https://stackoverflow.com/questions/22589598/bigquery-php-api-send-json-that-isnt-in-google-cloud-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!