This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitter.py
143 lines (128 loc) · 4.92 KB
/
splitter.py
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
import sys
import os
import argparse
import concurrent.futures
import multiprocessing
from datetime import datetime
"""
This splitter.py script is a helper script to spawn split a csv file into n
partitions, spawn n servers each serving one partition, and run our master
server on a specified port. Example usage:
python3 splitter.py -n=5 -f='./nanocube/data/crime50k.csv' \
-s=',' -t='time' -a='Latitude' -o='Longitude' -c='crime' -p=29512
Will split the csv file into 5 parts and run our master server on port 29512.
"""
ports = []
# parses and returns arguments
def getArgs():
parser = argparse.ArgumentParser(
description='Hosts distributed nanocubes by splitting the input .csv file with specified cols and number of clusters')
parser.add_argument(
'-n',
'--numclusters',
type=int,
help='Number of clusters',
required=True)
parser.add_argument(
'-f', '--filename', type=str, help='.csv file path', required=True)
parser.add_argument(
'-s', '--sep', type=str, help='.csv column separator', required=True)
parser.add_argument(
'-t', '--time', type=str, help='time col name', required=True)
parser.add_argument(
'-a', '--lat', type=str, help='latitude col name', required=True)
parser.add_argument(
'-o', '--lon', type=str, help='longitude col name', required=True)
parser.add_argument(
'-c', '--cat', type=str, help='category col name', required=True)
parser.add_argument(
'-p',
'--port',
type=int,
help='master server port',
required=False,
default=29512)
# Array for all arguments passed to script
args = parser.parse_args()
# Return all arg values
return args.numclusters, args.filename, args.sep, args.time, args.lat, args.lon, args.cat, args.port
# splitcsv takes a csv filename in the $NANOCUBE_SRC/data and splits it into
# numclusters csv files, each with the same header line
def splitcsv(filename, numclusters, sep, timecol):
numLines = sum(1 for line in open(filename))
linesPerFile = numLines / numclusters
count = 0
firstNum = 0
fn = filename.split('.csv')[0] + '_split'
for line in open(filename, 'r'):
if (count == 0):
firstLine = line
else:
fileNum = int(min((count - 1) / linesPerFile + 1, numclusters))
if (fileNum != firstNum):
f = open(fn + str(fileNum) + '.csv', 'w+')
f.write(firstLine)
firstNum = fileNum
f.write(line)
count += 1
# returns a random open port
def get_open_port():
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 0))
s.listen(1)
port = s.getsockname()[1]
s.close()
return port
# hostdmp convers the split csv files to dmp files, hosts the nanocube,
# and deletes the original split .csv file
def hostdmp(filename, sep, timecol, latcol, loncol, catcol, i):
try:
fileNum = i + 1
port = get_open_port()
ports.append(port)
fn = filename.split('.csv')[0] + '_split'
dmp_command = "$NANOCUBE_BIN/nanocube-binning-csv --sep='" + sep + "' --timecol='" + timecol + "' --latcol='" + latcol + "' --loncol='" + loncol + "' \
--catcol='" + catcol + "' " + fn + str(fileNum) + '.csv > '\
+ fn + str(fileNum) + '.dmp'
os.system(dmp_command)
host_command = 'cat ' + fn + \
str(fileNum) + '.dmp | $NANOCUBE_BIN/nanocube-leaf -q ' + str(port) + ' -f 10000 &'
os.system(host_command)
rm_command = 'rm ' + fn + str(fileNum) + '.csv'
os.system(rm_command)
except:
print('error with port')
def main(argv):
if not os.environ.get('NANOCUBE_SRC'):
print("You must set the NANOCUBE_SRC environment variable before " +
"running this script.")
os.exit(1)
# If the NANOCUBE_BIN environment variable is not set, set it to
# $NANOCUBE_SRC/bin.
if not os.environ.get('NANOCUBE_BIN'):
nanobin = os.environ.get('NANOCUBE_SRC') + "/bin"
print("Setting NANOCUBE_BIN to " + nanobin)
os.environ['NANOCUBE_BIN'] = nanobin
(numclusters, filename, sep, timecol, latcol, loncol, catcol, master_port) = getArgs()
splitcsv(filename, numclusters, sep, timecol)
executor = concurrent.futures.ThreadPoolExecutor(
multiprocessing.cpu_count())
futures = [
executor.submit(
hostdmp,
filename,
sep,
timecol,
latcol,
loncol,
catcol,
i) for i in range(numclusters)]
concurrent.futures.wait(futures)
dist_call = "go run cmd/cli/distnano.go -p " + str(master_port) + " "
for port in ports:
dist_call += "-a http://localhost:" + str(port) + " "
# now run distnano.go using the ports as arguments
os.system(dist_call)
if __name__ == '__main__':
main(sys.argv[1:])