如何使用 Python 客户端库将更新插入数据批量插入到 Google Cloud Spanner?
How do I batch upsert data into Google Cloud Spanner using the Python client library?
我想将 pandas 数据帧的内容更新到 Google Cloud Spanner 数据库中的 table。文档 here 建议使用批处理对象的 insert_or_update()
方法。
如果批处理对象是由运行 this
创建的
from google.cloud import spanner_v1
client = spanner_v1.Client()
batch = client.batch()
那么这个对象没有可用的方法。 运行 dir(client)
给我这些结果
['SCOPE',
'_SET_PROJECT',
'__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_credentials',
'_database_admin_api',
'_determine_default',
'_http',
'_http_internal',
'_instance_admin_api',
'_item_to_instance',
'copy',
'credentials',
'database_admin_api',
'from_service_account_json',
'instance',
'instance_admin_api',
'list_instance_configs',
'list_instances',
'project',
'project_name',
'user_agent']
如何在 Spanner 中进行批量更新插入?
代码片段有一个批量插入的例子。我检查了在片段中创建的批处理对象也有一个 insert_or_update 字段。
['class', 'delattr', 'dict ', 'doc', 'enter', 'exit', ' format', 'getattribute', 'hash', 'init', 'module', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof ', 'str', 'subclasshook', 'weakref', '_check_state', '_mutations', '_session', 'commit', 'committed', 'delete', 'insert', 'insert_or_update', 'replace', 'update']
你能试试看吗?
如果您有一个 pandas 数据框,这里是一个随机的 5 x 3 列,包含 a、b、c 列,您可以将数据框转换为列名和行并批量插入。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 3)),
columns=['a', 'b', 'c'])
您可以通过从 df
中提取列和值并批量插入,将其插入 Google Cloud Spanner。
from google.cloud import spanner
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
columns = df.columns
values = df.values.tolist()
with database.batch() as batch:
batch.insert(
table='table',
columns=columns
values=values
)
我想将 pandas 数据帧的内容更新到 Google Cloud Spanner 数据库中的 table。文档 here 建议使用批处理对象的 insert_or_update()
方法。
如果批处理对象是由运行 this
创建的from google.cloud import spanner_v1
client = spanner_v1.Client()
batch = client.batch()
那么这个对象没有可用的方法。 运行 dir(client)
给我这些结果
['SCOPE',
'_SET_PROJECT',
'__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_credentials',
'_database_admin_api',
'_determine_default',
'_http',
'_http_internal',
'_instance_admin_api',
'_item_to_instance',
'copy',
'credentials',
'database_admin_api',
'from_service_account_json',
'instance',
'instance_admin_api',
'list_instance_configs',
'list_instances',
'project',
'project_name',
'user_agent']
如何在 Spanner 中进行批量更新插入?
代码片段有一个批量插入的例子。我检查了在片段中创建的批处理对象也有一个 insert_or_update 字段。
['class', 'delattr', 'dict ', 'doc', 'enter', 'exit', ' format', 'getattribute', 'hash', 'init', 'module', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof ', 'str', 'subclasshook', 'weakref', '_check_state', '_mutations', '_session', 'commit', 'committed', 'delete', 'insert', 'insert_or_update', 'replace', 'update']
你能试试看吗?
如果您有一个 pandas 数据框,这里是一个随机的 5 x 3 列,包含 a、b、c 列,您可以将数据框转换为列名和行并批量插入。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 3)),
columns=['a', 'b', 'c'])
您可以通过从 df
中提取列和值并批量插入,将其插入 Google Cloud Spanner。
from google.cloud import spanner
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
columns = df.columns
values = df.values.tolist()
with database.batch() as batch:
batch.insert(
table='table',
columns=columns
values=values
)