cloud spanner python INT64 类型的客户端删除失败

cloud spanner python client delete failed with INT64 type

我正在尝试使用 INT64 类型的键在 table 上传递删除请求,但我收到以下错误: table INT_KEYS 中列 key1 的值无效:应为 INT64

在请求删除操作时是否有特定的方法来键入键范围? 我可以使用相同的输入很好地插入数据,但删除请求失败。 我写了下面的简短代码来说明

keys = ['key1', 'key2']
ins_values = [[2, 5],
              [3, 6],
              [4, 7]]
del_values = [[2, 5],
              [4, 7]]

for table_name in ['STR_KEYS', 'INT_KEYS']:
    with database.batch() as batch:
        result = batch.insert_or_update(
            table_name, keys, ins_values
        )
    print 'completed insert in table %s' % table_name

    with database.batch() as batch:
        key_ranges = list()
        for value_list in del_values:
            key_range = spanner.KeyRange(start_closed=value_list, end_closed=value_list)
            key_ranges.append(key_range)
        key_set = spanner.KeySet(keys=[keys], ranges=key_ranges)
        result = batch.delete(
            table_name, key_set
        )
    print 'deleted data from table %s' % table_name

我得到下面的输出

completed insert in table STR_KEYS
deleted data from table STR_KEYS
completed insert in table INT_KEYS
Traceback (most recent call last):
File "/Users/thibault/PycharmProjects/spanner-local/testIntDelete.py", line 89, in <module>
table_name, key_set
File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/google/cloud/spanner_v1/database.py", line 409, in __exit__
self._batch.commit()
File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/google/cloud/spanner_v1/batch.py", line 156, in commit
options=options)
File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/google/cloud/spanner_v1/gapic/spanner_client.py", line 845, in commit
return self._commit(request, options)
File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/google/gax/api_callable.py", line 452, in inner
return api_caller(api_call, this_settings, request)
File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/google/gax/api_callable.py", line 438, in base_caller
return api_call(*args)
File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/google/gax/api_callable.py", line 376, in inner
return a_func(*args, **kwargs)
File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/google/gax/retry.py", line 127, in inner
' classified as transient', exception)
google.gax.errors.RetryError: RetryError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.FAILED_PRECONDITION, Invalid value for column key1 in table INT_KEYS: Expected INT64.)>)

使用删除API,您只需指定"keys"或"ranges"中的主键值,而不是值。我推断 INT_KEYS 的架构类似于:

创建 TABLE INT_KEYS ( key1 INT64 不为空, key2 INT64 不为空, ) 主键 (key1, key2);

在这种情况下,要删除插入的行,您需要这样的东西:

with database.batch() as batch:
    key_ranges = list()
    for value_list in del_values:
        key_range = spanner.KeyRange(start_closed=value_list, end_closed=value_list)
        key_ranges.append(key_range)
    key_set = spanner.KeySet(ranges=key_ranges)
    result = batch.delete(
        table_name, key_set
    )

请注意,您不需要(也不应该尝试)通过 "keys" 属性指定列名;用于指定要删除的特定行。

FWIW,您的代码适用于 STR_KEYS,因为 ("key1", "key2") 可以成功解释为不存在的行的主键 (key1=key1 ,key2=key2),Spanner在删除不存在的行时不报错