Skip to content

Commit 3db89af

Browse files
Allow schema-enforced input CSVs, general refactor (#27)
* Port changes from LDBC branch * wip * Fix module link resolution * Improve documentation * WIP * Fix relation existence check * Tests don't catch exceptions * Refactor QueryBuffer * Comment out fields tests * Simplify node dictionary logic * Add schema tests * Update documentation * Various improvements * Consolidate skip_offset and column_names * Separate binary packing logic for typed and inferred values * Improving logic around types * Documentation update * Update tests * Temporarily disable null-handling test * Update README * [add] added coverage report * [add] added coverage report * Bump required redisgraph-py version * Address PR comments * Replace Config global with instance * Additional test files Co-authored-by: filipecosta90 <[email protected]>
1 parent fd7ea0e commit 3db89af

16 files changed

+901
-416
lines changed

.circleci/config.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ jobs:
3939
command: python setup.py sdist
4040

4141
- run:
42-
name: run tests
42+
name: run tests and upload coverage
4343
command: |
4444
. venv/bin/activate
45-
pip install git+https://github.com/RedisGraph/redisgraph-py.git@master
46-
python -m unittest test/test_bulk_loader.py
45+
pip install -r test/requirements.txt
46+
pytest --cov redisgraph_bulk_loader
47+
codecov -t ${CODECOV_TOKEN}
4748
4849
workflows:
4950
version: 2

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
22
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
33

4+
.coverage
5+
46
# User-specific stuff
57
.idea/**/workspace.xml
68
.idea/**/tasks.xml

README.md

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,65 @@ A Redis server with the [RedisGraph](https://github.com/RedisLabsModules/RedisGr
1717
https://oss.redislabs.com/redisgraph/
1818

1919
## Installation
20-
The bulk loader script's dependencies can be resolved using pip:
20+
The bulk loader can be installed using pip:
2121
```
2222
pip install git+https://github.com/RedisGraph/redisgraph-bulk-loader.git@master
2323
```
2424

2525
## Usage
26+
Pip installation exposes `redisgraph-bulk-insert` as a command to invoke this tool:
27+
```
2628
redisgraph-bulk-insert GRAPHNAME [OPTIONS]
29+
```
30+
31+
Installation by cloning the repository allows the script to be invoked via Python like so:
32+
```
33+
python3 redisgraph_bulk_loader/bulk_insert.py GRAPHNAME [OPTIONS]
34+
```
2735

28-
| Flags | Extended flags | Parameter |
29-
|---------|-----------------------|-----------------------------------------------------------------|
30-
| -h | --host TEXT | Redis server host (default: 127.0.0.1) |
31-
| -p | --port INTEGER | Redis server port (default: 6379) |
32-
| -a | --password TEXT | Redis server password |
33-
| -n | --nodes TEXT | path to node csv file [required] |
34-
| -r | --relations TEXT | path to relationship csv file |
35-
| -t | --max-token-count INT | max number of tokens sent in each Redis query (default 1024) |
36-
| -b | --max-buffer-size INT | max batch size (MBs) of each Redis query (default 4096) |
37-
| -c | --max-token-size INT | max size (MBs) of each token sent to Redis (default 500) |
38-
| -q | --quote | the quoting format used in the CSV file. QUOTE_MINIMAL=0,QUOTE_ALL=1,QUOTE_NONNUMERIC=2,QUOTE_NONE=3 |
39-
| -f | --field-types | json to set explicit types for each field, format {<label>:[<col1 type>, <col2 type> ...]} where type can be 0(null),1(bool),2(numeric),3(string) |
36+
| Flags | Extended flags | Parameter |
37+
|:-----:|----------------------------|:----------------------------------------------------------------------------------------------------:|
38+
| -h | --host TEXT | Redis server host (default: 127.0.0.1) |
39+
| -p | --port INTEGER | Redis server port (default: 6379) |
40+
| -a | --password TEXT | Redis server password (default: none) |
41+
| -n | --nodes TEXT | Path to Node CSV file with the filename as the Node Label |
42+
| -N | --nodes-with-label TEXT | Node Label followed by path to Node CSV file |
43+
| -r | --relations TEXT | Path to Relationship CSV file with the filename as the Relationship Type |
44+
| -R | --relations-with-type TEXT | Relationship Type followed by path to relationship CSV file |
45+
| -o | --separator CHAR | Field token separator in CSV files (default: comma) |
46+
| -d | --enforce-schema | Requires each cell to adhere to the schema defined in the CSV header |
47+
| -s | --skip-invalid-nodes | Skip nodes that reuse previously defined IDs instead of exiting with an error |
48+
| -e | --skip-invalid-edges | Skip edges that use invalid IDs for endpoints instead of exiting with an error |
49+
| -q | --quote INT | The quoting format used in the CSV file. QUOTE_MINIMAL=0,QUOTE_ALL=1,QUOTE_NONNUMERIC=2,QUOTE_NONE=3 |
50+
| -t | --max-token-count INT | (Debug argument) Max number of tokens sent in each Redis query (default 1024) |
51+
| -b | --max-buffer-size INT | (Debug argument) Max batch size (MBs) of each Redis query (default 4096) |
52+
| -c | --max-token-size INT | (Debug argument) Max size (MBs) of each token sent to Redis (default 500) |
4053

4154

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

45-
The flags for `max-token-count`, `max-buffer-size`, and `max-token-size` should only be specified if the memory overhead of graph creation 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. By lowering these limits from their defaults, the size of each transmission to Redis is lowered and fewer entities are held in memory, at the expense of a longer overall runtime.
46-
4758
```
4859
redisgraph-bulk-insert GRAPH_DEMO -n example/Person.csv -n example/Country.csv -r example/KNOWS.csv -r example/VISITED.csv
4960
```
5061
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`.
5162

52-
The default behaviour is to infer the type for each row based on the value of each row, which can cause type mismatch problem. For example if a string property contains string values of 'false', 'true' or numbers. To avoid this, use --field-types to explicitly set the type for each column in the csv.
53-
EG, to explicitly set to string.
63+
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).
5464

55-
```
56-
redisgraph-bulk-insert ROBOTS -f '{"Robots" : [3]}' -q1 -n example2/Robots.csv
57-
```
65+
### Extended parameter descriptions
66+
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. The bulk loader builds large graphs by sending binary tokens (each of which holds multiple nodes or relations) to Redis in batches. By lowering these limits from their defaults, the size of each transmission to Redis is lowered and fewer entities are held in memory, at the expense of a longer overall runtime.
67+
68+
`--quote` is maintained for backwards compatibility, and allows some control over Python's type inference in the default mode. `--enforce-schema-type` is preferred.
5869

59-
Notice that when -f isn't used, the robot name "30165" would be inserted as a number rather than a string which causes problems in RedisGraph when searching.
70+
`--enforce-schema-type` indicates that input CSV headers will follow the form described in [Input Schemas](#input-schemas).
71+
72+
`--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`.
6073

6174
## Input constraints
6275
### Node identifiers
6376
- If both nodes and relations are being created, each node must be associated with a unique identifier.
64-
- 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.
65-
- Each identifier must be entirely unique across all label files.
77+
- 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.
78+
- Each identifier must be entirely unique across all label files. [ID namespaces](#id-namespaces) can be used to write more granular identifiers.
6679
- Source and destination nodes in relation CSV files should be referred to by their identifiers.
6780
- The uniqueness restriction is lifted if only nodes are being created.
6881

@@ -71,22 +84,68 @@ Notice that when -f isn't used, the robot name "30165" would be inserted as a nu
7184
- Properties are not required to be exclusively composed of any type.
7285
- The types currently supported by the bulk loader are:
7386
- `boolean`: either `true` or `false` (case-insensitive, not quote-interpolated).
74-
- `numeric`: an unquoted value that can be read as a floating-point or integer type.
87+
- `integer`: an unquoted value that can be read as an integer type.
88+
- `double`: an unquoted value that can be read as a floating-point type.
7589
- `string`: any field that is either quote-interpolated or cannot be casted to a numeric or boolean type.
76-
- `NULL`: an empty field.
77-
- Default behaviour is to infer the property type, attempting to cast it to null, float, boolean or string in that order.
78-
- If explicit type is required, for example, if a value is "1234" and it must not be inferred into a float, you can use the option -f to specify the type explicitly for each row being imported.
90+
- Cypher does not allow NULL values to be assigned to properties.
91+
- The default behaviour is to infer the property type, attempting to cast it to integer, float, boolean, or string in that order.
92+
- The `--enforce-schema` flag and an [Input Schema](#input-schemas) should be used if type inference is not desired.
7993

8094
### Label file format:
8195
- Each row must have the same number of fields.
8296
- Leading and trailing whitespace is ignored.
83-
- The first field of a label file will be the node identifier, as described in [Node Identifiers](#node-identifiers).
97+
- 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).
8498
- All fields are property keys that will be associated with each node.
8599

86100
### Relationship files
87101
- Each row must have the same number of fields.
88102
- Leading and trailing whitespace is ignored.
89-
- 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.
103+
- 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.
90104
- If the file has more than 2 fields, all subsequent fields are relationship properties that adhere to the same rules as node properties.
91105
- Described relationships are always considered to be directed (source->destination).
92106

107+
## Input Schemas
108+
If the `--enforce-schema` flag is specified, all input CSVs will be expected to specify each column's data type in the header.
109+
110+
This format lifts some constraints of the default CSV format, such as ID fields being the first column.
111+
112+
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.
113+
114+
The accepted data types are:
115+
| Type String | Description | Requires name string |
116+
|:--------------------:|-------------------------------------------------------------------|:--------------------:|
117+
| ID | Label files only - Unique identifier for a node | Optional |
118+
| START_ID | Relation files only - The ID field of this relation's source | No |
119+
| END_ID | Relation files only - The ID field of this relation's destination | No |
120+
| IGNORE | This column will not be added to the graph | Optional |
121+
| DOUBLE / FLOAT | A signed 64-bit floating-point value | Yes |
122+
| INT / INTEGER / LONG | A signed 64-bit integer value | Yes |
123+
| BOOLEAN | A boolean value indicated by the string 'true' or 'false' | Yes |
124+
| STRING | A string value | Yes |
125+
126+
If an `ID` column has a name string, the value will be added to each node as a property. Otherwise, it 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.
127+
128+
### ID Namespaces
129+
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.
130+
131+
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)`.
132+
133+
### Input Schema CSV examples
134+
User.csv
135+
```
136+
:ID(User), name:STRING, rank:INT
137+
0, "Jeffrey", 5
138+
1, "Filipe", 8
139+
```
140+
141+
FOLLOWS.csv
142+
```
143+
:START_ID(User), :END_ID(User), reaction_count:INT
144+
0, 1, 25
145+
1, 0, 10
146+
```
147+
Inserting these CSVs with the command:
148+
`redisgraph-bulk-loader SocialGraph --enforce-schema --nodes User.csv --relations FOLLOWS.csv`
149+
150+
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.
151+

redisgraph_bulk_loader/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from redisgraph_bulk_loader import bulk_insert
2-
31
__all__ = [
42
'bulk_insert',
53
]

0 commit comments

Comments
 (0)