File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed
templates/dart/guides/search Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ import 'dart:convert';
2+ import 'dart:io';
3+
4+ { {> snippets/import} }
5+
6+ void saveObjectsChunks() async {
7+ const path = ' /tmp/records.json' ;
8+ final json = File(path).readAsStringSync();
9+ List< Map< String, dynamic>> records;
10+ try {
11+ records = List< Map< String, dynamic>> .from(jsonDecode(json));
12+ } catch (e) {
13+ throw Exception(' Failed to read file at $path: $e' );
14+ }
15+
16+ { {> snippets/init} }
17+
18+ const chunkSize = 10000;
19+
20+ for (var i = 0; i < records.length; i += chunkSize) {
21+ var chunk = records.sublist(i, (i + chunkSize > records.length) ? records.length : i + chunkSize);
22+ var batchParams = BatchWriteParams(
23+ requests: chunk.map((record) =>
24+ BatchRequest(
25+ action: Action.addObject,
26+ body: record,
27+ )
28+ ).toList()
29+ );
30+ await {{#dynamicSnippet} }batchChunks{ {/dynamicSnippet} };
31+ }
32+ }
33+
Original file line number Diff line number Diff line change 2828 "requests" : " $var: requests"
2929 }
3030 },
31+ "batchChunks" : {
32+ "method" : " batch" ,
33+ "parameters" : {
34+ "indexName" : " chunked_index" ,
35+ "batchWriteParams" : " $var: batchParams"
36+ }
37+ },
3138 "searchWithSearchParams" : {
3239 "method" : " searchSingleIndex" ,
3340 "parameters" : {
You can’t perform that action at this time.
0 commit comments