Skip to content

Commit 01eb959

Browse files
Merge pull request #5 from RedisLabsModules/enforce-python3
Require a Python 3 interpreter, better Unicode support
2 parents e436b37 + d87af85 commit 01eb959

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# redisgraph-bulk-loader
22
A Python utility for building RedisGraph databases from CSV inputs
33

4+
## Requirements
5+
The bulk loader utility requires a Python 3 interpreter.
6+
7+
A Redis server with the [RedisGraph](https://github.com/RedisLabsModules/RedisGraph) module must be running. Installation instructions may be found at:
8+
https://oss.redislabs.com/redisgraph/
9+
410
## Installation
511
The bulk loader script's dependencies can be resolved using pip:
612
```
713
pip install --user -r requirements.txt
814
```
915

10-
A Redis server with the [RedisGraph](https://github.com/RedisLabsModules/RedisGraph) module must be running. Installation instructions may be found at:
11-
https://oss.redislabs.com/redisgraph/
12-
1316
## Usage
1417
bulk_insert.py GRAPHNAME [OPTIONS]
1518

bulk_insert.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import csv
22
import os
33
import io
4+
import sys
45
import struct
56
from timeit import default_timer as timer
67
import redis
78
import click
8-
from backports import csv
99

1010
# Global variables
1111
CONFIGS = None # thresholds for batching Redis queries
@@ -99,17 +99,17 @@ def report_completion(self, runtime):
9999
class EntityFile(object):
100100
def __init__(self, filename):
101101
# The label or relation type string is the basename of the file
102-
self.entity_str = os.path.splitext(os.path.basename(filename))[0].encode('utf-8')
102+
self.entity_str = os.path.splitext(os.path.basename(filename))[0].encode()
103103
# Input file handling
104-
self.infile = io.open(filename, 'rt', encoding='utf-8')
104+
self.infile = io.open(filename, 'rt')
105105
# Initialize CSV reader that ignores leading whitespace in each field
106106
# and does not modify input quote characters
107107
self.reader = csv.reader(self.infile, skipinitialspace=True, quoting=csv.QUOTE_NONE)
108108

109109
self.prop_offset = 0 # Starting index of properties in row
110110
self.prop_count = 0 # Number of properties per entity
111111

112-
self.packed_header = ""
112+
self.packed_header = b''
113113
self.binary_entities = []
114114
self.binary_size = 0 # size of binary token
115115
self.count_entities() # number of entities/row in file.
@@ -143,7 +143,7 @@ def pack_header(self, header):
143143
fmt = "=%dsI" % (len(self.entity_str) + 1) # Unaligned native, entity_string, count of properties
144144
args = [self.entity_str, prop_count]
145145
for p in header[self.prop_offset:]:
146-
prop = p.encode('utf-8')
146+
prop = p.encode()
147147
fmt += "%ds" % (len(prop) + 1) # encode string with a null terminator
148148
args.append(prop)
149149
return struct.pack(fmt, *args)
@@ -291,8 +291,8 @@ def prop_to_binary(prop_str):
291291
return struct.pack(format_str + '?', Type.BOOL, True)
292292

293293
# If we've reached this point, the property is a string
294+
encoded_str = str.encode(prop_str) # struct.pack requires bytes objects as arguments
294295
# Encoding len+1 adds a null terminator to the string
295-
encoded_str = prop_str.encode('utf-8')
296296
format_str += "%ds" % (len(encoded_str) + 1)
297297
return struct.pack(format_str, Type.STRING, encoded_str)
298298

@@ -334,6 +334,9 @@ def bulk_insert(graph, host, port, password, nodes, relations, max_token_count,
334334
global TOP_NODE_ID
335335
global QUERY_BUF
336336

337+
if sys.version_info[0] < 3:
338+
raise Exception("Python 3 is required for the RedisGraph bulk loader.")
339+
337340
TOP_NODE_ID = 0 # reset global ID variable (in case we are calling bulk_insert from unit tests)
338341
CONFIGS = Configs(max_token_count, max_buffer_size, max_token_size)
339342

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
redis==2.10.6
22
click>=6.7
3-
backports.csv

0 commit comments

Comments
 (0)