-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin.py
611 lines (498 loc) · 15 KB
/
join.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
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
"""
Join dim and hash partitioned fact.
Campaign meta:
['campaign_00', 'video campaign "euhvnlkkhy"', 'metadata 1']
['campaign_01', 'video campaign "vuozqifebt"', 'metadata 1']
['campaign_02', 'video campaign "mbtfddmblb"', 'metadata 1']
...
Impressions.
['50000', 'campaign_00', 'video_0', '0.5']
['50018', 'campaign_08', 'video_0', '0.3333333333333333']
['50020', 'campaign_00', 'video_0', '0.25']
['50030', 'campaign_00', 'video_0', '0.2']
...
"""
import os, sys, csv, gzip
import io, time
import tempfile
import asyncio
import aioboto3
import boto3, shutil, string, random
from pprint import pprint as pp
from os.path import isdir, isfile, join
from include.upload import read_fact_from_s3, producer,consumer, upload_gzipped, linesep, colsep, get_pid
try:
import cStringIO
except ImportError:
import io as cStringIO
e=sys.exit
s3 = boto3.client('s3')
bucket_name = 'crossix-test'
fact_name = 'campaign_2k_c10.csv.gz'
ufn = 'unique_cn.csv'
sql_stmt = """SELECT * FROM s3object S """
cdim_fn = 'cdim_1000.csv'
cdim_s3fn = 'cdim_1000.csv.gz'
cfact_s3fn = 'campaign_100k_c1000.csv.gz'
if 0: #Create dim
with open(cdim_fn, mode='w') as fh:
csvw = csv.writer(fh, delimiter = ',', quotechar = '"', lineterminator = '\n', quoting=csv.QUOTE_MINIMAL)
for i in range(1000):
row=['campaign_%04d' % (i), 'video campaign "%s"' % \
(''.join(random.choice(string.ascii_lowercase) for i in range(100))), 'metadata 1']
#print (row)
csvw.writerow(row)
e()
if 0: #Create fact
with open('campaign_100k_c1000.csv', mode='w') as fh:
csvw = csv.writer(fh, delimiter = ',', quotechar = '"', lineterminator = '\n', quoting=csv.QUOTE_MINIMAL)
for i in range(100000):
row=[50000+i,'campaign_%04d' % (int(i)%1000), 'video_%s' % (int(i)//100), 1/(i//10+2)]
#print(row)
csvw.writerow(row)
e()
s = time.perf_counter()
if 0: #Fact partitioner
FACT_COLCNT=4
fact={}
s3_prefix = 'tables'
s3_fact_tn = 'FACT_IMPRESSIONS'
req_fact = s3.select_object_content(
Bucket = bucket_name,
Key = fact_name,
ExpressionType = 'SQL',
Expression = sql_stmt,
InputSerialization = { 'CompressionType':'GZIP',
'CSV': {'FileHeaderInfo': 'None'}},
OutputSerialization = {'CSV': {
'RecordDelimiter': '\n',
'FieldDelimiter': ','}},
#ScanRange = {'Start':0, 'End':40000 }
)
async def partition_fact():
queue = asyncio.Queue()
cid_queue = asyncio.Queue()
coro= {}
await asyncio.gather( producer(req_fact,queue), consumer(queue, coro,cid_queue),\
upload_gzipped(coro,cid_queue, bucket_name, s3_prefix, s3_fact_tn ))
await queue.join()
await cid_queue.join()
if 1:
asyncio.run(partition_fact())
e()
DIM_COLCNT=3
dim={}
s3_prefix = 'tables'
s3_dimtn = 'DIM_CAMPAIGNS'
s3_dimfn = 'cdim_10.csv.gz'
if 1: #read dim from s3
sql_stmt = """SELECT * FROM s3object S LIMIT 1"""
req_dim = s3.select_object_content(
Bucket = bucket_name,
Key = '%s/%s/%s' % (s3_prefix, s3_dimtn, s3_dimfn),
ExpressionType = 'SQL',
Expression = sql_stmt,
InputSerialization = { 'CompressionType':'GZIP',
'CSV': {'FileHeaderInfo': 'None'}},
OutputSerialization = {'CSV': {
'RecordDelimiter': '\n',
'FieldDelimiter': ','}},
#ScanRange = {'Start':0, 'End':40000 }
)
def read_dim(req, dim, colid):
leftover=''
for event in req['Payload']:
if 'Records' in event:
rr=event['Records']['Payload'].decode('utf-8')
for i, rec in enumerate(rr.split(os.linesep)):
if rec:
if leftover:
rec = leftover+rec
leftover = ''
row=rec.split(colsep)
if row:
if len(row) == DIM_COLCNT:
key=row[colid]
assert key not in dim
dim[key] = row
print(row)
else:
leftover = rec
read_dim(req_dim, dim,0)
pp(dim)
#e()
if 1:
FACT_COLCNT=4
fact={}
s3_prefix = 'tables'
s3_fact_tn = 'FACT_IMPRESSIONS'
s3_fact_fn = 'B_0.campaign_00.campaign.csv.gz'
dim_key_col_pos=0
fact_key_col_pos=1
part_pos_in_fact_fn = 1
key_pos_in_fact_fn = 2
s3r = boto3.resource('s3')
mybucket = s3r.Bucket(bucket_name)
bucket_prefix="%s/%s/" % (s3_prefix, s3_fact_tn)
objs = mybucket.objects.filter(
Prefix = bucket_prefix)
dim_cols_to_append = [2]
if 1:
s3_joined_fact_tn = 'FACT_IMPRESSIONS_JOINED'
def get_key_from_fn(fn):
return fn.split(".")[key_pos_in_fact_fn]
def get_part_from_fn(fn):
return fn.split(".")[part_pos_in_fact_fn]
async def producer(objs, queue,dim):
for obj in objs:
#print(obj.key)
path, filename = os.path.split(obj.key)
dim_key = get_key_from_fn(filename)
part_name = get_part_from_fn(filename)
if dim_key in dim:
#print(dim_key, filename)
await queue.put((path, filename, dim_key, part_name))
else:
if 0:
raise Exception('Campaign key "%s" does not exist in table %s' % (key,s3_fact_tn))
await queue.put(None)
async def compress_and_upload_to_s3(outq ,bid,cid, blob_s3_key, bucket):
if 0:
row = await outq.get()
outq.task_done()
print (7777777777777,blob_s3_key)
return
csize=1014*500
content_type='text/plain'
stream = io.BytesIO()
lid=1
part_info = {'Parts':[]}
next_chunk=''
async def get_chunk(outq):
out = b''
while outq.qsize():
payload = await outq.get()
outq.task_done()
if payload:
row = payload
if not row: break
out = out + b','.join([x.encode() for x in row])+ b'\n'
if len(out) >=csize:
break
else:
break
print('get_chunk', len(out), outq.qsize())
return out
async with aioboto3.client("s3") as s3:
mpu = await s3.create_multipart_upload(Bucket=bucket, Key=blob_s3_key)
chunk = await get_chunk(outq)
while chunk:
next_chunk = await get_chunk(outq)
if len(next_chunk)<csize:
print('merging',len(chunk), len(next_chunk) )
chunk = chunk+next_chunk;
next_chunk=''
with gzip.GzipFile(fileobj=stream, mode='wb') as gz:
gz.write(chunk)
stream.seek(0)
part = await s3.upload_part(Bucket=bucket, Key=blob_s3_key, PartNumber=lid,\
UploadId=mpu['UploadId'], Body=stream)
stream.seek(0)
stream.truncate()
part_info['Parts'].append(
{
'PartNumber': lid,
'ETag': part['ETag']
} )
lid +=1
chunk = next_chunk
#if not chunk: break
try:
await s3.complete_multipart_upload(Bucket=bucket, Key=blob_s3_key, UploadId=mpu['UploadId'],
MultipartUpload=part_info)
except:
raise Exception(bid)
pass
async def consumer(queue, dim):
readers=[]
qz=[]
while True:
row = await queue.get()
queue.task_done()
if row:
(s3_prefix, s3_fn, dim_key, part_name) = row
fact_queue = asyncio.Queue()
appended_q = asyncio.Queue()
readers.append(asyncio.create_task(read_fact_file(fact_queue, s3_prefix, s3_fn)))
data_to_append= [x for i, x in enumerate(dim[dim_key]) if i in dim_cols_to_append]
if 1:
readers.append(asyncio.create_task(append_dim_cols_to_fact(fact_queue, data_to_append,appended_q)))
if 1:
blob_s3_key= '%s/%s/%s/%s.%s.%s.joined_fact_and_dim.csv.gz' % ('tables',s3_joined_fact_tn, part_name, s3_joined_fact_tn,part_name,dim_key)
#print(blob_s3_key)
if 1:
readers.append(asyncio.create_task(compress_and_upload_to_s3(appended_q,\
part_name, dim_key, blob_s3_key, bucket_name\
)))
qz.append(fact_queue)
qz.append(appended_q)
else:
break
#await queue.join()
await asyncio.gather(*readers)
for q in qz:
await q.join()
async def append_dim_cols_to_fact(queue, append, appended_q):
readers=[]
while True:
row = await queue.get()
queue.task_done()
if row:
#print('merge----',row, append)
await appended_q.put(row +append)
else:
break
await appended_q.put(None)
async def read_fact_file(queue, s3_prefix, s3_fn):
if 1: #Fact partitioner
FACT_COLCNT=4
#fact={}
s3_prefix = 'tables'
s3_fact_tn = 'FACT_IMPRESSIONS'
sql_stmt = """SELECT * FROM s3object S LIMIT 2"""
req_fact = s3.select_object_content(
Bucket = bucket_name,
Key = fact_name,
ExpressionType = 'SQL',
Expression = sql_stmt,
InputSerialization = { 'CompressionType':'GZIP',
'CSV': {'FileHeaderInfo': 'None'}},
OutputSerialization = {'CSV': {
'RecordDelimiter': '\n',
'FieldDelimiter': ','}},
#ScanRange = {'Start':0, 'End':40000 }
)
leftover=''
for event in req_fact['Payload']:
if 'Records' in event:
rr=event['Records']['Payload'].decode('utf-8')
for i, rec in enumerate(rr.split(os.linesep)):
if rec:
if leftover:
rec = leftover+rec
leftover = ''
#pp(rec)
row=rec.split(colsep)
if row:
#print(row)
if len(row) == FACT_COLCNT:
key=row[fact_key_col_pos]
#assert key not in fact
#fact[key] = row
#print(key)
await queue.put(row)
else:
leftover = rec
await queue.put(None)
#print(1234, s3_prefix, s3_fn)
async def get_table_partition_files():
queue = asyncio.Queue()
cid_queue = asyncio.Queue()
coro= {}
await asyncio.gather( producer(objs, queue,dim), consumer(queue, dim))
await queue.join()
#await cid_queue.join()
if 1:
asyncio.run(get_table_partition_files())
elapsed = time.perf_counter() - s
print(f"{__file__} executed in {elapsed:0.2f} seconds.")
e()
FACT_COLCNT=4
fact={}
s3_prefix = 'tables'
s3_fact_tn = 'FACT_IMPRESSIONS_APPENDED'
s3_fact_fn = 'B_0.campaign_00.campaign.csv.gz'
sql_stmt = """SELECT * FROM s3object S"""
if 0: #Fact partitioner
req_fact = s3.select_object_content(
Bucket = bucket_name,
Key = fact_name,
ExpressionType = 'SQL',
Expression = sql_stmt,
InputSerialization = { 'CompressionType':'GZIP',
'CSV': {'FileHeaderInfo': 'None'}},
OutputSerialization = {'CSV': {
'RecordDelimiter': '\n',
'FieldDelimiter': ','}},
#ScanRange = {'Start':0, 'End':40000 }
)
async def split_fact_to_queues_by_cid(queue, coro, cid_queue):
while True:
row = await queue.get()
queue.task_done()
if row:
cid=row[1]
part_id=next(get_pid(cid))
if 0:
if part_id not in buckets:
buckets[part_id] = {}
if cid not in buckets[part_id]:
buckets[part_id][cid] = []
buckets[part_id][cid].append(row)
if 1: #part_id == 'B_4':
if part_id not in coro:
coro[part_id]={}
if cid not in coro[part_id]:
coro[part_id][cid]= outq = asyncio.Queue()
await cid_queue.put((part_id, cid))
outq = coro[part_id][cid]
pp(row)
await outq.put((part_id,cid, row))
else:
break
await outq.put(None)
await cid_queue.put(None)
async def append_upload_gzipped(coro,bid,cid, blob_s3_key, bucket, append):
outq = coro.get(bid).get(cid)
assert outq
csize=1014*500
content_type='text/plain'
stream = io.BytesIO()
lid=1
part_info = {'Parts':[]}
next_chunk=''
async def get_chunk(outq):
out = b''
while outq.qsize():
payload = await outq.get()
outq.task_done()
if payload:
bid, cid, row = payload
if not row: break
out = out + b','.join([x.encode() for x in (row+append)])+ b'\n'
if len(out) >=csize:
break
else:
break
print('get_chunk', len(out), outq.qsize())
return out
async with aioboto3.client("s3") as s3:
mpu = await s3.create_multipart_upload(Bucket=bucket, Key=blob_s3_key)
chunk = await get_chunk(outq)
while chunk:
next_chunk = await get_chunk(outq)
if len(next_chunk)<csize:
print('merging',len(chunk), len(next_chunk) )
chunk = chunk+next_chunk;
next_chunk=''
with gzip.GzipFile(fileobj=stream, mode='wb') as gz:
gz.write(chunk)
stream.seek(0)
part = await s3.upload_part(Bucket=bucket, Key=blob_s3_key, PartNumber=lid,\
UploadId=mpu['UploadId'], Body=stream)
stream.seek(0)
stream.truncate()
part_info['Parts'].append(
{
'PartNumber': lid,
'ETag': part['ETag']
} )
lid +=1
chunk = next_chunk
#if not chunk: break
try:
await s3.complete_multipart_upload(Bucket=bucket, Key=blob_s3_key, UploadId=mpu['UploadId'],
MultipartUpload=part_info)
except:
raise Exception(bid)
pass
async def append_and_upload_gzipped(coro,cid_queue,bucket, prefix, dim):
uploaders =[]
while True:
payload = await cid_queue.get()
cid_queue.task_done()
if payload:
pid, cid = payload
print('got pid, cid: ', pid, cid)
if not pid: break
pp(dim[cid])
dim_cols_to_append = [2]
data_to_append= [x for i, x in enumerate(dim[cid]) if i in dim_cols_to_append]
if 1:
uploaders.append(asyncio.create_task(append_upload_gzipped(coro,pid,cid,'%s/%s/%s.%s.%s.campaign.csv.gz' % (prefix, pid, s3_fact_tn,pid, cid), bucket, append=data_to_append)))
else:
break
if uploaders:
await asyncio.gather(*uploaders)
print('---- done uploading')
async def append_dim_cols_to_fact():
queue = asyncio.Queue()
cid_queue = asyncio.Queue()
coro= {}
await asyncio.gather( read_fact_from_s3(req_fact,queue), split_fact_to_queues_by_cid(queue, coro,cid_queue),\
append_and_upload_gzipped(coro,cid_queue, bucket_name, prefix='%s/%s' % (s3_prefix,s3_fact_tn), dim=dim))
await queue.join()
await cid_queue.join()
if 1:
asyncio.run(append_dim_cols_to_fact())
e()
if 0: #read fact from s3
req_fact = s3.select_object_content(
Bucket = bucket_name,
Key = '%s/%s/%s' % (s3_prefix, s3_fact_tn, s3_fact_fn),
ExpressionType = 'SQL',
Expression = sql_stmt,
InputSerialization = { 'CompressionType':'GZIP',
'CSV': {'FileHeaderInfo': 'None'}},
OutputSerialization = {'CSV': {
'RecordDelimiter': '\n',
'FieldDelimiter': ','}},
#ScanRange = {'Start':0, 'End':40000 }
)
def read_fact(req, fact, colid):
leftover=''
for event in req['Payload']:
if 'Records' in event:
rr=event['Records']['Payload'].decode('utf-8')
for i, rec in enumerate(rr.split(os.linesep)):
if rec:
if leftover:
rec = leftover+rec
leftover = ''
pp(rec)
row=rec.split(colsep)
if row:
#print(row)
if len(row) == FACT_COLCNT:
key=row[colid]
#assert key not in fact
fact[key] = row
print(row)
else:
leftover = rec
read_fact(req_fact, fact,1)
pp(fact)
if 0:
sql_stmt = """SELECT * FROM s3object S"""
req_fact = s3.select_object_content(
Bucket = bucket_name,
Key = fact_name,
ExpressionType = 'SQL',
Expression = sql_stmt,
InputSerialization = { 'CompressionType':'GZIP',
'CSV': {'FileHeaderInfo': 'None'}},
OutputSerialization = {'CSV': {
'RecordDelimiter': '\n',
'FieldDelimiter': ','}},
#ScanRange = {'Start':0, 'End':40000 }
)
async def upload_fact():
queue = asyncio.Queue()
cid_queue = asyncio.Queue()
coro= {}
await asyncio.gather( producer(req_fact,queue), consumer(queue, coro,cid_queue),\
upload_gzipped(coro,cid_queue, bucket_name))
await queue.join()
await cid_queue.join()
elapsed = time.perf_counter() - s
print(f"{__file__} executed in {elapsed:0.2f} seconds.")