简体   繁体   中英

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

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM