0
votes
            How do I get the datasetId and tableId from BigQuery. I tried to click the dropdown on the sidebar and copied the dataset info and table info,  Is there any way, I can query the datasetId and tableId?  but I got this error. I am using php client libraries to pull the BigQuery data.

How do I get the datasetId and tableId from BigQuery. I tried to click the dropdown on the sidebar and copied the dataset info and table info, Is there any way, I can query the datasetId and tableId? but I got this error. I am using php client libraries to pull the BigQuery data.

use Google\Cloud\BigQuery\BigQueryClient;

/** Uncomment and populate these variables in your code */
// $projectId = 'The Google project ID';
// $datasetId = 'The BigQuery dataset ID';
// $tableId   = 'The BigQuery table ID';
// $maxResults = 10;

$maxResults = 10;
$startIndex = 0;

$options = [
    'maxResults' => $maxResults,
    'startIndex' => $startIndex
];
$bigQuery = new BigQueryClient([
    'projectId' => $projectId,
]);
$dataset = $bigQuery->dataset($datasetId);
$table = $dataset->table($tableId);
$numRows = 0;
foreach ($table->rows($options) as $row) {
    print('---');
    foreach ($row as $column => $value) {
        printf('%s: %s' . PHP_EOL, $column, $value);
    }
    $numRows++;
}

I am getting this error.

 Google\Cloud\Core\Exception\BadRequestException  : {
  "error": {
    "code": 400,
    "message": "Invalid dataset ID \"mc-data-2:Turnflex\". Dataset IDs must be alphanumeric (plus underscores and dashes) and must be at most 1024 characters long.",
    "errors": [
      {
        "message": "Invalid dataset ID \"mc-data-2:Turnflex\". Dataset IDs must be alphanumeric (plus underscores and dashes) and must be at most 1024 characters long.",
        "domain": "global",
        "reason": "invalid"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}
1
It seems like mc-data-2 is your projectId and Turnflex is your datasetId but in your code, datasetId is being picked up as mc-data-2:Turnflex. I am not aware of the php coding but you need to modify something so that it picks the correct datasetId. - Priya Agarwal

1 Answers

1
votes

According to PHP Google Cloud client library documentation for a proper BigQueryClient() class initialization you might be requiring propagate $projectId variable. In case of any Biqguery data querying or discovering action aimed, you would probably leverage query() method submitting Bigquery Job with the particular custom query or explicitly define dataset() and table() as the target Bigquery location.

Confirming @Priya Agarwal presumption, I've checked the code snippet from your example and it works as intended. I've catched the similar error that you've reported, once I made connection with $datasetId variable exposed jointly with tableId:

$datasetId = 'datasetId:tableId'

instead of:

$datasetId = 'datasetId'