본문 바로가기
프로그래밍/C & C++

rocksdb sample code on ubuntu 18.04

by 체리 2022. 2. 27.
반응형

참고: https://techoverflow.net/2020/01/28/rocksdb-minimal-example-in-c/

 

RocksDB minimal example in C++ – TechOverflow

This minimal example shows how to open a RocksDB database, write a key and how to read it. #include #include #include using namespace std; int main(int argc, char** argv) { rocksdb::DB* db; rocksdb::Options options; options.create_if_missing = true; rocksd

techoverflow.net

 

1. 먼저 rocksdb 설치

18.04인 상태에서 설치를 한다면 기본적으로 rocksdb 5.8.8-1 버전이 설치됨

# apt install librocksdb-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  librocksdb5.8
The following NEW packages will be installed:
  librocksdb-dev librocksdb5.8
0 upgraded, 2 newly installed, 0 to remove and 1 not upgraded.
Need to get 3,803 kB of archives.
After this operation, 28.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] 
Get:1 http://kr.archive.ubuntu.com/ubuntu bionic/universe amd64 librocksdb5.8 amd64 5.8.8-1 [1,450 kB]
Get:2 http://kr.archive.ubuntu.com/ubuntu bionic/universe amd64 librocksdb-dev amd64 5.8.8-1 [2,352 kB]
Fetched 3,803 kB in 3s (1,203 kB/s)       
Selecting previously unselected package librocksdb5.8.
(Reading database ... 133618 files and directories currently installed.)
Preparing to unpack .../librocksdb5.8_5.8.8-1_amd64.deb ...
Unpacking librocksdb5.8 (5.8.8-1) ...
Selecting previously unselected package librocksdb-dev.
Preparing to unpack .../librocksdb-dev_5.8.8-1_amd64.deb ...
Unpacking librocksdb-dev (5.8.8-1) ...
Setting up librocksdb5.8 (5.8.8-1) ...
Setting up librocksdb-dev (5.8.8-1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.4) ...

 

2. sample code를 작성

#include <iostream>
#include <cassert>
#include <string>
#include <rocksdb/db.h>

using namespace std;

int main()
{
    rocksdb::DB* db;
    rocksdb::Options options;
    options.create_if_missing = true;
    rocksdb::Status status = rocksdb::DB::Open(options, "/tmp/testdb", &db);
    assert(status.ok());

    // Insert value
    status = db->Put(rocksdb::WriteOptions(), "Test key", "Test value");
    assert(status.ok());

    // Read back value
    std::string value;
    status = db->Get(rocksdb::ReadOptions(), "Test key", &value);
    assert(status.ok());
    assert(!status.IsNotFound());

    // Read key which does not exist
    status = db->Get(rocksdb::ReadOptions(), "This key does not exist", &value);
    assert(status.IsNotFound());
    
    std::cout << value << std::endl;

    return 0;
}

 

3. build

# g++ main.cpp -lrocksdb

 

4. 실행

# ./a.out
Test value
반응형

댓글0