Не так давно на Хабре была опубликована статья о NoSQL базе — «Tarantool 1.6 от первого лица». Уверен, в своих кругах эта база данных отлично известна и уже завоёвывает популярность. Уверен так же и в том, что есть те начинающие, руки не дошли, кто хотел бы попробовать Tarantool в действии. Именно для таких желающих я приведу несколько простых примеров, помогающих начать знакомиться с этим интересным продуктом. Как понятно из названия статьи — речь идет о версии Tarantool 1.6.
wget http://tarantool.org/dist/public.key
sudo apt-key add ./public.key
release=`lsb_release -c -s`
cat > /etc/apt/sources.list.d/tarantool.list <<- EOF
deb http://tarantool.org/dist/master/ubuntu/ $release main
deb-src http://tarantool.org/dist/master/ubuntu/ $release main
EOF
sudo apt-get update
sudo apt-get install tarantool
$ /usr/bin/tarantool
/usr/bin/tarantool: version 1.6.4-509-ga4af00e
type 'help' for interactive help
tarantool> 2 + 3
---
- 5
...
tarantool> print ('Ola lua!')
Ola lua!
---
...
box.cfg {
listen = 3311,
logger = 'tarantool.log',
}
local function hello()
print ('Hello world!)
end
hello()
$ tarantool init.lua
Hello world!
s = box.schema.space.create('test')
p = s:create_index('primary', {type = 'tree', parts = {1, 'NUM'}})
for i = 1, 10 do
s:insert({i, i})
end
box.space.test:insert({i, i})
$ tarantool init.lua
Hello world!
main/101/init.lua F> Space 'test' already exists
s = box.space.test
if not s then
s = box.schema.space.create('test')
p = s:create_index('primary', {type = 'tree', parts = {1, 'NUM'}})
for i = 1, 10 do
s:insert({i, i})
end
end
local console = require('console')
console.listen(127.0.0.1:3312)
$ rlwrap nc 0 3312
Tarantool 1.6.4-509-ga4af00e (Lua console)
type 'help' for interactive help
box.space.test:select()
---
- - [1, 1]
- [2, 2]
- [3, 3]
- [4, 4]
- [5, 5]
- [6, 6]
- [7, 7]
- [8, 8]
- [9, 9]
- [10, 10]
...
box.space.test:get(6)
---
- [6, 6]
...
box.cfg
---
- snapshot_count: 6
too_long_threshold: 0.5
slab_alloc_factor: 2
slab_alloc_maximal: 1048576
background: false
logger: tarantool.log
slab_alloc_arena: 1
sophia:
page_size: 131072
threads: 5
node_size: 134217728
memory_limit: 0
listen: '3311'
logger_nonblock: true
snap_dir: .
coredump: false
sophia_dir: .
wal_mode: write
slab_alloc_minimal: 64
panic_on_snap_error: true
panic_on_wal_error: true
rows_per_wal: 500000
wal_dir: .
log_level: 5
readahead: 16320
snapshot_period: 0
wal_dir_rescan_delay: 0.1
...
for k,v in box.space.test.index.primary:pairs(5, {iterator = box.index.GT}) do
s:update(v[1], {{'=', 2, v[2] + 10}})
end
box.space.test:select()
---
- - [1, 1]
- [2, 2]
- [3, 3]
- [4, 4]
- [5, 5]
- [6, 16]
- [7, 17]
- [8, 18]
- [9, 19]
- [10, 20]
...
-- Пример init cкрипта для tarantool
-- Подключение всех необходимых в скрипте модулей
-- удобно описывать в начале
local console = require('console')
-- Стартуем админскую консоль
console.listen('127.0.0.1:3312')
-- Конфигурация tarantool
box.cfg {
listen = 3311,
logger = 'tarantool.log',
}
local function hello()
print ('Hello world!')
end
-- Запуск функции hello
hello()
-- Создаем и заполняем тестовый space, если он отсутствует
s = box.space.test
if not s then
s = box.schema.space.create('test')
-- Первичный индекс
p = s:create_index('primary', {type = 'tree', parts = {1, 'NUM'}})
for i = 1, 10 do
s:insert({i, i})
end
end
-- Увеличим хранящиеся значения во всех записях, где первичный ключ больше 5
for k,v in box.space.test.index.primary:pairs(5, {iterator = box.index.GT}) do
s:update(v[1], {{'=', 2, v[2] + 10}})
end
К сожалению, не доступен сервер mySQL