Skip to content

Commit ee4e384

Browse files
committed
added parallel read table by shard key ranges
1 parent 121aceb commit ee4e384

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

examples/read_table/main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"context"
55
"flag"
66
"fmt"
7+
"github.com/ydb-platform/ydb-go-sdk/v3/table"
78
"log"
89
"os"
910
"path"
11+
"sync"
1012

1113
environ "github.com/ydb-platform/ydb-go-sdk-auth-environ"
1214
ydb "github.com/ydb-platform/ydb-go-sdk/v3"
@@ -204,4 +206,35 @@ func main() {
204206
if err != nil {
205207
panic(fmt.Errorf("read table error: %w", err))
206208
}
209+
210+
log.Println("Parallel read all rows from shards")
211+
var description options.Description
212+
err = db.Table().Do(ctx, func(ctx context.Context, s table.Session) (err error) {
213+
description, err = s.DescribeTable(ctx, tableName)
214+
if err != nil {
215+
return err
216+
}
217+
218+
return nil
219+
})
220+
if err != nil {
221+
panic(fmt.Errorf("describe table error: %w", err))
222+
}
223+
var wg sync.WaitGroup
224+
wg.Add(len(description.KeyRanges))
225+
for _, shard := range description.KeyRanges {
226+
go func(options.KeyRange) {
227+
defer wg.Done()
228+
err = readTable(
229+
ctx,
230+
db.Table(),
231+
path.Join(prefix, tableName),
232+
options.ReadKeyRange(shard),
233+
)
234+
if err != nil {
235+
panic(fmt.Errorf("shard %q read error: %w", shard.String(), err))
236+
}
237+
}(shard)
238+
}
239+
wg.Wait()
207240
}

0 commit comments

Comments
 (0)