--- title: Architecture practice tags: Tarantool, trainings description: --- # Практика архитектура [TOC] ## Установка Tarantool - Установить Tarantool ```bash $ sudo dnf install tarantool tarantool-devel ``` - Проверить установку ```bash $ tarantool ``` ## Создание спейсов, индексов - tuple ```lua box.tuple.new{1, 2, 3, 4} box.tuple.new{1, 2, 3, 4, "qwe", "qweqweqwe"} box.tuple.new{1, 2, 3, 4, "qwe", "qweqweqwe", {a = 1}} ``` - space ```lua box.cfg{} ``` ```lua box.schema.space.create("test") box.schema.create_space('disk', {engine = 'vinyl'}) box.space.test:insert(box.tuple.new{1, 2, 3, 4, "qwe", "qweqweqwe", {a = 1}}) --fail, index required ``` - index ```lua box.space.test:create_index('primary', {parts = {1, 'int'}}) box.space.test --info box.space.test:insert(box.tuple.new{1, 2, 3, 4, "qwe", "qweqweqwe", {a = 1}}) box.space.test:select() ``` ## Формат спейса ```lua box.space.test:format({{name = "id", type = "num"}, {name = "xx", type = "num"}}) box.space.test:insert({2, "ada"}) -- fail box.space.test:insert({2, 2}) -- success ``` ## SQL ```lua for k=3, 100 do box.space.test:insert({k, k}) end ``` ```sql \set language sql select * from "_space"; select * from "test"; select * from "test" where "xx" in (18, 23, 42); explain select * from "test" where "xx" in (18, 23, 42); ``` ```lua \set lang lua box.execute([[ select * from "test" ]]) ``` ## Работа с HTTP Модуль [http.client](https://www.tarantool.io/en/doc/2.2/reference/reference_lua/http/) ```lua= require'http.client'.get('http://mail.ru') require'http.client'.get('http://httpbin.org/get') local client = require 'http.client' ``` ## Коннекторы - Модуль [net.box](https://www.tarantool.io/en/doc/2.2/reference/reference_lua/net_box/) 1 terminal ```lua box.cfg{listen = 3302} box.schema.user.grant("guest", "super") ``` 2 terminal ```lua $ tarantool require('net.box').connect('0.0.0.0:3302'):eval('print("Hello")') require('net.box').connect('0.0.0.0:3302'):eval('return box.space.test:select()') ``` - java - Установить jdk \* ```bash $ sudo dnf install java-11-openjdk-devel ``` - Установить maven \* ```bash $ sudo dnf install maven ``` - Создать директорию для java приложения ```bash $ mkdir -p connector/src/main/java/connector $ cd connector ``` - В директории `src/main/java/connector/` создать файл [`Connector.java`](https://hb.bizmrg.com/trains/img/connector.java) ```java package connector; import java.util.*; import org.tarantool.TarantoolClient; import org.tarantool.TarantoolClientConfig; import org.tarantool.TarantoolClientImpl; import org.tarantool.Iterator; public class Connector { public static void main(String[] args) { TarantoolClientConfig config = new TarantoolClientConfig(); config.username = "guest"; TarantoolClient client = new TarantoolClientImpl("localhost:3302", config); List<?> result = client.syncOps().select("test", "primary", Collections.singletonList(3), 0, 1, Iterator.EQ); // Чтение записи с id 3 client.syncOps().insert("test", Arrays.asList(101, 101)); // Добавление тапла System.out.println(result); } } ``` - В корневой директории создать файл [`pom.xml`](https://hb.bizmrg.com/trains/img/pom.xml) ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework</groupId> <artifactId>tarantool_connector</artifactId> <packaging>jar</packaging> <version>0.1.0</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>connector.Connector</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.tarantool</groupId> <artifactId>connector</artifactId> <version>1.9.4</version> </dependency> </dependencies> </project> ``` - Собрать проект ```bash $ mvn package ``` - Запустить ```bash $ java -jar target/tarantool_connector-0.1.0.jar ``` - Проверить вывод программы (должен быть тапл [3, 3]) и хранилище test Tarantool (должна добавиться запись [101, 101]) - python - Установить коннектор ```bash $ sudo python3 -m pip install tarantool\>0.4 ``` - ```bash $ python3 ``` ```python import tarantool conn = tarantool.connect("localhost", 3302) test = conn.space('test') ``` - Чтение записи ```python test.select(3) ``` - Добавление записи ```python test.insert((102, 102)) ```