admin管理员组文章数量:1025761
After I obtain my authenticate key with gcloud CLI, I try to execute the below Python code to create a table in my instance of Spanner. However, Spanner returned a message "Transaction is not begun."
from google.cloud import spanner
from google.api_core.exceptions import GoogleAPICallError
def _create_table(transaction):
"""
a transaction for creating a table
"""
query = (
'CREATE TABLE test_table (id INT64, data STRING(MAX)) PRIMARY KEY (id)'
)
transaction.execute_sql(query)
# access to Spanner
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
# execute a query with run_in_transaction
try:
database.run_in_transaction(_create_table)
except GoogleAPICallError as ex:
print(ex)
except ValueError as ex:
print(ex)
I expect that a transaction will begin when the method Database.run_in_transaction(transaction)
is invoked.
Please tell me how to fix the above code.
After I obtain my authenticate key with gcloud CLI, I try to execute the below Python code to create a table in my instance of Spanner. However, Spanner returned a message "Transaction is not begun."
from google.cloud import spanner
from google.api_core.exceptions import GoogleAPICallError
def _create_table(transaction):
"""
a transaction for creating a table
"""
query = (
'CREATE TABLE test_table (id INT64, data STRING(MAX)) PRIMARY KEY (id)'
)
transaction.execute_sql(query)
# access to Spanner
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
# execute a query with run_in_transaction
try:
database.run_in_transaction(_create_table)
except GoogleAPICallError as ex:
print(ex)
except ValueError as ex:
print(ex)
I expect that a transaction will begin when the method Database.run_in_transaction(transaction)
is invoked.
Please tell me how to fix the above code.
Share Improve this question asked Nov 17, 2024 at 4:46 Kazuma KusuKazuma Kusu 52 bronze badges1 Answer
Reset to default 0Spanner does not support executing DDL statements in a transaction. Instead, you should use the spanner_client.database_admin_api. update_database_ddl
function to execute DDL statements.
A full example for executing a DDL statement with the Python client can be found here: https://cloud.google/spanner/docs/getting-started/python#add-column-client-library-Python
After I obtain my authenticate key with gcloud CLI, I try to execute the below Python code to create a table in my instance of Spanner. However, Spanner returned a message "Transaction is not begun."
from google.cloud import spanner
from google.api_core.exceptions import GoogleAPICallError
def _create_table(transaction):
"""
a transaction for creating a table
"""
query = (
'CREATE TABLE test_table (id INT64, data STRING(MAX)) PRIMARY KEY (id)'
)
transaction.execute_sql(query)
# access to Spanner
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
# execute a query with run_in_transaction
try:
database.run_in_transaction(_create_table)
except GoogleAPICallError as ex:
print(ex)
except ValueError as ex:
print(ex)
I expect that a transaction will begin when the method Database.run_in_transaction(transaction)
is invoked.
Please tell me how to fix the above code.
After I obtain my authenticate key with gcloud CLI, I try to execute the below Python code to create a table in my instance of Spanner. However, Spanner returned a message "Transaction is not begun."
from google.cloud import spanner
from google.api_core.exceptions import GoogleAPICallError
def _create_table(transaction):
"""
a transaction for creating a table
"""
query = (
'CREATE TABLE test_table (id INT64, data STRING(MAX)) PRIMARY KEY (id)'
)
transaction.execute_sql(query)
# access to Spanner
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
# execute a query with run_in_transaction
try:
database.run_in_transaction(_create_table)
except GoogleAPICallError as ex:
print(ex)
except ValueError as ex:
print(ex)
I expect that a transaction will begin when the method Database.run_in_transaction(transaction)
is invoked.
Please tell me how to fix the above code.
Share Improve this question asked Nov 17, 2024 at 4:46 Kazuma KusuKazuma Kusu 52 bronze badges1 Answer
Reset to default 0Spanner does not support executing DDL statements in a transaction. Instead, you should use the spanner_client.database_admin_api. update_database_ddl
function to execute DDL statements.
A full example for executing a DDL statement with the Python client can be found here: https://cloud.google/spanner/docs/getting-started/python#add-column-client-library-Python
本文标签:
版权声明:本文标题:gcloud - The method `run_in_transaction` of Spanner client library for Python returned a message "Transaction is not be 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745638410a2160592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论