|
1 | 1 | API V3
|
2 | 2 | ======
|
3 | 3 |
|
4 |
| -* [Methods](#methods) |
5 |
| - * [new](#new) |
6 |
| - * [get](#get) |
7 |
| - * [set](#set) |
8 |
| - * [setnx](#setnx) |
9 |
| - * [setx](#setx) |
10 |
| - * [delete](#delete) |
11 |
| - * [watch](#watch) |
12 |
| - * [watchcancel](#watchcancel) |
13 |
| - * [readdir](#readdir) |
14 |
| - * [watchdir](#watchdir) |
15 |
| - * [rmdir](#rmdir) |
16 |
| - * [txn](#txn) |
17 |
| - * [version](#version) |
18 |
| - * [grant](#grant) |
19 |
| - * [revoke](#revoke) |
20 |
| - * [keepalive](#keepalive) |
21 |
| - * [timetolive](#timetolive) |
22 |
| - * [leases](#leases) |
| 4 | +- [API V3](#api-v3) |
| 5 | +- [Method](#method) |
| 6 | + - [new](#new) |
| 7 | + - [get](#get) |
| 8 | + - [set](#set) |
| 9 | + - [setnx](#setnx) |
| 10 | + - [setx](#setx) |
| 11 | + - [delete](#delete) |
| 12 | + - [watch](#watch) |
| 13 | + - [watchcancel](#watchcancel) |
| 14 | + - [readdir](#readdir) |
| 15 | + - [watchdir](#watchdir) |
| 16 | + - [rmdir](#rmdir) |
| 17 | + - [txn](#txn) |
| 18 | + - [version](#version) |
| 19 | + - [grant](#grant) |
| 20 | + - [revoke](#revoke) |
| 21 | + - [keepalive](#keepalive) |
| 22 | + - [timetolive](#timetolive) |
| 23 | + - [leases](#leases) |
| 24 | + - [nextkey](#nextkey) |
| 25 | + - [rangeend](#rangeend) |
23 | 26 |
|
24 | 27 | Method
|
25 | 28 | ======
|
@@ -358,3 +361,72 @@ To retrieve lease information.
|
358 | 361 | To list all existing leases.
|
359 | 362 |
|
360 | 363 | [Back to TOP](#api-v3)
|
| 364 | + |
| 365 | +### nextkey |
| 366 | + |
| 367 | +`syntax: next_key = etcd.nextkey(key:string)` |
| 368 | + |
| 369 | +- `key`: etcd path to key |
| 370 | + |
| 371 | +return next path to key, only used when `sort_order='ASCEND' and sort_target='KEY'`. |
| 372 | + |
| 373 | +[Back to TOP](#api-v3) |
| 374 | + |
| 375 | + |
| 376 | +### rangeend |
| 377 | + |
| 378 | +`syntax: range_end = etcd.rangeend(key:string)` |
| 379 | + |
| 380 | +- `key`: etcd path to key |
| 381 | + |
| 382 | +return range end path to key. |
| 383 | + |
| 384 | +When using etcd's `cli:readdir`, if the value in this path is too large (by default: exceeding 4MB), you will encounter the following error: |
| 385 | + |
| 386 | +``` |
| 387 | +rpc error: code = ResourceExhausted desc = grpc: received message larger than max (8653851 vs. 4194304). |
| 388 | +``` |
| 389 | + |
| 390 | +Therefore, we need to read this directory in batches. |
| 391 | + |
| 392 | +The code example : |
| 393 | + |
| 394 | +``` lua |
| 395 | +local cli, _ = etcd.new([option:table]) |
| 396 | + |
| 397 | +local res, err |
| 398 | +local start_key = '/path/to/dir' |
| 399 | +local last_key = start_key |
| 400 | +local range_end = cli.rangeend(start_key) |
| 401 | +local data = {} |
| 402 | +while(1) do |
| 403 | + res, err = cli:readdir(last_key, { |
| 404 | + timeout = 3000, |
| 405 | + range_end = range_end, |
| 406 | + revision = -1, |
| 407 | + limit = 20, -- batch size |
| 408 | + sort_order = 'ASCEND', |
| 409 | + sort_target = 'KEY', |
| 410 | + }) |
| 411 | + -- error handle |
| 412 | + if err or not res then |
| 413 | + break |
| 414 | + end |
| 415 | + -- collect res.body |
| 416 | + table.insert(data, res.body) |
| 417 | + local last_kv_key = res.body.kvs[#res.body.kvs].key |
| 418 | + last_key = cli.nextkey(last_key) |
| 419 | + if not res.body.more then |
| 420 | + break |
| 421 | + end |
| 422 | +end |
| 423 | + |
| 424 | +for _, body in ipairs(data) do |
| 425 | + -- handle data |
| 426 | +end |
| 427 | +``` |
| 428 | + |
| 429 | +[Back to TOP](#api-v3) |
| 430 | + |
| 431 | + |
| 432 | + |
0 commit comments