2

我需要使用 python 客户端在 Tarantool 1.6 中调用 auto_increment 函数。

我试过没有成功:

database = tarantool.connect("localhost", 3301)
s = database.space("customer")
s.call('auto_increment','foo')

有人可以澄清如何在 python 中使用 auto_increment 插入一条带有 'foo' 作为字段的新记录吗?

我包含错误消息,我尝试了几种在 Python 中使用 auto_increment 的方法,但均未成功。

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nameko/containers.py", line 388, in _run_worker
    result = method(*worker_ctx.args, **worker_ctx.kwargs)
  File "./service.py", line 25, in create
    self.server.call('box.auto_increment', (0, 'foo'))
  File "/usr/local/lib/python2.7/dist-packages/tarantool/connection.py", line 373, in call
    response = self._send_request(request)
  File "/usr/local/lib/python2.7/dist-packages/tarantool/connection.py", line 341, in _send_request
    return self._send_request_wo_reconnect(request)
  File "/usr/local/lib/python2.7/dist-packages/tarantool/connection.py", line 261, in _send_request_wo_reconnect
    response = Response(self, self._read_response())
  File "/usr/local/lib/python2.7/dist-packages/tarantool/response.py", line 87, in __init__
    raise DatabaseError(self._return_code, self._return_message)
DatabaseError: (48, 'Unknown request type 10')
4

2 回答 2

2

首先,您应该使用tarantool 1.7+而不是1.6. 根据您的设置,您应该使用操作系统的包管理器安装更新版本的 Tarantool,或者使用相应的 docker 映像,即:

$ docker run --rm -p 3301:3301 -t -i tarantool/tarantool:1.7

在 tarantool 控制台中运行以下代码:

box.cfg{ listen=3301 }
customer = box.schema.space.create('customer', { 
    if_not_exists=true, 
    temporary=true 
})
customer:create_index('primary', {parts = {1, 'unsigned' }})

现在,运行 python 并执行以下命令:

$ python
>> import tarantool
>> server = tarantool.connect("localhost", 3301)
>> space = server.space("customer")
>> space.call("box.space.customer:auto_increment", [['alpha']])
- [1, 'alpha']
>> space.call("box.space.customer:auto_increment", [['bravo']])
- [2, 'bravo']

请注意参数中的二维数组space.call()

由于不推荐使用 1.7 版auto_increment(),因此拥有自动递增索引的正确方法是使用sequences.

重新启动 tarantool 并在 tarantool 控制台中执行以下 lua 代码:

box.cfg{ listen=3301 }

customer = box.schema.space.create('customer', {
    if_not_exists=true,
    temporary=true
})

box.schema.sequence.create('S', { min=1 })

customer:create_index('primary', {
    parts = {1, 'unsigned' },
    sequence = 'S'
})

现在,运行 python 并执行以下命令:

$ python
>> import tarantool
>> server = tarantool.connect("localhost", 3301)
>> space = server.space("customer")
>> space.insert((None, "alpha"))
- [1, 'alpha']
>> space.insert((None, "bravo"))
- [2, 'bravo']

您可以在此处阅读有关序列的更多信息。

于 2017-12-18T08:40:35.370 回答
0

您只能对主键进行 auto_increment。无法自动增加元组中的其他字段。

见这里:https ://tarantool.org/en/doc/1.6/book/box/box_space.html#box-space-auto-increment

于 2017-12-11T13:12:28.737 回答