|
| 1 | +--- |
| 2 | +code: false |
| 3 | +type: page |
| 4 | +title: Batch Processing |
| 5 | +description: Process batches of data with the SDK |
| 6 | +order: 600 |
| 7 | +--- |
| 8 | + |
| 9 | +# Batch Processing |
| 10 | + |
| 11 | +Most of the methods of the Document controller have a batch alternative: |
| 12 | + - create => mCreate |
| 13 | + - replace => mReplace |
| 14 | + - createOrReplace => mCreateOrReplace |
| 15 | + - update => mUpdate |
| 16 | + - get => mGet |
| 17 | + - exists => mGet |
| 18 | + - delete => mDelete |
| 19 | + |
| 20 | +Those methods can be used to process batches of documents at once and increase performances. |
| 21 | + |
| 22 | +## BatchController |
| 23 | + |
| 24 | +Although the m* methods offer very good performances when handling documents, they will need a refactor of the code and architecture. |
| 25 | + |
| 26 | +Instead the [BatchController](/sdk/js/7/core-classes/batch-controller/introduction) provides a consistent way to deal with documents per batch. |
| 27 | + |
| 28 | +It overloads the original DocumentController but methods will be executed in batch at a fixed interval. |
| 29 | + |
| 30 | +The BatchController is usable without modifying the original code, just by replacing the original calls to `document.*` to `batch.*` |
| 31 | + |
| 32 | +::: info |
| 33 | +The BatchController can be used with [strong typing](/sdk/js/7/essentials/strong-typing) like the Document controller. |
| 34 | + |
| 35 | +```js |
| 36 | +const doc = await batch.get<DeviceContent>('nyc-open-data', 'yellow-taxi', 'aschen'); |
| 37 | +``` |
| 38 | + |
| 39 | +::: |
| 40 | + |
| 41 | + |
| 42 | +**Example:** |
| 43 | + |
| 44 | +```js |
| 45 | +import { BatchController, Kuzzle, Http } from 'kuzzle'; |
| 46 | + |
| 47 | +const sdk = new Kuzzle(new Http('localhost')); |
| 48 | + |
| 49 | +const batch = new BatchController(sdk); |
| 50 | + |
| 51 | +// Same as sdk.document.exists but executed in a batch |
| 52 | +const exists = await batch.exists('city', 'galle', 'dana'); |
| 53 | + |
| 54 | +if (exists) { |
| 55 | + // Same as sdk.document.update but executed in a batch |
| 56 | + await batch.update('city', 'galle', 'dana', { power: 'off' }); |
| 57 | +} |
| 58 | +else { |
| 59 | + // Same as sdk.document.create but executed in a batch |
| 60 | + await batch.create('city', 'galle', { power: 'off' }, 'dana'); |
| 61 | +} |
| 62 | + |
| 63 | +// Original sdk.document.search method |
| 64 | +const results = await batch.search('city', 'galle', {}); |
| 65 | +``` |
| 66 | + |
| 67 | +::: warning |
| 68 | +Standard API errors will not be available. |
| 69 | +Except for the `services.storage.not_found` error. |
| 70 | +::: |
| 71 | + |
| 72 | +By default, the BatchController send a batch of document every 10ms. This can be configured when instantiating the BatchController through the `options.interval` [constructor](/sdk/js/7/core-classes/batch-controller/constructor) parameter. |
| 73 | + |
| 74 | +::: info |
| 75 | +Depending on your load, you may want to increase the timer interval to execute bigger batch. |
| 76 | +A bigger interval will also mean more time between two batch and potentialy degraded performances. |
| 77 | +The default value of 10ms offer a good balance between batch size and maximum delay between two batch and should be suitable for most situations. |
| 78 | +::: |
| 79 | + |
| 80 | + |
0 commit comments