Skip to content

Commit fc7cbac

Browse files
authored
add ability to create an index after the import (#38)
* add ability to create an index after the import * fix typo and reword to be more clear
1 parent d4bf196 commit fc7cbac

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ python3 redisgraph_bulk_loader/bulk_insert.py GRAPHNAME [OPTIONS]
5454
| -t | --max-token-count INT | (Debug argument) Max number of tokens sent in each Redis query (default 1024) |
5555
| -b | --max-buffer-size INT | (Debug argument) Max batch size (MBs) of each Redis query (default 4096) |
5656
| -c | --max-token-size INT | (Debug argument) Max size (MBs) of each token sent to Redis (default 500) |
57+
| -i | --index Label:Property | After bulk import, create an Index on provided Label:Property pair (optional) |
5758

5859

5960
The only required arguments are the name to give the newly-created graph (which can appear anywhere) and at least one node CSV file.

redisgraph_bulk_loader/bulk_insert.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ def process_entities(entities):
6262
@click.option('--max-token-count', '-c', default=1024, help='max number of processed CSVs to send per query (default 1024)')
6363
@click.option('--max-buffer-size', '-b', default=2048, help='max buffer size in megabytes (default 2048)')
6464
@click.option('--max-token-size', '-t', default=500, help='max size of each token in megabytes (default 500, max 512)')
65-
def bulk_insert(graph, host, port, password, nodes, nodes_with_label, relations, relations_with_type, separator, enforce_schema, skip_invalid_nodes, skip_invalid_edges, quote, max_token_count, max_buffer_size, max_token_size):
65+
@click.option('--index', '-i', multiple=True, help='Label:Propery on which to create an index')
66+
def bulk_insert(graph, host, port, password, nodes, nodes_with_label, relations, relations_with_type, separator, enforce_schema, skip_invalid_nodes, skip_invalid_edges, quote, max_token_count, max_buffer_size, max_token_size, index):
6667
if sys.version_info[0] < 3:
6768
raise Exception("Python 3 is required for the RedisGraph bulk loader.")
6869

@@ -115,6 +116,16 @@ def bulk_insert(graph, host, port, password, nodes, nodes_with_label, relations,
115116
end_time = timer()
116117
query_buf.report_completion(end_time - start_time)
117118

119+
for i in index:
120+
l, p = i.split(":")
121+
print("Creating Index on Label: %s, Property: %s" %(l, p))
122+
try:
123+
index_create = client.execute_command("GRAPH.QUERY", graph, "CREATE INDEX ON :%s(%s)" %(l, p))
124+
for z in index_create:
125+
print(z[0].decode("utf-8") )
126+
except redis.exceptions.ResponseError as e:
127+
print("Unable to create Index on Label: %s, Property %s" %(l, p))
128+
print(e)
118129

119130
if __name__ == '__main__':
120131
bulk_insert()

test/test_bulk_loader.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,31 @@ def test16_error_on_schema_failure(self):
660660
self.assertEqual(sys.exc_info()[0].__name__, 'SchemaError')
661661
self.assertIn("Could not parse 'strval' as an array", e.args)
662662

663+
def test17_ensure_index_is_created(self):
664+
graphname = "index_test"
665+
with open('/tmp/nodes_index.tmp', mode='w') as csv_file:
666+
out = csv.writer(csv_file, delimiter='|')
667+
out.writerow(['name:STRING', 'age:INT'])
668+
out.writerow(['Alex', 17])
669+
out.writerow(['Sean', 12])
670+
csv_file.close()
671+
672+
runner = CliRunner()
673+
res = runner.invoke(bulk_insert, ['--nodes-with-label', 'Person', '/tmp/nodes_index.tmp',
674+
'--separator', '|',
675+
'--index', 'Person:age',
676+
'--enforce-schema',
677+
graphname], catch_exceptions=False)
678+
679+
self.assertEqual(res.exit_code, 0)
680+
self.assertIn('2 nodes created', res.output)
681+
self.assertIn('Indices created: 1', res.output)
682+
683+
graph = Graph(graphname, self.redis_con)
684+
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
685+
res = r.execute_command("GRAPH.EXPLAIN", graphname, 'MATCH (p:Person) WHERE p.age > 16 RETURN p')
686+
self.assertIn(' Index Scan | (p:Person)', res)
687+
663688

664689
if __name__ == '__main__':
665690
unittest.main()

0 commit comments

Comments
 (0)