Skip to content

Commit 7ea930a

Browse files
add post 'Java 에서 RocksDB 사용하기'
Signed-off-by: jonghoonpark <[email protected]>
1 parent 3548c81 commit 7ea930a

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

_posts/2025-08-18-rocksdb.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
layout: "post"
3+
title: "Java 에서 RocksDB 사용하기"
4+
categories:
5+
- "스터디-데이터베이스"
6+
- "개발"
7+
tags:
8+
- "RocksDB"
9+
- "Key Value Store"
10+
- "Facebook"
11+
- "LevelDB"
12+
- "LSM"
13+
- "Log-Structured Merge Tree"
14+
date: "2025-08-18 22:30:00 +0900"
15+
---
16+
17+
[RocksDB](https://rocksdb.org/)는 고성능 Key-Value Store 이다.
18+
19+
LSM(Log-Structured Merge) 트리를 기반으로 동작한다.
20+
21+
RocksDB는 임베디드 형태로 동작한다. 별도의 서버 프로세스를 띄우지 않는다.
22+
23+
---
24+
25+
dependency를 추가한다. gradle의 경우 다음과 같이 추가하면 된다.
26+
27+
```kotlin
28+
implementation("org.rocksdb:rocksdbjni:10.2.1")
29+
```
30+
31+
다음과 같이 간단한 예제를 작성하여 동작을 확인하였다.
32+
33+
```java
34+
public class Main {
35+
36+
private static final File baseDir = new File("/tmp/rocks", "db");
37+
38+
public static void main(String[] args) {
39+
RocksDB.loadLibrary();
40+
41+
final Options options = new Options();
42+
options.setCreateIfMissing(true);
43+
44+
try {
45+
Files.createDirectories(baseDir.getParentFile().toPath());
46+
Files.createDirectories(baseDir.getAbsoluteFile().toPath());
47+
RocksDB db = RocksDB.open(options, baseDir.getAbsolutePath());
48+
49+
db.put("안녕".getBytes(), "하세요".getBytes());
50+
byte[] bytes = db.get("안녕".getBytes());
51+
if (bytes != null) {
52+
System.out.println(new String(bytes, StandardCharsets.UTF_8));
53+
}
54+
} catch(IOException | RocksDBException e) {
55+
System.err.println("Error: " + e.getMessage());
56+
}
57+
}
58+
}
59+
```
60+
61+
`get` 메소드에서 키 값에 해당 되는 값이 없을 경우 `null` 을 반환한다. 따라서 예시에 `null` 체크를 넣어주었다.

0 commit comments

Comments
 (0)