我已经启动了Tarantool并调用了box.cfg{}进行第一次配置。
下一步:我想在Tarantool中创建一个空间。我阅读了文档,但我并不完全理解所有的内容。
我该怎么做呢?
发布于 2020-09-23 18:05:30
通过Box API创建:
box.schema.sequence.create('user_seq', { if_not_exists = true })
box.schema.create_space('users', { if_not_exists = true, format={
{ name = 'id', type = 'unsigned'},
{ name = 'name', type = 'string'},
{ name = 'age', type = 'unsigned'}}
})
box.space.users:create_index('pk', { parts = { 'id' }, if_not_exists = true })使用if_not_exists,tarantool不会尝试创建已经存在的空间。
创建索引是必须的,因为Tarantool不允许在没有任何索引的情况下在空间中插入数据。
创建空间后,可以插入和选择数据:
box.space.users:insert({ box.sequence.user_seq:next(), 'Artur Barsegyan', 24 })
box.space.users:get({1})---
- - [1, 'Artur Barsegyan']
...你可以阅读更多in the documentation.
发布于 2020-09-23 21:13:49
您不需要手动创建序列;只需传递true,tarantool将创建一个序列,甚至在您删除空格时将其删除。也可以跳过缺省为{1, 'unsigned'}的parts选项
box.space.users:create_index("pk", { if_not_exists = true, sequence = true })https://stackoverflow.com/questions/64025558
复制相似问题