summaryrefslogtreecommitdiff
path: root/python-redisgraph-bulk-loader.spec
blob: 2e3a3d40a5a97eeee7efcaa822975d9c24fac7e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
%global _empty_manifest_terminate_build 0
Name:		python-redisgraph-bulk-loader
Version:	0.12.3
Release:	1
Summary:	RedisGraph Bulk Import Tool
License:	BSD-3-Clause
URL:		https://pypi.org/project/redisgraph-bulk-loader/
Source0:	https://mirrors.nju.edu.cn/pypi/web/packages/26/2b/45aefcd0c284b5a738960e9537d4b0f12e0ce4660f6611fe5441d119f70c/redisgraph_bulk_loader-0.12.3.tar.gz
BuildArch:	noarch

Requires:	python3-click
Requires:	python3-redis
Requires:	python3-pathos

%description
[![license](https://img.shields.io/github/license/RedisGraph/redisgraph-bulk-loader.svg)](https://github.com/RedisGraph/redisgraph-bulk-loader)
[![CircleCI](https://circleci.com/gh/RedisGraph/redisgraph-bulk-loader/tree/master.svg?style=svg)](https://circleci.com/gh/RedisGraph/redisgraph-bulk-loader/tree/master)
[![Release](https://img.shields.io/github/release/RedisGraph/redisgraph-bulk-loader.svg)](https://github.com/RedisGraph/redisgraph-bulk-loader/releases/latest)
[![PyPI version](https://badge.fury.io/py/redisgraph-bulk-loader.svg)](https://badge.fury.io/py/redisgraph-bulk-loader)
[![Codecov](https://codecov.io/gh/RedisGraph/redisgraph-bulk-loader/branch/master/graph/badge.svg)](https://codecov.io/gh/RedisGraph/redisgraph-bulk-loader)

# redisgraph-bulk-loader
[![Forum](https://img.shields.io/badge/Forum-RedisGraph-blue)](https://forum.redis.com/c/modules/redisgraph)
[![Discord](https://img.shields.io/discord/697882427875393627?style=flat-square)](https://discord.gg/gWBRT6P)

A Python utility for building RedisGraph databases from CSV inputs

## Requirements
The bulk loader utility requires a Python 3 interpreter.

A Redis server with the [RedisGraph](https://github.com/RedisLabsModules/RedisGraph) module must be running. Installation instructions may be found at:
https://oss.redis.com/redisgraph/

## Installation
The bulk loader can be installed using pip:
```
pip install redisgraph-bulk-loader
```
Or
```
pip install git+https://github.com/RedisGraph/redisgraph-bulk-loader.git@master
```

## Usage
Pip installation exposes `redisgraph-bulk-insert` as a command to invoke this tool:
```
redisgraph-bulk-insert GRAPHNAME [OPTIONS]
```

Installation by cloning the repository allows the script to be invoked via Python like so:
```
python3 redisgraph_bulk_loader/bulk_insert.py GRAPHNAME [OPTIONS]
```

| Flags | Extended flags             |                                              Parameter                                               |
|:-----:|----------------------------|:----------------------------------------------------------------------------------------------------:|
|  -u   | --redis-url TEXT                |                                Redis url (default: redis://127.0.0.1:6379)                                |
|  -n   | --nodes TEXT               |                      Path to Node CSV file with the filename as the Node Label                       |
|  -N   | --nodes-with-label TEXT    |                             Node Label followed by path to Node CSV file                             |
|  -r   | --relations TEXT           |               Path to Relationship CSV file with the filename as the Relationship Type               |
|  -R   | --relations-with-type TEXT |                     Relationship Type followed by path to relationship CSV file                      |
|  -o   | --separator CHAR           |                         Field token separator in CSV files (default: comma)                          |
|  -d   | --enforce-schema           |                 Requires each cell to adhere to the schema defined in the CSV header                 |
|  -j   | --id-type TEXT             |                The data type of unique node ID properties (either STRING or INTEGER)                 |
|  -s   | --skip-invalid-nodes       |            Skip nodes that reuse previously defined IDs instead of exiting with an error             |
|  -e   | --skip-invalid-edges       |            Skip edges that use invalid IDs for endpoints instead of exiting with an error            |
|  -q   | --quote INT                | The quoting format used in the CSV file. QUOTE_MINIMAL=0,QUOTE_ALL=1,QUOTE_NONNUMERIC=2,QUOTE_NONE=3 |
|  -t   | --max-token-count INT      |            (Debug argument) Max number of tokens sent in each Redis query (default 1024)             |
|  -b   | --max-buffer-size INT      |                (Debug argument) Max batch size (MBs) of each Redis query (default 64)                |
|  -c   | --max-token-size INT       |               (Debug argument) Max size (MBs) of each token sent to Redis (default 64)               |
|  -i   | --index Label:Property     |              After bulk import, create an Index on provided Label:Property pair (optional)           |
|  -f   | --full-text-index Label:Property     |              After bulk import, create an full text index on provided Label:Property pair (optional)           |


The only required arguments are the name to give the newly-created graph (which can appear anywhere) and at least one node CSV file.
The nodes and relationship flags should be specified once per input file.

```
redisgraph-bulk-insert GRAPH_DEMO -n example/Person.csv -n example/Country.csv -r example/KNOWS.csv -r example/VISITED.csv
```
The label (for nodes) or relationship type (for relationships) is derived from the base name of the input CSV file. In this example, we'll construct two sets of nodes, labeled `Person` and `Country`, and two types of relationships - `KNOWS` and `VISITED`.

RedisGraph does not impose a schema on properties, so the same property key can have values of differing types, such as strings and integers. As such, the bulk loader's default behaviour is to infer the type for each field independently for each value. This can cause unexpected behaviors when, for example, a property expected to always have string values has a field that can be cast to an integer or double. To avoid this, use the `--enforce-schema` flag and update your CSV headers as described in [Input Schemas](#input-schemas).

### Extended parameter descriptions
The flags for `max-token-count`, `max-buffer-size`, and `max-token-size` are typically not required. They should only be specified if the memory overhead of graph creation is too high, or raised if the volume of Redis calls is too high. The bulk loader builds large graphs by sending binary tokens (each of which holds multiple nodes or relations) to Redis in batches.

`--quote` is maintained for backwards compatibility, and allows some control over Python's type inference in the default mode. `--enforce-schema-type` is preferred.

`--enforce-schema-type` indicates that input CSV headers will follow the form described in [Input Schemas](#input-schemas).

`--nodes-with-label` and `--relations-with-type` allows the node label or relationship type to be explicitly written instead of inferring them from the filename. For example, `--relations-with-type HAS_TAG post_hasTag_tag.csv` will add all relationships described in the specified CSV with the type `HAS_TAG`. To specify miltiple label separate them with ':'. For example, `--nodes-with-label Actor:Director actors.csv` will add all nodes described in the specified CSV with the labels `Actor` and `Director`.

## Input constraints
### Node identifiers
- If both nodes and relations are being created, each node must be associated with a unique identifier.
- If not using `--enforce-schema`, the identifier is the first column of each label CSV file. If this column's name starts with an underscore (`_`), the identifier is internal to the bulk loader operation and does not appear in the resulting graph. Otherwise, it is treated as a node property.
- Each identifier must be entirely unique across all label files. [ID namespaces](#id-namespaces) can be used to write more granular identifiers.
- Source and destination nodes in relation CSV files should be referred to by their identifiers.
- The uniqueness restriction is lifted if only nodes are being created.

### Entity properties
- Property types do not need to be explicitly provided.
- Properties are not required to be exclusively composed of any type.
- The types currently supported by the bulk loader are:
    - `bool`: either `true` or `false` (case-insensitive, not quote-interpolated).
    - `integer`: an unquoted value that can be read as an integer type.
    - `double`: an unquoted value that can be read as a floating-point type.
    - `string`: any field that is either quote-interpolated or cannot be casted to a numeric or boolean type.
    - `array`: A bracket-interpolated array of elements of any types. Strings within the array must be explicitly quote-interpolated. Array properties require use of a non-comma delimiter for the CSV (`-o`).
- Cypher does not allow NULL values to be assigned to properties.
- The default behaviour is to infer the property type, attempting to cast it to integer, float, boolean, or string in that order.
- The `--enforce-schema` flag and an [Input Schema](#input-schemas) should be used if type inference is not desired.

### Label file format:
- Each row must have the same number of fields.
- Leading and trailing whitespace is ignored.
- If not using an [Input Schema](#input-schemas), the first field of a label file will be the node identifier, as described in [Node Identifiers](#node-identifiers).
- All fields are property keys that will be associated with each node.

### Relationship files
- Each row must have the same number of fields.
- Leading and trailing whitespace is ignored.
- If not using an [Input Schema](#input-schemas), the first two fields of each row are the source and destination node identifiers. The names of these fields in the header do not matter.
- If the file has more than 2 fields, all subsequent fields are relationship properties that adhere to the same rules as node properties.
- Described relationships are always considered to be directed (source->destination).

### Input CSV example
Store.csv
```
storeNum | Location | daysOpen |
118 | 123 Main St | ['Mon', 'Wed', 'Fri']
136 | 55 Elm St | ['Sat', 'Sun']
```
This CSV would be inserted with the command:
`redisgraph-bulk-insert StoreGraph --separator \| --nodes Store.csv`

(Since the pipe character has meaning in the terminal, it must be backslash-escaped.)

All `storeNum` properties will be inserted as integers, `Location` will be inserted as strings, and `daysOpen` will be inserted as arrays of strings.

## Input Schemas
If the `--enforce-schema` flag is specified, all input CSVs will be expected to specify each column's data type in the header.

This format lifts some constraints of the default CSV format, such as ID fields being the first column.

Most header fields should be a colon-separated pair of the property name and its data type, such as `Name:STRING`. Certain data types do not require a name string, as indicated below.

The accepted data types are:
|     Type String      | Description                                                       | Requires name string |
|:--------------------:|-------------------------------------------------------------------|:--------------------:|
|          ID          | Label files only - Unique identifier for a node                   |       Optional       |
|       START_ID       | Relation files only - The ID field of this relation's source      |          No          |
|        END_ID        | Relation files only - The ID field of this relation's destination |          No          |
|        IGNORE        | This column will not be added to the graph                        |       Optional       |
|    DOUBLE / FLOAT    | A signed 64-bit floating-point value                              |         Yes          |
| INT / INTEGER / LONG | A signed 64-bit integer value                                     |         Yes          |
|    BOOL / BOOLEAN    | A boolean value indicated by the string 'true' or 'false'         |         Yes          |
|        STRING        | A string value                                                    |         Yes          |
|        ARRAY         | An array value                                                    |         Yes          |

If an `ID` column has a name string, the value will be added to each node as a property. This property will be a string by default, though it may be switched to integer using the `--id-type` argument. If the name string is not provided, the ID is internal to the bulk loader operation and will not appear in the graph. `START_ID` and `END_ID` columns will never be added as properties.

### ID Namespaces
Typically, node identifiers need to be unique across all input CSVs. When using an input schema, it is (optionally) possible to create ID namespaces, and the identifier only needs to be unique across its namespace. This is particularly useful when each input CSV has primary keys which overlap with others.

To introduce a namespace, follow the `:ID` type string with a parentheses-interpolated namespace string, such as `:ID(User)`. The same namespace should be specified in the `:START_ID` or `:END_ID` field of relation files, as in `:START_ID(User)`.

### Input Schema CSV examples
User.csv
```
:ID(User), name:STRING, rank:INT
0, "Jeffrey", 5
1, "Filipe", 8
```

FOLLOWS.csv
```
:START_ID(User), :END_ID(User), reaction_count:INT
0, 1, 25
1, 0, 10
```
Inserting these CSVs with the command:
`redisgraph-bulk-insert SocialGraph --enforce-schema --nodes User.csv --relations FOLLOWS.csv`

Will produce a graph named SocialGraph with 2 users, Jeffrey and Filipe. Jeffrey follows Filipe, and that relation has a reaction_count of 25. Filipe also follows Jeffrey, with a reaction_count of 10.

## Performing bulk updates
Pip installation also exposes the command `redisgraph-bulk-update`:
```
redisgraph-bulk-update GRAPHNAME [OPTIONS]
```

Installation by cloning the repository allows the bulk updater to be invoked via Python like so:
```
python3 redisgraph_bulk_loader/bulk_update.py GRAPHNAME [OPTIONS]
```

| Flags | Extended flags           |                         Parameter                          |
|:-----:|--------------------------|:----------------------------------------------------------:|
|  -h   | --host TEXT              |           Redis server host (default: 127.0.0.1)           |
|  -p   | --port INTEGER           |             Redis server port (default: 6379)              |
|  -a   | --password TEXT          |           Redis server password (default: none)            |
|  -u   | --unix-socket-path TEXT  |           Redis unix socket path (default: none)           |
|  -q   | --query TEXT             |                   Query to run on server                   |
|  -v   | --variable-name TEXT     |   Variable name for row array in queries (default: row)    |
|  -c   | --csv TEXT               |                   Path to CSV input file                   |
|  -o   | --separator TEXT         |             Field token separator in CSV file              |
|  -n   | --no-header              |             If set, the CSV file has no header             |
|  -t   | --max-token-size INTEGER | Max size of each token in megabytes (default 500, max 512) |

The bulk updater allows a CSV file to be read in batches and committed to RedisGraph according to the provided query.

For example, given the CSV files described in [Input Schema CSV examples](#input-schema-csv-examples), the bulk loader could create the same nodes and relationships with the commands:
```
redisgraph-bulk-update SocialGraph --csv User.csv --query "MERGE (:User {id: row[0], name: row[1], rank: row[2]})"
redisgraph-bulk-update SocialGraph --csv FOLLOWS.csv --query "MATCH (start {id: row[0]}), (end {id: row[1]}) MERGE (start)-[f:FOLLOWS]->(end) SET f.reaction_count = row[2]"
```

When using the bulk updater, it is essential to sanitize CSV inputs beforehand, as RedisGraph *will* commit changes to the graph incrementally. As such, malformed inputs may leave the graph in a partially-updated state.


%package -n python3-redisgraph-bulk-loader
Summary:	RedisGraph Bulk Import Tool
Provides:	python-redisgraph-bulk-loader
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
BuildRequires:	python3-pip
%description -n python3-redisgraph-bulk-loader
[![license](https://img.shields.io/github/license/RedisGraph/redisgraph-bulk-loader.svg)](https://github.com/RedisGraph/redisgraph-bulk-loader)
[![CircleCI](https://circleci.com/gh/RedisGraph/redisgraph-bulk-loader/tree/master.svg?style=svg)](https://circleci.com/gh/RedisGraph/redisgraph-bulk-loader/tree/master)
[![Release](https://img.shields.io/github/release/RedisGraph/redisgraph-bulk-loader.svg)](https://github.com/RedisGraph/redisgraph-bulk-loader/releases/latest)
[![PyPI version](https://badge.fury.io/py/redisgraph-bulk-loader.svg)](https://badge.fury.io/py/redisgraph-bulk-loader)
[![Codecov](https://codecov.io/gh/RedisGraph/redisgraph-bulk-loader/branch/master/graph/badge.svg)](https://codecov.io/gh/RedisGraph/redisgraph-bulk-loader)

# redisgraph-bulk-loader
[![Forum](https://img.shields.io/badge/Forum-RedisGraph-blue)](https://forum.redis.com/c/modules/redisgraph)
[![Discord](https://img.shields.io/discord/697882427875393627?style=flat-square)](https://discord.gg/gWBRT6P)

A Python utility for building RedisGraph databases from CSV inputs

## Requirements
The bulk loader utility requires a Python 3 interpreter.

A Redis server with the [RedisGraph](https://github.com/RedisLabsModules/RedisGraph) module must be running. Installation instructions may be found at:
https://oss.redis.com/redisgraph/

## Installation
The bulk loader can be installed using pip:
```
pip install redisgraph-bulk-loader
```
Or
```
pip install git+https://github.com/RedisGraph/redisgraph-bulk-loader.git@master
```

## Usage
Pip installation exposes `redisgraph-bulk-insert` as a command to invoke this tool:
```
redisgraph-bulk-insert GRAPHNAME [OPTIONS]
```

Installation by cloning the repository allows the script to be invoked via Python like so:
```
python3 redisgraph_bulk_loader/bulk_insert.py GRAPHNAME [OPTIONS]
```

| Flags | Extended flags             |                                              Parameter                                               |
|:-----:|----------------------------|:----------------------------------------------------------------------------------------------------:|
|  -u   | --redis-url TEXT                |                                Redis url (default: redis://127.0.0.1:6379)                                |
|  -n   | --nodes TEXT               |                      Path to Node CSV file with the filename as the Node Label                       |
|  -N   | --nodes-with-label TEXT    |                             Node Label followed by path to Node CSV file                             |
|  -r   | --relations TEXT           |               Path to Relationship CSV file with the filename as the Relationship Type               |
|  -R   | --relations-with-type TEXT |                     Relationship Type followed by path to relationship CSV file                      |
|  -o   | --separator CHAR           |                         Field token separator in CSV files (default: comma)                          |
|  -d   | --enforce-schema           |                 Requires each cell to adhere to the schema defined in the CSV header                 |
|  -j   | --id-type TEXT             |                The data type of unique node ID properties (either STRING or INTEGER)                 |
|  -s   | --skip-invalid-nodes       |            Skip nodes that reuse previously defined IDs instead of exiting with an error             |
|  -e   | --skip-invalid-edges       |            Skip edges that use invalid IDs for endpoints instead of exiting with an error            |
|  -q   | --quote INT                | The quoting format used in the CSV file. QUOTE_MINIMAL=0,QUOTE_ALL=1,QUOTE_NONNUMERIC=2,QUOTE_NONE=3 |
|  -t   | --max-token-count INT      |            (Debug argument) Max number of tokens sent in each Redis query (default 1024)             |
|  -b   | --max-buffer-size INT      |                (Debug argument) Max batch size (MBs) of each Redis query (default 64)                |
|  -c   | --max-token-size INT       |               (Debug argument) Max size (MBs) of each token sent to Redis (default 64)               |
|  -i   | --index Label:Property     |              After bulk import, create an Index on provided Label:Property pair (optional)           |
|  -f   | --full-text-index Label:Property     |              After bulk import, create an full text index on provided Label:Property pair (optional)           |


The only required arguments are the name to give the newly-created graph (which can appear anywhere) and at least one node CSV file.
The nodes and relationship flags should be specified once per input file.

```
redisgraph-bulk-insert GRAPH_DEMO -n example/Person.csv -n example/Country.csv -r example/KNOWS.csv -r example/VISITED.csv
```
The label (for nodes) or relationship type (for relationships) is derived from the base name of the input CSV file. In this example, we'll construct two sets of nodes, labeled `Person` and `Country`, and two types of relationships - `KNOWS` and `VISITED`.

RedisGraph does not impose a schema on properties, so the same property key can have values of differing types, such as strings and integers. As such, the bulk loader's default behaviour is to infer the type for each field independently for each value. This can cause unexpected behaviors when, for example, a property expected to always have string values has a field that can be cast to an integer or double. To avoid this, use the `--enforce-schema` flag and update your CSV headers as described in [Input Schemas](#input-schemas).

### Extended parameter descriptions
The flags for `max-token-count`, `max-buffer-size`, and `max-token-size` are typically not required. They should only be specified if the memory overhead of graph creation is too high, or raised if the volume of Redis calls is too high. The bulk loader builds large graphs by sending binary tokens (each of which holds multiple nodes or relations) to Redis in batches.

`--quote` is maintained for backwards compatibility, and allows some control over Python's type inference in the default mode. `--enforce-schema-type` is preferred.

`--enforce-schema-type` indicates that input CSV headers will follow the form described in [Input Schemas](#input-schemas).

`--nodes-with-label` and `--relations-with-type` allows the node label or relationship type to be explicitly written instead of inferring them from the filename. For example, `--relations-with-type HAS_TAG post_hasTag_tag.csv` will add all relationships described in the specified CSV with the type `HAS_TAG`. To specify miltiple label separate them with ':'. For example, `--nodes-with-label Actor:Director actors.csv` will add all nodes described in the specified CSV with the labels `Actor` and `Director`.

## Input constraints
### Node identifiers
- If both nodes and relations are being created, each node must be associated with a unique identifier.
- If not using `--enforce-schema`, the identifier is the first column of each label CSV file. If this column's name starts with an underscore (`_`), the identifier is internal to the bulk loader operation and does not appear in the resulting graph. Otherwise, it is treated as a node property.
- Each identifier must be entirely unique across all label files. [ID namespaces](#id-namespaces) can be used to write more granular identifiers.
- Source and destination nodes in relation CSV files should be referred to by their identifiers.
- The uniqueness restriction is lifted if only nodes are being created.

### Entity properties
- Property types do not need to be explicitly provided.
- Properties are not required to be exclusively composed of any type.
- The types currently supported by the bulk loader are:
    - `bool`: either `true` or `false` (case-insensitive, not quote-interpolated).
    - `integer`: an unquoted value that can be read as an integer type.
    - `double`: an unquoted value that can be read as a floating-point type.
    - `string`: any field that is either quote-interpolated or cannot be casted to a numeric or boolean type.
    - `array`: A bracket-interpolated array of elements of any types. Strings within the array must be explicitly quote-interpolated. Array properties require use of a non-comma delimiter for the CSV (`-o`).
- Cypher does not allow NULL values to be assigned to properties.
- The default behaviour is to infer the property type, attempting to cast it to integer, float, boolean, or string in that order.
- The `--enforce-schema` flag and an [Input Schema](#input-schemas) should be used if type inference is not desired.

### Label file format:
- Each row must have the same number of fields.
- Leading and trailing whitespace is ignored.
- If not using an [Input Schema](#input-schemas), the first field of a label file will be the node identifier, as described in [Node Identifiers](#node-identifiers).
- All fields are property keys that will be associated with each node.

### Relationship files
- Each row must have the same number of fields.
- Leading and trailing whitespace is ignored.
- If not using an [Input Schema](#input-schemas), the first two fields of each row are the source and destination node identifiers. The names of these fields in the header do not matter.
- If the file has more than 2 fields, all subsequent fields are relationship properties that adhere to the same rules as node properties.
- Described relationships are always considered to be directed (source->destination).

### Input CSV example
Store.csv
```
storeNum | Location | daysOpen |
118 | 123 Main St | ['Mon', 'Wed', 'Fri']
136 | 55 Elm St | ['Sat', 'Sun']
```
This CSV would be inserted with the command:
`redisgraph-bulk-insert StoreGraph --separator \| --nodes Store.csv`

(Since the pipe character has meaning in the terminal, it must be backslash-escaped.)

All `storeNum` properties will be inserted as integers, `Location` will be inserted as strings, and `daysOpen` will be inserted as arrays of strings.

## Input Schemas
If the `--enforce-schema` flag is specified, all input CSVs will be expected to specify each column's data type in the header.

This format lifts some constraints of the default CSV format, such as ID fields being the first column.

Most header fields should be a colon-separated pair of the property name and its data type, such as `Name:STRING`. Certain data types do not require a name string, as indicated below.

The accepted data types are:
|     Type String      | Description                                                       | Requires name string |
|:--------------------:|-------------------------------------------------------------------|:--------------------:|
|          ID          | Label files only - Unique identifier for a node                   |       Optional       |
|       START_ID       | Relation files only - The ID field of this relation's source      |          No          |
|        END_ID        | Relation files only - The ID field of this relation's destination |          No          |
|        IGNORE        | This column will not be added to the graph                        |       Optional       |
|    DOUBLE / FLOAT    | A signed 64-bit floating-point value                              |         Yes          |
| INT / INTEGER / LONG | A signed 64-bit integer value                                     |         Yes          |
|    BOOL / BOOLEAN    | A boolean value indicated by the string 'true' or 'false'         |         Yes          |
|        STRING        | A string value                                                    |         Yes          |
|        ARRAY         | An array value                                                    |         Yes          |

If an `ID` column has a name string, the value will be added to each node as a property. This property will be a string by default, though it may be switched to integer using the `--id-type` argument. If the name string is not provided, the ID is internal to the bulk loader operation and will not appear in the graph. `START_ID` and `END_ID` columns will never be added as properties.

### ID Namespaces
Typically, node identifiers need to be unique across all input CSVs. When using an input schema, it is (optionally) possible to create ID namespaces, and the identifier only needs to be unique across its namespace. This is particularly useful when each input CSV has primary keys which overlap with others.

To introduce a namespace, follow the `:ID` type string with a parentheses-interpolated namespace string, such as `:ID(User)`. The same namespace should be specified in the `:START_ID` or `:END_ID` field of relation files, as in `:START_ID(User)`.

### Input Schema CSV examples
User.csv
```
:ID(User), name:STRING, rank:INT
0, "Jeffrey", 5
1, "Filipe", 8
```

FOLLOWS.csv
```
:START_ID(User), :END_ID(User), reaction_count:INT
0, 1, 25
1, 0, 10
```
Inserting these CSVs with the command:
`redisgraph-bulk-insert SocialGraph --enforce-schema --nodes User.csv --relations FOLLOWS.csv`

Will produce a graph named SocialGraph with 2 users, Jeffrey and Filipe. Jeffrey follows Filipe, and that relation has a reaction_count of 25. Filipe also follows Jeffrey, with a reaction_count of 10.

## Performing bulk updates
Pip installation also exposes the command `redisgraph-bulk-update`:
```
redisgraph-bulk-update GRAPHNAME [OPTIONS]
```

Installation by cloning the repository allows the bulk updater to be invoked via Python like so:
```
python3 redisgraph_bulk_loader/bulk_update.py GRAPHNAME [OPTIONS]
```

| Flags | Extended flags           |                         Parameter                          |
|:-----:|--------------------------|:----------------------------------------------------------:|
|  -h   | --host TEXT              |           Redis server host (default: 127.0.0.1)           |
|  -p   | --port INTEGER           |             Redis server port (default: 6379)              |
|  -a   | --password TEXT          |           Redis server password (default: none)            |
|  -u   | --unix-socket-path TEXT  |           Redis unix socket path (default: none)           |
|  -q   | --query TEXT             |                   Query to run on server                   |
|  -v   | --variable-name TEXT     |   Variable name for row array in queries (default: row)    |
|  -c   | --csv TEXT               |                   Path to CSV input file                   |
|  -o   | --separator TEXT         |             Field token separator in CSV file              |
|  -n   | --no-header              |             If set, the CSV file has no header             |
|  -t   | --max-token-size INTEGER | Max size of each token in megabytes (default 500, max 512) |

The bulk updater allows a CSV file to be read in batches and committed to RedisGraph according to the provided query.

For example, given the CSV files described in [Input Schema CSV examples](#input-schema-csv-examples), the bulk loader could create the same nodes and relationships with the commands:
```
redisgraph-bulk-update SocialGraph --csv User.csv --query "MERGE (:User {id: row[0], name: row[1], rank: row[2]})"
redisgraph-bulk-update SocialGraph --csv FOLLOWS.csv --query "MATCH (start {id: row[0]}), (end {id: row[1]}) MERGE (start)-[f:FOLLOWS]->(end) SET f.reaction_count = row[2]"
```

When using the bulk updater, it is essential to sanitize CSV inputs beforehand, as RedisGraph *will* commit changes to the graph incrementally. As such, malformed inputs may leave the graph in a partially-updated state.


%package help
Summary:	Development documents and examples for redisgraph-bulk-loader
Provides:	python3-redisgraph-bulk-loader-doc
%description help
[![license](https://img.shields.io/github/license/RedisGraph/redisgraph-bulk-loader.svg)](https://github.com/RedisGraph/redisgraph-bulk-loader)
[![CircleCI](https://circleci.com/gh/RedisGraph/redisgraph-bulk-loader/tree/master.svg?style=svg)](https://circleci.com/gh/RedisGraph/redisgraph-bulk-loader/tree/master)
[![Release](https://img.shields.io/github/release/RedisGraph/redisgraph-bulk-loader.svg)](https://github.com/RedisGraph/redisgraph-bulk-loader/releases/latest)
[![PyPI version](https://badge.fury.io/py/redisgraph-bulk-loader.svg)](https://badge.fury.io/py/redisgraph-bulk-loader)
[![Codecov](https://codecov.io/gh/RedisGraph/redisgraph-bulk-loader/branch/master/graph/badge.svg)](https://codecov.io/gh/RedisGraph/redisgraph-bulk-loader)

# redisgraph-bulk-loader
[![Forum](https://img.shields.io/badge/Forum-RedisGraph-blue)](https://forum.redis.com/c/modules/redisgraph)
[![Discord](https://img.shields.io/discord/697882427875393627?style=flat-square)](https://discord.gg/gWBRT6P)

A Python utility for building RedisGraph databases from CSV inputs

## Requirements
The bulk loader utility requires a Python 3 interpreter.

A Redis server with the [RedisGraph](https://github.com/RedisLabsModules/RedisGraph) module must be running. Installation instructions may be found at:
https://oss.redis.com/redisgraph/

## Installation
The bulk loader can be installed using pip:
```
pip install redisgraph-bulk-loader
```
Or
```
pip install git+https://github.com/RedisGraph/redisgraph-bulk-loader.git@master
```

## Usage
Pip installation exposes `redisgraph-bulk-insert` as a command to invoke this tool:
```
redisgraph-bulk-insert GRAPHNAME [OPTIONS]
```

Installation by cloning the repository allows the script to be invoked via Python like so:
```
python3 redisgraph_bulk_loader/bulk_insert.py GRAPHNAME [OPTIONS]
```

| Flags | Extended flags             |                                              Parameter                                               |
|:-----:|----------------------------|:----------------------------------------------------------------------------------------------------:|
|  -u   | --redis-url TEXT                |                                Redis url (default: redis://127.0.0.1:6379)                                |
|  -n   | --nodes TEXT               |                      Path to Node CSV file with the filename as the Node Label                       |
|  -N   | --nodes-with-label TEXT    |                             Node Label followed by path to Node CSV file                             |
|  -r   | --relations TEXT           |               Path to Relationship CSV file with the filename as the Relationship Type               |
|  -R   | --relations-with-type TEXT |                     Relationship Type followed by path to relationship CSV file                      |
|  -o   | --separator CHAR           |                         Field token separator in CSV files (default: comma)                          |
|  -d   | --enforce-schema           |                 Requires each cell to adhere to the schema defined in the CSV header                 |
|  -j   | --id-type TEXT             |                The data type of unique node ID properties (either STRING or INTEGER)                 |
|  -s   | --skip-invalid-nodes       |            Skip nodes that reuse previously defined IDs instead of exiting with an error             |
|  -e   | --skip-invalid-edges       |            Skip edges that use invalid IDs for endpoints instead of exiting with an error            |
|  -q   | --quote INT                | The quoting format used in the CSV file. QUOTE_MINIMAL=0,QUOTE_ALL=1,QUOTE_NONNUMERIC=2,QUOTE_NONE=3 |
|  -t   | --max-token-count INT      |            (Debug argument) Max number of tokens sent in each Redis query (default 1024)             |
|  -b   | --max-buffer-size INT      |                (Debug argument) Max batch size (MBs) of each Redis query (default 64)                |
|  -c   | --max-token-size INT       |               (Debug argument) Max size (MBs) of each token sent to Redis (default 64)               |
|  -i   | --index Label:Property     |              After bulk import, create an Index on provided Label:Property pair (optional)           |
|  -f   | --full-text-index Label:Property     |              After bulk import, create an full text index on provided Label:Property pair (optional)           |


The only required arguments are the name to give the newly-created graph (which can appear anywhere) and at least one node CSV file.
The nodes and relationship flags should be specified once per input file.

```
redisgraph-bulk-insert GRAPH_DEMO -n example/Person.csv -n example/Country.csv -r example/KNOWS.csv -r example/VISITED.csv
```
The label (for nodes) or relationship type (for relationships) is derived from the base name of the input CSV file. In this example, we'll construct two sets of nodes, labeled `Person` and `Country`, and two types of relationships - `KNOWS` and `VISITED`.

RedisGraph does not impose a schema on properties, so the same property key can have values of differing types, such as strings and integers. As such, the bulk loader's default behaviour is to infer the type for each field independently for each value. This can cause unexpected behaviors when, for example, a property expected to always have string values has a field that can be cast to an integer or double. To avoid this, use the `--enforce-schema` flag and update your CSV headers as described in [Input Schemas](#input-schemas).

### Extended parameter descriptions
The flags for `max-token-count`, `max-buffer-size`, and `max-token-size` are typically not required. They should only be specified if the memory overhead of graph creation is too high, or raised if the volume of Redis calls is too high. The bulk loader builds large graphs by sending binary tokens (each of which holds multiple nodes or relations) to Redis in batches.

`--quote` is maintained for backwards compatibility, and allows some control over Python's type inference in the default mode. `--enforce-schema-type` is preferred.

`--enforce-schema-type` indicates that input CSV headers will follow the form described in [Input Schemas](#input-schemas).

`--nodes-with-label` and `--relations-with-type` allows the node label or relationship type to be explicitly written instead of inferring them from the filename. For example, `--relations-with-type HAS_TAG post_hasTag_tag.csv` will add all relationships described in the specified CSV with the type `HAS_TAG`. To specify miltiple label separate them with ':'. For example, `--nodes-with-label Actor:Director actors.csv` will add all nodes described in the specified CSV with the labels `Actor` and `Director`.

## Input constraints
### Node identifiers
- If both nodes and relations are being created, each node must be associated with a unique identifier.
- If not using `--enforce-schema`, the identifier is the first column of each label CSV file. If this column's name starts with an underscore (`_`), the identifier is internal to the bulk loader operation and does not appear in the resulting graph. Otherwise, it is treated as a node property.
- Each identifier must be entirely unique across all label files. [ID namespaces](#id-namespaces) can be used to write more granular identifiers.
- Source and destination nodes in relation CSV files should be referred to by their identifiers.
- The uniqueness restriction is lifted if only nodes are being created.

### Entity properties
- Property types do not need to be explicitly provided.
- Properties are not required to be exclusively composed of any type.
- The types currently supported by the bulk loader are:
    - `bool`: either `true` or `false` (case-insensitive, not quote-interpolated).
    - `integer`: an unquoted value that can be read as an integer type.
    - `double`: an unquoted value that can be read as a floating-point type.
    - `string`: any field that is either quote-interpolated or cannot be casted to a numeric or boolean type.
    - `array`: A bracket-interpolated array of elements of any types. Strings within the array must be explicitly quote-interpolated. Array properties require use of a non-comma delimiter for the CSV (`-o`).
- Cypher does not allow NULL values to be assigned to properties.
- The default behaviour is to infer the property type, attempting to cast it to integer, float, boolean, or string in that order.
- The `--enforce-schema` flag and an [Input Schema](#input-schemas) should be used if type inference is not desired.

### Label file format:
- Each row must have the same number of fields.
- Leading and trailing whitespace is ignored.
- If not using an [Input Schema](#input-schemas), the first field of a label file will be the node identifier, as described in [Node Identifiers](#node-identifiers).
- All fields are property keys that will be associated with each node.

### Relationship files
- Each row must have the same number of fields.
- Leading and trailing whitespace is ignored.
- If not using an [Input Schema](#input-schemas), the first two fields of each row are the source and destination node identifiers. The names of these fields in the header do not matter.
- If the file has more than 2 fields, all subsequent fields are relationship properties that adhere to the same rules as node properties.
- Described relationships are always considered to be directed (source->destination).

### Input CSV example
Store.csv
```
storeNum | Location | daysOpen |
118 | 123 Main St | ['Mon', 'Wed', 'Fri']
136 | 55 Elm St | ['Sat', 'Sun']
```
This CSV would be inserted with the command:
`redisgraph-bulk-insert StoreGraph --separator \| --nodes Store.csv`

(Since the pipe character has meaning in the terminal, it must be backslash-escaped.)

All `storeNum` properties will be inserted as integers, `Location` will be inserted as strings, and `daysOpen` will be inserted as arrays of strings.

## Input Schemas
If the `--enforce-schema` flag is specified, all input CSVs will be expected to specify each column's data type in the header.

This format lifts some constraints of the default CSV format, such as ID fields being the first column.

Most header fields should be a colon-separated pair of the property name and its data type, such as `Name:STRING`. Certain data types do not require a name string, as indicated below.

The accepted data types are:
|     Type String      | Description                                                       | Requires name string |
|:--------------------:|-------------------------------------------------------------------|:--------------------:|
|          ID          | Label files only - Unique identifier for a node                   |       Optional       |
|       START_ID       | Relation files only - The ID field of this relation's source      |          No          |
|        END_ID        | Relation files only - The ID field of this relation's destination |          No          |
|        IGNORE        | This column will not be added to the graph                        |       Optional       |
|    DOUBLE / FLOAT    | A signed 64-bit floating-point value                              |         Yes          |
| INT / INTEGER / LONG | A signed 64-bit integer value                                     |         Yes          |
|    BOOL / BOOLEAN    | A boolean value indicated by the string 'true' or 'false'         |         Yes          |
|        STRING        | A string value                                                    |         Yes          |
|        ARRAY         | An array value                                                    |         Yes          |

If an `ID` column has a name string, the value will be added to each node as a property. This property will be a string by default, though it may be switched to integer using the `--id-type` argument. If the name string is not provided, the ID is internal to the bulk loader operation and will not appear in the graph. `START_ID` and `END_ID` columns will never be added as properties.

### ID Namespaces
Typically, node identifiers need to be unique across all input CSVs. When using an input schema, it is (optionally) possible to create ID namespaces, and the identifier only needs to be unique across its namespace. This is particularly useful when each input CSV has primary keys which overlap with others.

To introduce a namespace, follow the `:ID` type string with a parentheses-interpolated namespace string, such as `:ID(User)`. The same namespace should be specified in the `:START_ID` or `:END_ID` field of relation files, as in `:START_ID(User)`.

### Input Schema CSV examples
User.csv
```
:ID(User), name:STRING, rank:INT
0, "Jeffrey", 5
1, "Filipe", 8
```

FOLLOWS.csv
```
:START_ID(User), :END_ID(User), reaction_count:INT
0, 1, 25
1, 0, 10
```
Inserting these CSVs with the command:
`redisgraph-bulk-insert SocialGraph --enforce-schema --nodes User.csv --relations FOLLOWS.csv`

Will produce a graph named SocialGraph with 2 users, Jeffrey and Filipe. Jeffrey follows Filipe, and that relation has a reaction_count of 25. Filipe also follows Jeffrey, with a reaction_count of 10.

## Performing bulk updates
Pip installation also exposes the command `redisgraph-bulk-update`:
```
redisgraph-bulk-update GRAPHNAME [OPTIONS]
```

Installation by cloning the repository allows the bulk updater to be invoked via Python like so:
```
python3 redisgraph_bulk_loader/bulk_update.py GRAPHNAME [OPTIONS]
```

| Flags | Extended flags           |                         Parameter                          |
|:-----:|--------------------------|:----------------------------------------------------------:|
|  -h   | --host TEXT              |           Redis server host (default: 127.0.0.1)           |
|  -p   | --port INTEGER           |             Redis server port (default: 6379)              |
|  -a   | --password TEXT          |           Redis server password (default: none)            |
|  -u   | --unix-socket-path TEXT  |           Redis unix socket path (default: none)           |
|  -q   | --query TEXT             |                   Query to run on server                   |
|  -v   | --variable-name TEXT     |   Variable name for row array in queries (default: row)    |
|  -c   | --csv TEXT               |                   Path to CSV input file                   |
|  -o   | --separator TEXT         |             Field token separator in CSV file              |
|  -n   | --no-header              |             If set, the CSV file has no header             |
|  -t   | --max-token-size INTEGER | Max size of each token in megabytes (default 500, max 512) |

The bulk updater allows a CSV file to be read in batches and committed to RedisGraph according to the provided query.

For example, given the CSV files described in [Input Schema CSV examples](#input-schema-csv-examples), the bulk loader could create the same nodes and relationships with the commands:
```
redisgraph-bulk-update SocialGraph --csv User.csv --query "MERGE (:User {id: row[0], name: row[1], rank: row[2]})"
redisgraph-bulk-update SocialGraph --csv FOLLOWS.csv --query "MATCH (start {id: row[0]}), (end {id: row[1]}) MERGE (start)-[f:FOLLOWS]->(end) SET f.reaction_count = row[2]"
```

When using the bulk updater, it is essential to sanitize CSV inputs beforehand, as RedisGraph *will* commit changes to the graph incrementally. As such, malformed inputs may leave the graph in a partially-updated state.


%prep
%autosetup -n redisgraph-bulk-loader-0.12.3

%build
%py3_build

%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
	find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
	find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
	find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
	find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
	find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .

%files -n python3-redisgraph-bulk-loader -f filelist.lst
%dir %{python3_sitelib}/*

%files help -f doclist.lst
%{_docdir}/*

%changelog
* Wed May 31 2023 Python_Bot <Python_Bot@openeuler.org> - 0.12.3-1
- Package Spec generated