Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GoでDynamoDBのCLI(便利)を作った

 GoでDynamoDBのCLI(便利)を作った

元スライドはGoogle Slideをご参照ください

https://docs.google.com/presentation/d/1wNYydZQckmyKrU0NgwNlUc2vn6V0TuGEaUh1PGkWZyo

shuntaka

June 11, 2022
Tweet

More Decks by shuntaka

Other Decks in Technology

Transcript

  1. 自己紹介 kyoto.go初参加です!宜しくお願いします😌 埼玉からお送りしています! GitHub: https://github.com/shuntaka9576 Twitter: @shuntaka_jp blog: https://shuntaka.dev 仕事:

    AWS,IoT,サーバサイド,TypeScript,Pythonなど Go歴: 仕事ではほぼなく、ときどき趣味で書きます、、 趣味: 原神
  2. BatchRequestを作るのが面倒 { "RequestItems" : RequestItems } RequestItems { "TableName1" :

    [ Request, Request, ... ], "TableName2" : [ Request, Request, ... ], ... } Request ::= PutRequest | DeleteRequest PutRequest ::={ "PutRequest" : { "Item" : { "Attribute-Name1" : Attribute-Value, "Attribute-Name2" : Attribute-Value, ... } } }
  3. リストア仕組み worker Pool worker worker worker worker Batch Writer jsonl

    ① 必要レコード分要 求(default: 25) BatchReq generator ② file.Seek, file.Read で必要な行数取得 ③取得OK ④ ・取りすぎた場合は捨てる ・次読出位置を覚える ・BatchWirteRequestへ変換 ⑤ 返却 ⑥ 書き込み
  4. for { select { case <-ctx.Done(): break LOOP case <-ticker.C:

    limit := *b.LimitUnit for { reqUnitSize := DEFAULT_UNIT_SIZE if limit < reqUnitSize { reqUnitSize = limit } batchReq, err := generator.generate(reqUnitSize) if err != nil { if batchReq.Number() == 0 && err == ErrBatchEOF { close(b.Done) break LOOP } } reqs <- batchReq atomic.AddInt64(b.RemainCount, 1) limit -= batchReq.totalWU if limit == 0 { break } } } } 秒間書き込み制御 1秒に1回イベント受け取る 書き込みリクエスト数を算出 リクエスト生成 リクエストをワーカーへ送信
  5. github.com/alecthomas/kong var CLI struct { Globals Backup struct { TableName

    string `arg:"" name:"tableName" help:"Specify table name to backup."` File string `short:"f" name:"file" help:"Specify the file path to output (default backup_tableName_yyyymmdd-HHMMSS.jsonl)."` Limit int `short:"l" help:"Limit the number of reads per second to the specified number (units are automatically determined as RCUs for provisioned tables and RRUs for on-demand tables)."` } `cmd:"" help:"Backup DynamoDB table."` ...