Создаем свою первую базу данных на Tarantool | Tarantool
Документация на русском языке
поддерживается сообществом
Руководство для начинающих Создаем свою первую базу данных на Tarantool

Создаем свою первую базу данных на Tarantool

Example on GitHub: create_db

In this tutorial, you create a Tarantool database, write data to it, and select data from this database.

Before starting this tutorial:

  • Install the tt utility.

  • Install Tarantool.

    Примечание

    The tt utility provides the ability to install Tarantool software using the tt install command.

The tt create command can be used to create an application from a predefined or custom template. In this tutorial, the application layout is prepared manually:

  1. Create a tt environment in the current directory using the tt init command.

  2. Inside the instances.enabled directory of the created tt environment, create the create_db directory.

  3. Inside instances.enabled/create_db, create the instances.yml and config.yaml files:

    • instances.yml specifies instances to run in the current environment. In this example, there is one instance:

      instance001:
      
    • config.yaml contains basic instance configuration:

      groups:
        group001:
          replicasets:
            replicaset001:
              instances:
                instance001:
                  iproto:
                    listen:
                    - uri: '127.0.0.1:3301'
      

      The instance in the configuration accepts incoming requests on the 3301 port.

  1. Start the Tarantool instance from the tt environment directory using tt start:

    $ tt start create_db
    
  2. To check the running instance, use the tt status command:

    $ tt status create_db
    INSTANCE               STATUS   PID   MODE  CONFIG  BOX      UPSTREAM
    create_db:instance001  RUNNING  8685  RW    ready   running  --
    
  3. Connect to the instance with tt connect:

    $ tt connect create_db:instance001
       • Connecting to the instance...
       • Connected to create_db:instance001
    
    create_db:instance001>
    

    This command opens an interactive Tarantool console with the create_db:instance001> prompt. Now you can enter requests in the command line.

  1. Create a space named bands:

    create_db:instance001> box.schema.space.create('bands')
    ---
    - engine: memtx
      before_replace: 'function: 0x010229d788'
      field_count: 0
      is_sync: false
      is_local: false
      on_replace: 'function: 0x010229d750'
      temporary: false
      index: []
      type: normal
      enabled: false
      name: bands
      id: 512
    - created
    ...
    
  2. Форматируйте созданный спейс, указывая имена и типы полей:

    create_db:instance001> box.space.bands:format({
                               { name = 'id', type = 'unsigned' },
                               { name = 'band_name', type = 'string' },
                               { name = 'year', type = 'unsigned' }
                           })
    ---
    ...
    

  1. Create the primary index based on the id field:

    create_db:instance001> box.space.bands:create_index('primary', { parts = { 'id' } })
    ---
    - unique: true
      parts:
      - fieldno: 1
        sort_order: asc
        type: unsigned
        exclude_null: false
        is_nullable: false
      hint: true
      id: 0
      type: TREE
      space_id: 512
      name: primary
    ...
    
  2. Create the secondary index based on the band_name field:

    create_db:instance001> box.space.bands:create_index('secondary', { parts = { 'band_name' } })
    ---
    - unique: true
      parts:
      - fieldno: 2
        sort_order: asc
        type: string
        exclude_null: false
        is_nullable: false
      hint: true
      id: 1
      type: TREE
      space_id: 512
      name: secondary
    ...
    

  1. Insert three tuples into the space:

    create_db:instance001> box.space.bands:insert { 1, 'Roxette', 1986 }
    ---
    - [1, 'Roxette', 1986]
    ...
    create_db:instance001> box.space.bands:insert { 2, 'Scorpions', 1965 }
    ---
    - [2, 'Scorpions', 1965]
    ...
    create_db:instance001> box.space.bands:insert { 3, 'Ace of Base', 1987 }
    ---
    - [3, 'Ace of Base', 1987]
    ...
    
  2. Select a tuple using the primary index:

    create_db:instance001> box.space.bands:select { 3 }
    ---
    - - [3, 'Ace of Base', 1987]
    ...
    
  3. Select tuples using the secondary index:

    create_db:instance001> box.space.bands.index.secondary:select{'Scorpions'}
    ---
    - - [2, 'Scorpions', 1965]
    ...
    
Нашли ответ на свой вопрос?
Обратная связь