我正在嘗試使用php api在大查詢中創建一個表。我可以創建一個沒有架構的表,但是當我提供架構時,我會收到錯誤??雌饋砦沂褂昧隋e誤的語法,但是我嘗試了我能想到的任何格式,但找不到我想要實現的目標的任何示例。我使用字符串文本作為用于測試的字段參數。我的代碼看起來像這樣: $bigQuery = new BigQueryClient([ 'keyFilePath' => [keyfilepath], 'projectId' => [projectid], 'location' => [location] ]); /** @var Dataset $dataSet */ $dataSet = $bigQuery->dataset('my-data-set'); $fieldString = '{"name": "myfield","type": "STRING","mode": "REQUIRED"}' . "\n" . '{"name": "anotherfield", "type": "STRING", "mode": "REQUIRED"}'; $options = [ 'fields' => $fieldString ]; $dataSet->createTable('mytable', $options);這給出了錯誤:“字段選擇無效 {\”名稱“:”我的字段“”或者,當我像這樣格式化“$fieldString”時:$fieldString = '[{"name": "myfield","type": "STRING","mode": "REQUIRED"}, {"name": "anotherfield", "type": "STRING", "mode": "REQUIRED"}]';我收到錯誤:無效的字段掩碼“[{\”name\“:”myfield“,”類型“:”字符串“,”模式“:”必需“},{”名稱“:”另一個字段“,”類型“:”字符串“,”模式“:”必需“}}”。映射鍵應表示為 [\“some_key\”]。我還嘗試先創建表,然后像這樣更新它:$table = $dataSet->createTable('mytable');$table->update($options);但我得到同樣的錯誤。即使我使用與此處完全相同的 json 表示形式,問題仍然存在。我在這里做錯了什么?更新:實際上,在我切換到字段的字符串文本之前,我首先嘗試了以下方法: $fields = [ ['name'=> 'myfield', 'type' => 'INTEGER', 'mode' => 'REQUIRED'], ['name'=> 'anotherfield', 'type' => 'INTEGER', 'mode' => 'REQUIRED'] ]; $options = [ 'schema' => $fields ]; $dataSet->createTable('mytable', $options);這將產生錯誤:“收到的 JSON 有效負載無效。“表”中的未知名稱“架構”:Proto 字段不重復,無法啟動列表。然后我編輯了代碼,如下所示: $fields = [ ['name'=> 'myfield', 'type' => 'INTEGER', 'mode' => 'REQUIRED'], ['name'=> 'anotherfield', 'type' => 'INTEGER', 'mode' => 'REQUIRED'] ]; $options = [ 'fields' => $fields ]; $dataSet->createTable('mytable', $options);這給出了:警告:原始代碼()期望參數 1 是字符串,數組給定我之前在我的問題中沒有提到這一點,因為我認為這無關緊要。事后看來,這可能是,但我的問題仍然存在。
1 回答

眼眸繁星
TA貢獻1873條經驗 獲得超9個贊
沒有數組這樣的東西$fieldString$fields
喜歡這個:
$fields = [
[
'name' => 'field1',
'type' => 'string',
'mode' => 'required'
],
[
'name' => 'field2',
'type' => 'integer'
],
];
然后
$schema = ['fields' => $fields];
$table = $dataset->createTable($tableId, ['schema' => $schema]);
請參閱此處的示例。
- 1 回答
- 0 關注
- 138 瀏覽
添加回答
舉報
0/150
提交
取消