我正在尝试运行一个脚本文件,其中包含创建临时表,加载数据,创建测试表,合并数据以及删除BigQuery中的表。 Intial 3命令正常工作,但MERGE命令无效。
bq mk --table --expiration 3600 --description "This is my source table" dataset_name.table_name
emp_id:INTEGER,emp_name:STRING
bq load --source_format=CSV income_tracking.temp_table /home/username/physical.csv
bq mk --table --expiration 3600 --description "This is my target table" dataset_name.table_name
emp_id:INTEGER,emp_name:STRING
MERGE dataset.test_data T
USING dataset.temp_table S
ON T.emp_id = S.emp_id
WHEN MATCHED THEN
UPDATE SET emp_name = S.emp_name
WHEN NOT MATCHED THEN
INSERT (emp_id, emp_name) VALUES(emp_id, emp_name)
bq rm -f -t project_id:dataset_name.temp_table
此处MERGE命令不起作用,错误为-
username@cloudshell:~ (bmg-dt-prd-svc)$ bash bq_merge.sh
Table 'project_id:dataset_name.table_name' successfully created.
Upload complete.
Waiting on bqjob_r75d2358a0232abdb_0000016f227e7a0f_1 ... (1s) Current status: DONE
Table 'project_id:dataset_name.table_name' successfully created.
bq_merge.sh: line 4: MERGE: command not found
bq_merge.sh: line 5: USING: command not found
bq_merge.sh: line 6: ON: command not found
bq_merge.sh: line 7: WHEN: command not found
bq_merge.sh: line 8: UPDATE: command not found
bq_merge.sh: line 9: WHEN: command not found
bq_merge.sh: line 10: syntax error near unexpected token `emp_id,'
'q_merge.sh: line 10: ` INSERT (emp_id, emp_name) VALUES(emp_id, emp_name)
有什么解决方案,谢谢
答案 0 :(得分:3)
这里的问题是您试图将查询作为bash命令运行。 您应该使用以下命令替换bash脚本中的查询:
bq query --use_legacy_sql=false \
"""
MERGE dataset.test_data T
USING dataset.temp_table S
ON T.emp_id = S.emp_id
WHEN MATCHED THEN
UPDATE SET emp_name = S.emp_name
WHEN NOT MATCHED THEN
INSERT (emp_id, emp_name) VALUES(emp_id, emp_name)
"""