管理和数据访问工具
Ldb 工具
ldb命令行工具提供多个数据访问和数据库管理命令。下面列出了一些示例。 有关更多信息,请参考运行ldb时不带任何参数显示的帮助消息和tools/ldb_test.py中的单元测试。
示例数据访问序列:
$./ldb --db=/tmp/test_db --create_if_missing put a1 b1
OK
$ ./ldb --db=/tmp/test_db get a1
b1
$ ./ldb --db=/tmp/test_db get a2
Failed: NotFound:
$ ./ldb --db=/tmp/test_db scan
a1 : b1
$ ./ldb --db=/tmp/test_db scan --hex
0x6131 : 0x6231
$ ./ldb --db=/tmp/test_db put --key_hex 0x6132 b2
OK
$ ./ldb --db=/tmp/test_db scan
a1 : b1
a2 : b2
$ ./ldb --db=/tmp/test_db get --value_hex a2
0x6232
$ ./ldb --db=/tmp/test_db get --hex 0x6131
0x6231
$ ./ldb --db=/tmp/test_db batchput a3 b3 a4 b4
OK
$ ./ldb --db=/tmp/test_db scan
a1 : b1
a2 : b2
a3 : b3
a4 : b4
$ ./ldb --db=/tmp/test_db batchput "multiple words key" "multiple words value"
OK
$ ./ldb --db=/tmp/test_db scan
Created bg thread 0x7f4a1dbff700
a1 : b1
a2 : b2
a3 : b3
a4 : b4
multiple words key : multiple words value
以十六进制转储现有leveldb数据库:
$ ./ldb --db=/tmp/test_db dump --hex > /tmp/dbdump
要将转储的十六进制格式数据加载到新的leveldb数据库:
$ cat /tmp/dbdump | ./ldb --db=/tmp/test_db_new load --hex --compression_type=bzip2 --block_size=65536 --create_if_missing --disable_wal
压缩现有的leveldb数据库:
$ ./ldb --db=/tmp/test_db_new compact --compression_type=bzip2 --block_size=65536
您可以指定命令行—column_family=
—try_load_options将尝试加载DB中的选项文件来打开DB。 在操作数据库时,最好总是启用这个选项。 如果使用默认选项打开数据库,可能会打乱无法自动恢复的LSM-tree结构。
SST dump tool
可以使用sst_dump工具了解特定的SST文件。sst_dump可以在一个SST文件上执行多个操作。
$ ./sst_dump
file or directory must be specified.
sst_dump --file=<data_dir_OR_sst_file> [--command=check|scan|raw]
--file=<data_dir_OR_sst_file>
Path to SST file or directory containing SST files
--command=check|scan|raw|verify
check: Iterate over entries in files but dont print anything except if an error is encounterd (default command)
scan: Iterate over entries in files and print them to screen
raw: Dump all the table contents to <file_name>_dump.txt
verify: Iterate all the blocks in files verifying checksum to detect possible coruption but dont print anything except if a corruption is encountered
recompress: reports the SST file size if recompressed with different
compression types
--output_hex
Can be combined with scan command to print the keys and values in Hex
--from=<user_key>
Key to start reading from when executing check|scan
--to=<user_key>
Key to stop reading at when executing check|scan
--prefix=<user_key>
Returns all keys with this prefix when executing check|scan
Cannot be used in conjunction with --from
--read_num=<num>
Maximum number of entries to read when executing check|scan
--verify_checksum
Verify file checksum when executing check|scan
--input_key_hex
Can be combined with --from and --to to indicate that these values are encoded in Hex
--show_properties
Print table properties after iterating over the file when executing
check|scan|raw
--set_block_size=<block_size>
Can be combined with --command=recompress to set the block size that will
be used when trying different compression algorithms
--compression_types=<comma-separated list of CompressionType members, e.g.,
kSnappyCompression>
Can be combined with --command=recompress to run recompression for this
list of compression types
--parse_internal_key=<0xKEY>
Convenience option to parse an internal key on the command line. Dumps the
internal key in hex format {'key' @ SN: type}
dump SST文件块
./sst_dump --file=/path/to/sst/000829.sst --command=raw
该命令将生成一个名为/path/to/sst/000829_dump.txt的txt文件,该文件将包含所有十六进制编码的索引块和数据块。它还将包含表属性、页脚详细信息和元索引详细信息等信息
在SST文件中打印条目
./sst_dump --file=/path/to/sst/000829.sst --command=scan --read_num=5
这个命令将把SST文件中的前5个键打印到屏幕上。输出可能是这样的
'Key1' @ 5: 1 => Value1
'Key2' @ 2: 1 => Value2
'Key3' @ 4: 1 => Value3
'Key4' @ 3: 1 => Value4
'Key5' @ 1: 1 => Value5
输出可以这样解释:
'<key>' @ <sequence number>: <type> => <value>
请注意,如果您的键有非ascii字符,将很难在屏幕上打印它,在这种情况下,使用output_hex是一个好主意,就像这样
./sst_dump --file=/path/to/sst/000829.sst --command=scan --read_num=5 --output_hex
您还可以像这样使用from和to来指定从哪里开始读取,以及从哪里停止读取
./sst_dump --file=/path/to/sst/000829.sst --command=scan --from="key2" --to="key4"
您还可以通过使用input_key_hex将 —from 和 —to使用十六进制
./sst_dump --file=/path/to/sst/000829.sst --command=scan --from="0x6B657932" --to="0x6B657934" --input_key_hex
检测 SST file
./sst_dump --file=/path/to/sst/000829.sst --command=check --verify_checksum
这个命令将遍历SST文件中的所有条目,但是除了在SST文件中遇到问题外,不会打印任何东西。它还将验证校验和
打印SST文件属性
./sst_dump --file=/path/to/sst/000829.sst --show_properties
此命令将读取SST文件属性并打印它们,输出可能如下所示
from [] to []
Process /path/to/sst/000829.sst
Sst file format: block-based
Table Properties:
------------------------------
# data blocks: 26541
# entries: 2283572
raw key size: 264639191
raw average key size: 115.888262
raw value size: 26378342
raw average value size: 11.551351
data block size: 67110160
index block size: 3620969
filter block size: 0
(estimated) table size: 70731129
filter policy name: N/A
# deleted keys: 571272
尝试不同的压缩算法
可以使用sst_dump检查不同压缩算法下文件的大小
./sst_dump --file=/path/to/sst/000829.sst --show_compression_sizes
通过使用—show_compression_size sst_dump将使用不同的压缩算法在内存中重新创建SST文件并报告大小,输出可能如下所示
from [] to []
Process /path/to/sst/000829.sst
Sst file format: block-based
Block Size: 16384
Compression: kNoCompression Size: 103974700
Compression: kSnappyCompression Size: 103906223
Compression: kZlibCompression Size: 80602892
Compression: kBZip2Compression Size: 76250777
Compression: kLZ4Compression Size: 103905572
Compression: kLZ4HCCompression Size: 97234828
Compression: kZSTDNotFinalCompression Size: 79821573
这些文件是在内存中创建的,它们的块大小为16KB,可以使用—set_block_size更改块大小