Golang操作Rocksdb demomac环境
1. RocksDB 安装
1.1 brew 安装(推荐)
brew install rocksdb
安装完成后, 查看安装路径:
brew info rocksdb
结果如下图(安装路径为):
安装路径为:/opt/homebrew/Cellar/rocksdb/9.5.2
1.2 源码安装(brew安装成功,可忽略)
-
源码下载:
官网选择最新版本: https://github.com/facebook/rocksdb/releases
以9.5.2为例:
下载: https://github.com/facebook/rocksdb/archive/refs/tags/v9.5.2.tar.gz -
解压&安装
tar -xzvf rocksdb-9.5.2.tar.gz
cd rocksdb-9.5.2
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/rocksdb ..
make
make install
- 安装完成后 会在/usr/local/rocksdb 生成include和lib文件夹
1.3 设置rocksdb环境变量
- 编辑 vi ~/.bash_profile, 添加如下内容:
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/local/rocksdb/include/
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/rocksdb/lib/
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/rocksdb/lib:/usr/local/lib
export DYLD_LIBRARY_PATH=/usr/local/rocksdb/lib
bash_profile使生效
source ~/.bash_profile
2. 安装依赖
2.1 安装zstd
cd /usr/local
git clone https://github.com/facebook/zstd.git
cd zstd
make
make install
2.2 安装snappy
brew install snappy
查看snappy安装路径:
brew info snappy
为: /opt/homebrew/Cellar/snappy/1.2.1
2.3 安装lz4
brew install lz4
查看snappy安装路径:
brew info lz4
为: /opt/homebrew/Cellar/lz4/1.10.0
2.5 安装gcc
brew install gcc48
3. rocksdb本地测试
3.1 brew 安装测试
1. 在安装目录下有个bin/rocksdb_ldb可执行文件
2. 添加数据
cd /opt/homebrew/Cellar/rocksdb/9.5.2/bin
sudo ./rocksdb_ldb --db=/tmp/test_db --create_if_missing put a1 b1
结果如下:
3. 查看数据
./rocksdb_ldb --db=/tmp/test_db get a1
结果如下:
3.2 源码安装测试
参考:https://docs.chainmaker.org.cn/v1.2.5/html/tech/RocksDB%E5%AE%89%E8%A3%85%E9%83%A8%E7%BD%B2.html?highlight=rocksdb
4. 安装grocksdb
4.1 说明
- 一开始用的https://github.com/tecbot/gorocksdb, 这个版本和最新的rocksdb 9.*版本有冲突, 里面很多方法找不到, 然后rocksdb 换成5.*版本 安装又是各种报错, 就放弃了
- 后来找的这个go rocksdb库: github.com/linxGnu/grocksdb
4.2 安装grocksdb
go get github.com/linxGnu/grocksdb
4.3 go build / go run
说明:
- CGO_CFLAGS=rocksdb include目录路径
- CGO_LDFLAGS=动态库的目录路径,其中:
-L/opt/homebrew/Cellar/rocksdb/9.5.2/lib -lrocksdb 是rocksdb库路径
-L/opt/homebrew/Cellar/snappy/1.2.1/lib -lsnappy 是snappy库路径
-L/opt/homebrew/Cellar/lz4/1.10.0/lib -llz4 是lz4库路径
go build:
CGO_CFLAGS="-I/opt/homebrew/Cellar/rocksdb/9.5.2/include" CGO_LDFLAGS="-L/opt/homebrew/Cellar/rocksdb/9.5.2/lib -lrocksdb -lstdc++ -lm -lz -L/opt/homebrew/Cellar/snappy/1.2.1/lib -lsnappy -L/opt/homebrew/Cellar/lz4/1.10.0/lib -llz4 -lzstd" go build main.go
go run:
CGO_CFLAGS="-I/opt/homebrew/Cellar/rocksdb/9.5.2/include" CGO_LDFLAGS="-L/opt/homebrew/Cellar/rocksdb/9.5.2/lib -lrocksdb -lstdc++ -lm -lz -L/opt/homebrew/Cellar/snappy/1.2.1/lib -lsnappy -L/opt/homebrew/Cellar/lz4/1.10.0/lib -llz4 -lzstd" go run main.go
5.运行go
go 代码如下:
package main
import (
"context"
"fmt"
"github.com/linxGnu/grocksdb"
"log"
)
func main() {
// 创建 RocksDB 实例
options := grocksdb.NewDefaultOptions()
options.SetCreateIfMissing(true)
db, err := grocksdb.OpenDb(options, "/Users/jemes/workspace/test_db")
if err != nil {
log.Fatalf("failed to open database: %v", err)
}
defer db.Close()
// 创建写选项
writeOptions := grocksdb.NewDefaultWriteOptions()
defer writeOptions.Destroy()
// 写入数据
err = db.Put(writeOptions, []byte("key"), []byte("hahsas"))
if err != nil {
log.Fatalf("failed to write data: %v", err)
}
// 读取数据
readOptions := grocksdb.NewDefaultReadOptions()
defer readOptions.Destroy()
data, err := db.Get(readOptions, []byte("key"))
if err != nil {
log.Fatalf("failed to read data: %v", err)
}
defer data.Free()
fmt.Printf("value: %s\n", data.Data())
}
执行结果