| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: commands_load |
| 5 |
|
| 6 |
``load`` |
| 7 |
======== |
| 8 |
|
| 9 |
Summary |
| 10 |
------- |
| 11 |
|
| 12 |
|
| 13 |
``load`` loads data as records in the current database and updates values of each columns. |
| 14 |
|
| 15 |
Syntax |
| 16 |
------ |
| 17 |
|
| 18 |
The required parameters are only ``values`` and ``table``. Other |
| 19 |
parameters are optional:: |
| 20 |
|
| 21 |
load values |
| 22 |
table |
| 23 |
[columns=null] |
| 24 |
[ifexists=null] |
| 25 |
[input_type=json] |
| 26 |
[each=null] |
| 27 |
[output_ids=no] |
| 28 |
[output_errors=no] |
| 29 |
[lock_table=no] |
| 30 |
|
| 31 |
This command is a special command. Other commands need to pass all |
| 32 |
parameters to one line but this command can accept ``values`` as |
| 33 |
followed data. |
| 34 |
|
| 35 |
If you use command line style, you can pass ``values`` like the |
| 36 |
following:: |
| 37 |
|
| 38 |
load --table Bookmarks |
| 39 |
[ |
| 40 |
{"_key": "http://groonga.org/", "title": "Groonga"}, |
| 41 |
{"_key": "http://mroonga.org/", "title": "Mroonga"} |
| 42 |
] |
| 43 |
|
| 44 |
``[...]`` is value of ``values``. |
| 45 |
|
| 46 |
If you use HTTP style, you can pass ``values`` as body:: |
| 47 |
|
| 48 |
% curl \ |
| 49 |
--request POST \ |
| 50 |
--header "Content-Type: application/json" \ |
| 51 |
--data-raw '[{"_key": "http://groonga.org/"}]' \ |
| 52 |
http://localhost:10041/d/load?table=Bookmarks" |
| 53 |
|
| 54 |
|
| 55 |
Usage |
| 56 |
----- |
| 57 |
|
| 58 |
Here is a schema definition to show usage: |
| 59 |
|
| 60 |
.. groonga-command |
| 61 |
.. include:: ../../example/reference/commands/load/usage_setup.log |
| 62 |
.. table_create Entries TABLE_HASH_KEY ShortText |
| 63 |
.. column_create Entries content COLUMN_SCALAR Text |
| 64 |
|
| 65 |
Here is an example to add records to ``Entries`` table by parameter: |
| 66 |
|
| 67 |
.. groonga-command |
| 68 |
.. include:: ../../example/reference/commands/load/usage_parameter.log |
| 69 |
.. load \ |
| 70 |
.. --table Entries \ |
| 71 |
.. --values "[{\"_key\":\"Groonga\",\"content\":\"It's very fast!!\"}]" |
| 72 |
|
| 73 |
Here is an example to add records to ``Entries`` table from standard input: |
| 74 |
|
| 75 |
.. groonga-command |
| 76 |
.. include:: ../../example/reference/commands/load/usage_standard_input.log |
| 77 |
.. load --table Entries |
| 78 |
.. [ |
| 79 |
.. {"_key": "Groonga", "content": "It's very fast!!"} |
| 80 |
.. ] |
| 81 |
|
| 82 |
Here is an example to lock table while updating columns: |
| 83 |
|
| 84 |
.. groonga-command |
| 85 |
.. include:: ../../example/reference/commands/load/usage_lock_table.log |
| 86 |
.. load --table Entries --lock_table yes |
| 87 |
.. [ |
| 88 |
.. {"_key": "Groonga", "content": "It's very fast!!"} |
| 89 |
.. ] |
| 90 |
|
| 91 |
Parameters |
| 92 |
---------- |
| 93 |
|
| 94 |
This section describes all parameters. Parameters are categorized. |
| 95 |
|
| 96 |
Required parameters |
| 97 |
^^^^^^^^^^^^^^^^^^^ |
| 98 |
|
| 99 |
There are some required parameters. |
| 100 |
|
| 101 |
.. _load-values: |
| 102 |
|
| 103 |
``values`` |
| 104 |
"""""""""" |
| 105 |
|
| 106 |
Specifies values to be loaded. |
| 107 |
|
| 108 |
Values should satisfy ``input_type`` format. If you specify ``json`` |
| 109 |
as ``input_type``, you can choose a format from below: |
| 110 |
|
| 111 |
Bracket style:: |
| 112 |
|
| 113 |
[ |
| 114 |
[COLUMN_NAME1, COLUMN_NAME2, ...], |
| 115 |
[VALUE1, VALUE2, ...], |
| 116 |
[VALUE1, VALUE2, ...], |
| 117 |
... |
| 118 |
] |
| 119 |
|
| 120 |
Brace style:: |
| 121 |
|
| 122 |
[ |
| 123 |
{"COLUMN_NAME1": VALUE1, "COLUMN_NAME2": VALUE2, ...}, |
| 124 |
{"COLUMN_NAME1": VALUE1, "COLUMN_NAME2": VALUE2, ...}, |
| 125 |
... |
| 126 |
] |
| 127 |
|
| 128 |
``[COLUMN_NAME1, COLUMN_NAME2, ...]`` in bracket style is effective |
| 129 |
only when :ref:`load-columns` parameter isn't specified. |
| 130 |
|
| 131 |
When a target table contains primary key, you must specify ``_key`` |
| 132 |
column (pseudo column associated primary key) as the one of |
| 133 |
``COLUMN_NAME``. |
| 134 |
|
| 135 |
If you specify ``apache-arrow`` as ``input_type``, you must use |
| 136 |
`Apache Arrow IPC Streaming Format`_. You can't use |
| 137 |
`Apache Arrow IPC File Format`_. |
| 138 |
|
| 139 |
You must use HTTP interface to use Apache Arrow. You can't use Apache |
| 140 |
Arrow in command line interface. |
| 141 |
|
| 142 |
You must set ``application/x-apache-arrow-streaming`` to |
| 143 |
``Content-Type`` HTTP request header. |
| 144 |
|
| 145 |
You must choose suitable record batch size. Groonga loads data per |
| 146 |
record batch. If you choose very large record batch size, Groonga |
| 147 |
can't start loading until whole data of a record batch are received. |
| 148 |
If you choose very small record batch size, Groonga can load data |
| 149 |
incrementally but overhead will be large. Suitable record batch size |
| 150 |
depends on your system but 1024 or so will be suitable. |
| 151 |
|
| 152 |
If ``values`` isn't specified any values, they are read from the |
| 153 |
standard input in command line style or body in HTTP style. |
| 154 |
|
| 155 |
.. _load-table: |
| 156 |
|
| 157 |
``table`` |
| 158 |
""""""""" |
| 159 |
|
| 160 |
Specifies a table name you want to add records. |
| 161 |
|
| 162 |
Optional parameters |
| 163 |
^^^^^^^^^^^^^^^^^^^ |
| 164 |
|
| 165 |
There are some optional parameters. |
| 166 |
|
| 167 |
.. _load-columns: |
| 168 |
|
| 169 |
``columns`` |
| 170 |
""""""""""" |
| 171 |
|
| 172 |
Specifies column names in added records with comma separations. |
| 173 |
|
| 174 |
.. _load-ifexists: |
| 175 |
|
| 176 |
``ifexists`` |
| 177 |
"""""""""""" |
| 178 |
|
| 179 |
Specifies executed expression in |
| 180 |
:doc:`/reference/grn_expr/script_syntax` when the same primary key as |
| 181 |
added records already exists in your table. |
| 182 |
|
| 183 |
If ``ifexists`` specifies expression and its value is ``true``, values |
| 184 |
in other (all columns excluding ``_key`` column) columns is updated. |
| 185 |
|
| 186 |
.. _load-input-type: |
| 187 |
|
| 188 |
``input_type`` |
| 189 |
"""""""""""""" |
| 190 |
|
| 191 |
Specifies an input format for ``values``. |
| 192 |
|
| 193 |
Note that you must also specify suitable HTTP ``Content-Type`` header |
| 194 |
value when you use ``input_type`` with HTTP interface. |
| 195 |
|
| 196 |
Here are available types and ``Content-Type`` values: |
| 197 |
|
| 198 |
.. list-table:: |
| 199 |
:header-rows: 1 |
| 200 |
|
| 201 |
* - Type |
| 202 |
- ``Content-Type`` |
| 203 |
- Description |
| 204 |
* - ``json`` |
| 205 |
- ``application/json`` |
| 206 |
- Use JSON for ``values`` format. |
| 207 |
|
| 208 |
This is the default. |
| 209 |
* - ``apache-arrow`` |
| 210 |
- ``application/x-apache-arrow-streaming`` |
| 211 |
- .. versionadded:: 9.1.1 |
| 212 |
|
| 213 |
Use Apache Arrow for ``values`` format. |
| 214 |
|
| 215 |
.. _load-each: |
| 216 |
|
| 217 |
``each`` |
| 218 |
"""""""" |
| 219 |
|
| 220 |
TODO |
| 221 |
|
| 222 |
.. _load-output-ids: |
| 223 |
|
| 224 |
``output_ids`` |
| 225 |
"""""""""""""" |
| 226 |
|
| 227 |
TODO |
| 228 |
|
| 229 |
.. _load-output-errors: |
| 230 |
|
| 231 |
``output_errors`` |
| 232 |
""""""""""""""""" |
| 233 |
|
| 234 |
TODO |
| 235 |
|
| 236 |
.. _load-lock-table: |
| 237 |
|
| 238 |
``lock_table`` |
| 239 |
"""""""""""""" |
| 240 |
|
| 241 |
.. versionadded:: 8.0.6 |
| 242 |
|
| 243 |
Specifies whether locking table while updating columns. |
| 244 |
|
| 245 |
The default is ``no``. |
| 246 |
|
| 247 |
If you may run destructive commands such as ``load``, ``delete`` and |
| 248 |
so on concurrently, it may break database. For example, if you're |
| 249 |
updating a record by ``load`` and deleting the updating record by |
| 250 |
``delete``, the ``load`` may refer the delete record. |
| 251 |
|
| 252 |
You can guard the update conflict by locking the target table but it |
| 253 |
reduces load performance. |
| 254 |
|
| 255 |
If you specify ``yes`` to this parameter, you can lock the target |
| 256 |
table while updating columns. Here is the update sequence of each |
| 257 |
record: |
| 258 |
|
| 259 |
1. Lock the target table |
| 260 |
2. Add or refer a record to the target table |
| 261 |
3. Unlock the target table |
| 262 |
4. Lock the target table when ``lock_table`` is ``yes`` |
| 263 |
5. Update columns of the target record |
| 264 |
6. Unlock the target table when ``lock_table`` is ``yes`` |
| 265 |
|
| 266 |
Return value |
| 267 |
------------ |
| 268 |
|
| 269 |
The command returns a response with the following format:: |
| 270 |
|
| 271 |
[THE_NUMBER_OF_LOADED_RECORDS] |
| 272 |
|
| 273 |
The command returns a response with the following format with |
| 274 |
:doc:`/reference/command/command_version` 3 or later:: |
| 275 |
|
| 276 |
{ |
| 277 |
"n_loaded_records": THE_NUMBER_OF_LOADED_RECORDS, |
| 278 |
"loaded_ids": [ |
| 279 |
LOADED_RECORD'S_ID1, |
| 280 |
LOADED_RECORD'S_ID2, |
| 281 |
... |
| 282 |
], |
| 283 |
"errors": [ |
| 284 |
{ |
| 285 |
"return_code": RETURN_CODE_FOR_1ST_RECORD, |
| 286 |
"message": MESSAGE_FOR_1ST_RECORD |
| 287 |
}, |
| 288 |
{ |
| 289 |
"return_code": RETURN_CODE_FOR_2ND_RECORD, |
| 290 |
"message": MESSAGE_FOR_2ND_RECORD |
| 291 |
}, |
| 292 |
... |
| 293 |
] |
| 294 |
} |
| 295 |
|
| 296 |
``loaded_ids`` is only included when :ref:`load-output-ids` is ``yes``. |
| 297 |
|
| 298 |
``errors`` is only included when :ref:`load-output-errors` is ``yes``. |
| 299 |
|
| 300 |
See also |
| 301 |
-------- |
| 302 |
|
| 303 |
* :doc:`/reference/grn_expr/script_syntax` |
| 304 |
|
| 305 |
.. _Apache Arrow IPC Streaming Format: https://arrow.apache.org/docs/format/Columnar.html#ipc-streaming-format |
| 306 |
|
| 307 |
.. _Apache Arrow IPC File Format: https://arrow.apache.org/docs/format/Columnar.html#ipc-file-format |