Python中的Tarantool - 如何调用auto_increment函数

时间:2017-12-09 11:32:08

标签: python-2.7 tarantool

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

我尝试过没有成功:

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

有人可以澄清如何使用&f; foo'来插入新记录。作为在python中使用auto_increment的字段?

我包含错误消息,我尝试了几种方法在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')

2 个答案:

答案 0 :(得分:1)

首先,您应该使用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']

您可以阅读有关序列here的更多信息。

答案 1 :(得分:0)

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

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

相关问题