-
Notifications
You must be signed in to change notification settings - Fork 10
/
sqoop.py
1305 lines (1177 loc) · 45.1 KB
/
sqoop.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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Wikimedia Analytics Refinery sqoop python helpers
"""
import sys
import logging
import re
from subprocess import check_call, DEVNULL
from refinery.hdfs import Hdfs
from refinery.util import is_yarn_application_running, get_dbnames_from_mw_config
logger = logging.getLogger()
class SqoopConfig:
def __init__(self, yarn_job_name_prefix,
user, password_file, jdbc_string,
num_mappers, fetch_size, output_format, tmp_base_path,
table_path_template, dbname, table, queries,
target_jar_dir, jar_file, yarn_queue, driver_class,
current_try, dry_run):
self.yarn_job_name_prefix = yarn_job_name_prefix
self.user = user
self.password_file = password_file
self.jdbc_string = jdbc_string
self.num_mappers_base = num_mappers
self.fetch_size = fetch_size
self.output_format = output_format
self.tmp_base_path = tmp_base_path
self.table_path_template = table_path_template
self.dbname = dbname
self.table = table
query_info = queries[table]
self.query = query_info.get('query')
self.boundary_query = query_info.get('boundary-query')
self.split_by = query_info.get('split-by')
self.map_types = query_info.get('map-types')
# If sqoopable_dbnames is not defined for this table, it means there is no restriction
# on dbnames for that table, meaning all dbnames are sqoopable.
self.is_sqoopable = (('sqoopable_dbnames' not in query_info) or (dbname in query_info['sqoopable_dbnames']))
self.num_mappers_weighted = max(int(num_mappers * query_info['mappers-weight']), 1)
self.target_jar_dir = target_jar_dir
self.jar_file = jar_file
self.yarn_queue = yarn_queue
self.driver_class = driver_class
self.current_try = current_try
self.dry_run = dry_run
def __str__(self):
return self.dbname + ':' + self.table
def sqoop_wiki(config):
"""
Run a single sqoop import (1 database, 1 table)
Parameters
config: SqoopConfig object filed in with needed parameters
Returns
None if the sqoop worked
The config object with updated try number if the sqoop errored or failed in any way
"""
if not config.is_sqoopable:
logger.info('SKIPPING: Table {} is not to be sqooped for database {}'.format(config.table, config.dbname))
return None
full_table = '.'.join([config.dbname, config.table])
log_message = '{} (try {})'.format(full_table, config.current_try)
logger.info('STARTING: {}'.format(log_message))
try:
query = config.query
command = 'import'
if config.target_jar_dir:
query = query + ' and 1=0'
command = 'codegen'
sqoop_arguments = [
'sqoop',
command,
'-D' , "mapred.job.name='{}-{}'".format(
config.yarn_job_name_prefix, full_table),
'-D' , "mapreduce.job.queuename={}".format(config.yarn_queue),
'--username' , config.user,
'--password-file' , config.password_file,
'--connect' , config.jdbc_string,
'--query' , query,
]
if config.driver_class:
sqoop_arguments += ['--driver' , config.driver_class]
if config.target_jar_dir:
sqoop_arguments += [
'--class-name' , config.table,
'--outdir' , config.target_jar_dir,
'--bindir' , config.target_jar_dir,
]
else:
# We don't use the hive-partition folder style since
# it fails when sqooping as parquet outpout format.
# We instead sqoop into a temporary folder, and then
# move it to the correct place
target_directory = (config.table_path_template + '/wiki_db={db}').format(
table=config.table, db=config.dbname)
tmp_target_directory = table_path_to_tmp_path(target_directory, config.tmp_base_path)
sqoop_arguments += [
'--target-dir' , tmp_target_directory,
'--num-mappers' , str(config.num_mappers_weighted),
'--as-{}file'.format(config.output_format),
]
if config.fetch_size:
sqoop_arguments += ['--fetch-size', str(config.fetch_size)]
if config.num_mappers_weighted > 1:
if config.boundary_query:
sqoop_arguments += ['--boundary-query', config.boundary_query]
# if num_mappers_weighted <= 1, split_by can be None, otherwise it should be set
sqoop_arguments += ['--split-by', config.split_by]
if config.jar_file:
sqoop_arguments += [
'--class-name' , config.table,
'--jar-file' , config.jar_file,
]
if config.map_types:
sqoop_arguments += [
'--map-column-java' , config.map_types
]
# Force deletion of possibly existing dir in case of retry
if config.current_try > 1:
sqoop_arguments += [
'--delete-target-dir'
]
# This step is needed particularly in case of retry - tmp_target_directory
# Folder is created at first try, so we need to delete it (if it exists)
# before trying again.
if not config.target_jar_dir:
logger.info('Deleting temporary target directory {} if it exists'.format(tmp_target_directory))
if not config.dry_run:
try:
Hdfs.rm(tmp_target_directory)
except(Exception):
pass
logger.debug('Sqooping with: {}'.format(sqoop_arguments))
logger.debug('You can copy the parameters above and execute the sqoop command manually')
# Ignore sqoop output because it's in Yarn and grabbing output is way complicated
if not config.dry_run:
check_call(sqoop_arguments, stdout=DEVNULL, stderr=DEVNULL)
if not config.target_jar_dir:
logger.info('Moving sqooped folder from {} to {}'.format(tmp_target_directory, target_directory))
if not config.dry_run:
Hdfs.mv(tmp_target_directory, target_directory, inParent=False)
logger.info('FINISHED: {}'.format(log_message))
return None
except(Exception):
logger.exception('ERROR: {}'.format(log_message))
config.current_try += 1
return config
def make_timestamp_clause(field_name, from_timestamp, to_timestamp):
"""
Returns a valid SQL boolean clause using the timetamp_field as field-name
and from_timestamp and to_timestamp as limit values if defined
"""
timestamp_clause = ''
if from_timestamp:
timestamp_clause += ''' and {n} >= '{f}' '''.format(n=field_name, f=from_timestamp)
if to_timestamp:
timestamp_clause += ''' and {n} < '{t}' '''.format(n=field_name, t=to_timestamp)
return timestamp_clause
def validate_tables_and_get_queries(filter_tables, from_timestamp, to_timestamp):
"""
Returns a dictionary of mediawiki table names to the correct sqoop
query to execute for import.
Notes
- convert(... using utf8mb4) is used to decode varbinary fields into strings
- map-types is used to handle some databases having booleans in
tinyint(1) and others in tinyint(3,4) (newer databases like wikivoyage)
- from_timestamp and to_timestamp are optional
Parameters
filter_tables: list of tables to return queries for, None for all tables
from_timestamp: imported timestamps must be newer than this, (YYYYMMDDHHmmss)
to_timestamp: timestamps must be *strictly* older than this, (YYYYMMDDHHmmss)
Returns
An object of the form:
{
'table-name': {
'query' : <<the sql query to run on mysql>>,
'boundary-query': <<the sql query to get min and max values for split-by>>,
'split-by' : <<the column to use to parallelize imports>>,
'map-types' : <<any info to pass as --map-column-java>>,
},
...
}
"""
queries = {}
############################################################################
# Tables sqooped from labs (usually)
############################################################################
queries['archive'] = {
'query': '''
select ar_id,
ar_namespace,
convert(ar_title using utf8mb4) ar_title,
null ar_text,
null ar_comment,
null ar_user,
null ar_user_text,
convert(ar_timestamp using utf8mb4) ar_timestamp,
ar_minor_edit,
null ar_flags,
ar_rev_id,
null ar_text_id,
ar_deleted,
ar_len,
ar_page_id,
ar_parent_id,
convert(ar_sha1 using utf8mb4) ar_sha1,
null ar_content_model,
null ar_content_format,
ar_actor,
ar_comment_id
from archive
where $CONDITIONS
{ts_clause}
'''.format(ts_clause=make_timestamp_clause('ar_timestamp', from_timestamp, to_timestamp)),
'map-types': '"{}"'.format(','.join([
'ar_actor=Long',
'ar_comment=String',
'ar_comment_id=Long',
'ar_content_format=String',
'ar_content_model=String',
'ar_deleted=Integer',
'ar_flags=String',
'ar_minor_edit=Boolean',
'ar_text=String',
'ar_user=Long',
'ar_user_text=String',
'ar_text_id=Long',
])),
'boundary-query': '''
SELECT MIN(ar_id),
MAX(ar_id)
FROM archive
WHERE TRUE
{ts_clause}
'''.format(ts_clause=make_timestamp_clause('ar_timestamp', from_timestamp, to_timestamp)),
'split-by': 'ar_id',
'mappers-weight': 0.5,
}
queries['category'] = {
'query': '''
select cat_id,
convert(cat_title using utf8mb4) cat_title,
cat_pages,
cat_subcats,
cat_files
from category
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'cat_id=Long',
'cat_title=String',
'cat_pages=Integer',
'cat_subcats=Integer',
'cat_files=Integer',
])),
'boundary-query': 'SELECT MIN(cat_id), MAX(cat_id) FROM category',
'split-by': 'cat_id',
'mappers-weight': 0.25,
}
queries['categorylinks'] = {
'query': '''
select cl_from,
convert(cl_to using utf8mb4) cl_to,
convert(cl_sortkey using utf8mb4) cl_sortkey,
convert(cl_sortkey_prefix using utf8mb4) cl_sortkey_prefix,
convert(cl_timestamp using utf8mb4) cl_timestamp,
convert(cl_collation using utf8mb4) cl_collation,
convert(cl_type using utf8mb4) cl_type
from categorylinks
where $CONDITIONS
{ts_clause}
'''.format(ts_clause=make_timestamp_clause('cl_timestamp', from_timestamp, to_timestamp)),
'map-types': '"{}"'.format(','.join([
'cl_from=Long',
'cl_to=String',
'cl_sortkey=String',
'cl_sortkey_prefix=String',
'cl_timestamp=String',
'cl_collation=String',
'cl_type=String',
])),
'boundary-query': '''
SELECT MIN(cl_from),
MAX(cl_from)
FROM categorylinks
WHERE TRUE
{ts_clause}
'''.format(ts_clause=make_timestamp_clause('cl_timestamp', from_timestamp, to_timestamp)),
'split-by': 'cl_from',
'mappers-weight': 1.0,
}
queries['change_tag'] = {
'query': '''
select ct_id,
ct_log_id,
ct_rev_id,
ct_tag_id,
convert(ct_params using utf8mb4) ct_params
from change_tag
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'ct_id=Long',
'ct_log_id=Long',
'ct_rev_id=Long',
'ct_tag_id=Long',
])),
'boundary-query': 'SELECT MIN(ct_id), MAX(ct_id) FROM change_tag',
'split-by': 'ct_id',
'mappers-weight': 0.5,
}
queries['change_tag_def'] = {
'query': '''
select ctd_id,
convert(ctd_name using utf8mb4) ctd_name,
ctd_user_defined,
ctd_count
from change_tag_def
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'ctd_count=Long',
'ctd_id=Long',
'ctd_user_defined=Boolean',
])),
'boundary-query': 'SELECT MIN(ctd_id), MAX(ctd_id) FROM change_tag_def',
'split-by': 'ctd_id',
'mappers-weight': 0.0,
}
queries['content'] = {
'query': '''
select content_id,
content_size,
convert(content_sha1 using utf8mb4) content_sha1,
content_model,
convert(content_address using utf8mb4) content_address
from content
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'content_id=Long',
'content_size=Integer',
'content_model=Integer',
])),
'boundary-query': 'SELECT MIN(content_id), MAX(content_id) FROM content',
'split-by': 'content_id',
'mappers-weight': 1.0,
# Sqooping content table for commonswiki and etwiki only for now
# https://phabricator.wikimedia.org/T238878
# Note: etwiki is needed as we build ORM jar from it
'sqoopable_dbnames': ['commonswiki', 'etwiki']
}
queries['content_models'] = {
'query': '''
select model_id,
convert(model_name using utf8mb4) model_name
from content_models
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'model_id=Integer',
])),
'boundary-query': 'SELECT MIN(model_id), MAX(model_id) FROM content_models',
'split-by': 'model_id',
'mappers-weight': 0.0,
}
queries['externallinks'] = {
'query': '''
select el_id,
el_from,
convert(el_to_domain_index using utf8mb4) el_to_domain_index,
convert(el_to_path using utf8mb4) el_to_path
from externallinks
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'el_id=Long',
'el_from=Long',
'el_to_domain_index=String',
'el_to_path=String'
])),
'boundary-query': 'SELECT MIN(el_id), MAX(el_id) FROM externallinks',
'split-by': 'el_id',
'mappers-weight': 1.0,
}
queries['image'] = {
'query': '''
select convert(img_name using utf8mb4) img_name,
img_size,
img_width,
img_height,
-- Field not sqooped as it can contain more than 10Mb of data
-- leading to job failure (commonswiki database only)
-- convert(img_metadata using utf8mb4) img_metadata,
img_bits,
convert(img_media_type using utf8mb4) img_media_type,
convert(img_major_mime using utf8mb4) img_major_mime,
convert(img_minor_mime using utf8mb4) img_minor_mime,
img_description_id,
img_actor,
convert(img_timestamp using utf8mb4) img_timestamp,
convert(img_sha1 using utf8mb4) img_sha1
from image
where $CONDITIONS
{ts_clause}
'''.format(ts_clause=make_timestamp_clause('img_timestamp', from_timestamp, to_timestamp)),
'map-types': '"{}"'.format(','.join([
'img_name=String',
'img_size=Long',
'img_width=Integer',
'img_height=Integer',
#'img_metadata=String',
'img_bits=Integer',
'img_media_type=String',
'img_major_mime=String',
'img_minor_mime=String',
'img_description_id=Long',
'img_actor=Long',
'img_timestamp=String',
'img_sha1=String',
])),
# Forcing single mapper to prevent having to split-by as table's primary-key
# is a varchar (complicated to split). Data-size is not big even for commonswiki
# so single-mapper does the job.
'mappers-weight': 0.0,
}
queries['imagelinks'] = {
'query': '''
select il_from,
convert(il_to using utf8mb4) il_to,
il_from_namespace
from imagelinks
where $CONDITIONS
''',
'boundary-query': 'SELECT MIN(il_from), MAX(il_from) FROM imagelinks',
'split-by': 'il_from',
'mappers-weight': 0.25,
}
queries['ipblocks'] = {
'query': '''
select ipb_id,
convert(ipb_address using utf8mb4) ipb_address,
ipb_user,
null ipb_by,
null ipb_by_text,
null ipb_reason,
convert(ipb_timestamp using utf8mb4) ipb_timestamp,
ipb_auto,
ipb_anon_only,
ipb_create_account,
ipb_enable_autoblock,
convert(ipb_expiry using utf8mb4) ipb_expiry,
convert(ipb_range_start using utf8mb4) ipb_range_start,
convert(ipb_range_end using utf8mb4) ipb_range_end,
ipb_deleted,
ipb_block_email,
ipb_allow_usertalk,
ipb_parent_block_id,
ipb_by_actor,
ipb_reason_id
from ipblocks
where $CONDITIONS
{ts_clause}
'''.format(ts_clause=make_timestamp_clause('ipb_timestamp', from_timestamp, to_timestamp)),
'map-types': '"{}"'.format(','.join([
'ipb_allow_usertalk=Boolean',
'ipb_anon_only=Boolean',
'ipb_auto=Boolean',
'ipb_block_email=Boolean',
'ipb_by=Long',
'ipb_by_actor=Long',
'ipb_by_text=String',
'ipb_create_account=Boolean',
'ipb_deleted=Boolean',
'ipb_enable_autoblock=Boolean',
'ipb_reason=String',
'ipb_reason_id=Long',
])),
'boundary-query': '''
SELECT MIN(ipb_id),
MAX(ipb_id)
FROM ipblocks
WHERE TRUE
{ts_clause}
'''.format(ts_clause=make_timestamp_clause('ipb_timestamp', from_timestamp, to_timestamp)),
'split-by': 'ipb_id',
'mappers-weight': 0.0,
}
queries['ipblocks_restrictions'] = {
'query': '''
select ir_ipb_id,
ir_type,
ir_value
from ipblocks_restrictions
where $CONDITIONS
''',
'boundary-query': 'SELECT MIN(ir_ipb_id), MAX(ir_ipb_id) FROM ipblocks_restrictions',
'split-by': 'ir_ipb_id',
'mappers-weight': 0.0,
}
queries['iwlinks'] = {
'query': '''
select iwl_from,
convert(iwl_prefix using utf8mb4) iwl_prefix,
convert(iwl_title using utf8mb4) iwl_title
from iwlinks
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'iwl_from=Long',
'iwl_prefix=String',
'iwl_title=String',
])),
'boundary-query': 'SELECT MIN(iwl_from), MAX(iwl_from) FROM iwlinks',
'split-by': 'iwl_from',
'mappers-weight': 0.5,
}
queries['langlinks'] = {
'query': '''
select ll_from,
convert(ll_lang using utf8mb4) ll_lang,
convert(ll_title using utf8mb4) ll_title
from langlinks
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'll_from=Long',
'll_lang=String',
'll_title=String',
])),
'boundary-query': 'SELECT MIN(ll_from), MAX(ll_from) FROM langlinks',
'split-by': 'll_from',
'mappers-weight': 0.5,
}
queries['logging'] = {
'query': '''
select log_id,
convert(log_type using utf8mb4) log_type,
convert(log_action using utf8mb4) log_action,
convert(log_timestamp using utf8mb4) log_timestamp,
null log_user,
log_namespace,
convert(log_title using utf8mb4) log_title,
null log_comment,
convert(log_params using utf8mb4) log_params,
log_deleted,
null log_user_text,
log_page,
log_actor,
log_comment_id
from logging
where $CONDITIONS
{ts_clause}
'''.format(ts_clause=make_timestamp_clause('log_timestamp', from_timestamp, to_timestamp)),
'map-types': '"{}"'.format(','.join([
'log_actor=Long',
'log_comment=String',
'log_comment_id=Long',
'log_user=Long',
'log_user_text=String',
])),
'boundary-query': '''
SELECT MIN(log_id),
MAX(log_id)
FROM logging
WHERE TRUE
{ts_clause}
'''.format(ts_clause=make_timestamp_clause('log_timestamp', from_timestamp, to_timestamp)),
'split-by': 'log_id',
'mappers-weight': 1.0,
}
queries['page'] = {
'query': '''
select page_id,
page_namespace,
convert(page_title using utf8mb4) page_title,
page_is_redirect,
page_is_new,
page_random,
convert(page_touched using utf8mb4) page_touched,
convert(page_links_updated using utf8mb4) page_links_updated,
page_latest,
page_len,
convert(page_content_model using utf8mb4) page_content_model
from page
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'page_is_new=Boolean',
'page_is_redirect=Boolean',
])),
'boundary-query': 'SELECT MIN(page_id), MAX(page_id) FROM page',
'split-by': 'page_id',
'mappers-weight': 0.5,
}
queries['pagelinks'] = {
'query': '''
select pl_from,
pl_from_namespace,
pl_target_id
from pagelinks
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'pl_from=Long',
'pl_from_namespace=Integer',
'pl_target_id=Long',
])),
'boundary-query': 'SELECT MIN(pl_from), MAX(pl_from) FROM pagelinks',
'split-by': 'pl_from',
'mappers-weight': 1.0,
}
queries['page_props'] = {
'query': '''
select pp_page,
convert(pp_propname using utf8mb4) pp_propname,
convert(pp_value using utf8mb4) pp_value,
pp_sortkey
from page_props
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'pp_page=Long',
'pp_propname=String',
'pp_value=String',
'pp_sortkey=Float',
])),
'boundary-query': 'SELECT MIN(pp_page), MAX(pp_page) FROM page_props',
'split-by': 'pp_page',
'mappers-weight': 0.125,
}
queries['page_restrictions'] = {
'query': '''
select pr_id,
pr_page,
convert(pr_type using utf8mb4) pr_type,
convert(pr_level using utf8mb4) pr_level,
pr_cascade,
convert(pr_expiry using utf8mb4) pr_expiry
from page_restrictions
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'pr_id=Long',
'pr_page=Long',
'pr_type=String',
'pr_level=String',
'pr_cascade=Integer',
'pr_expiry=String',
])),
'boundary-query': 'SELECT MIN(pr_id), MAX(pr_id) FROM page_restrictions',
'split-by': 'pr_id',
'mappers-weight': 0.125,
}
queries['redirect'] = {
'query': '''
select rd_from,
rd_namespace,
convert(rd_title using utf8mb4) rd_title,
convert(rd_interwiki using utf8mb4) rd_interwiki,
convert(rd_fragment using utf8mb4) rd_fragment
from redirect
where $CONDITIONS
''',
'boundary-query': 'SELECT MIN(rd_from), MAX(rd_from) FROM redirect',
'split-by': 'rd_from',
'mappers-weight': 0.125,
}
queries['revision'] = {
'query': '''
select rev_id,
rev_page,
null rev_text_id,
null rev_comment,
null rev_user,
null rev_user_text,
convert(rev_timestamp using utf8mb4) rev_timestamp,
rev_minor_edit,
rev_deleted,
rev_len,
rev_parent_id,
convert(rev_sha1 using utf8mb4) rev_sha1,
null rev_content_model,
null rev_content_format,
rev_actor,
rev_comment_id
from revision
where $CONDITIONS
{ts_clause}
'''.format(ts_clause=make_timestamp_clause('rev_timestamp', from_timestamp, to_timestamp)),
'map-types': '"{}"'.format(','.join([
'rev_actor=Long',
'rev_comment=String',
'rev_comment_id=Long',
'rev_deleted=Integer',
'rev_minor_edit=Boolean',
'rev_user=Long',
'rev_user_text=String',
'rev_text_id=Long',
'rev_content_model=String',
'rev_content_format=String',
])),
'boundary-query': '''
SELECT MIN(rev_id),
MAX(rev_id)
FROM revision
WHERE TRUE
{ts_clause}
'''.format(ts_clause=make_timestamp_clause('rev_timestamp', from_timestamp, to_timestamp)),
'split-by': 'rev_id',
'mappers-weight': 1.0,
}
queries['slots'] = {
'query': '''
select slot_revision_id,
slot_role_id,
slot_content_id,
slot_origin
from slots
where $CONDITIONS
''',
'boundary-query': 'SELECT MIN(slot_revision_id), MAX(slot_revision_id) FROM slots',
'split-by': 'slot_revision_id',
'mappers-weight': 1.0,
}
queries['slot_roles'] = {
'query': '''
select role_id,
convert(role_name using utf8mb4) role_name
from slot_roles
where $CONDITIONS
''',
'boundary-query': 'SELECT MIN(role_id), MAX(role_id) FROM slot_roles',
'split-by': 'role_id',
'mappers-weight': 0.0,
}
queries['templatelinks'] = {
'query': '''
select tl_from,
tl_from_namespace,
null as tl_namespace,
null as tl_title,
tl_target_id
from templatelinks
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'tl_from=Long',
'tl_from_namespace=Integer',
'tl_namespace=Integer',
'tl_title=String',
'tl_target_id=Long',
])),
'boundary-query': 'SELECT MIN(tl_from), MAX(tl_from) FROM templatelinks',
'split-by': 'tl_from',
'mappers-weight': 1.0,
}
queries['user'] = {
'query': '''
select user_id,
convert(user_name using utf8mb4) user_name,
user_name user_name_binary,
convert(user_real_name using utf8mb4) user_real_name,
convert(user_email using utf8mb4) user_email,
convert(user_touched using utf8mb4) user_touched,
convert(user_registration using utf8mb4) user_registration,
user_editcount,
convert(user_password_expires using utf8mb4) user_password_expires
from user
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'user_id=Long',
'user_editcount=Long',
])),
'boundary-query': 'SELECT MIN(user_id), MAX(user_id) FROM user',
'split-by': 'user_id',
'mappers-weight': 0.5,
}
queries['user_groups'] = {
'query': '''
select ug_user,
convert(ug_group using utf8mb4) ug_group
from user_groups
where $CONDITIONS
''',
'boundary-query': 'SELECT MIN(ug_user), MAX(ug_user) FROM user_groups',
'split-by': 'ug_user',
'mappers-weight': 0.0,
}
queries['user_properties'] = {
'query': '''
select up_user,
convert(up_property using utf8mb4) up_property,
convert(up_value using utf8mb4) up_value
from user_properties
where $CONDITIONS
''',
'map-types': '"{}"'.format(','.join([
'up_user=Long',
'up_property=String',
'up_value=String',
])),
'boundary-query': 'SELECT MIN(up_user), MAX(up_user) FROM user_properties',
'split-by': 'up_user',
'mappers-weight': 0.125,
}
wbc_entity_usage_sqoopable_dbs = get_dbnames_from_mw_config(['wikidataclient.dblist'])
# Manually removed table (empty in prod, not replicated in labs)
wbc_entity_usage_sqoopable_dbs.discard('sewikimedia')
queries['wbc_entity_usage'] = {
'query': '''
select eu_row_id,
convert(eu_entity_id using utf8mb4) eu_entity_id,
convert(eu_aspect using utf8mb4) eu_aspect,
eu_page_id
from wbc_entity_usage
where $CONDITIONS
''',
'boundary-query': 'SELECT MIN(eu_row_id), MAX(eu_row_id) FROM wbc_entity_usage',
'split-by': 'eu_row_id',
'map-types': '"{}"'.format(','.join([
'eu_row_id=Long',
'eu_entity_id=String',
'eu_aspect=String',
'eu_page_id=Long'
])),
'mappers-weight': 1.0,
'sqoopable_dbnames': wbc_entity_usage_sqoopable_dbs,
}
############################################################################
# Tables sqooped from production replica
# cu_changes and watchlist are not available in labs
# actor, comment and linktarget are too slow due to expensive join at sanitization
############################################################################
# documented at https://www.mediawiki.org/wiki/Extension:CheckUser/cu_changes_table
queries['cu_changes'] = {
'query': '''
select cuc_id,
cuc_namespace,
cuc_title,
coalesce(actor_user, 0) cuc_user,
convert(actor_name using utf8mb4) cuc_user_text,
cuc_actor,
cuc_actiontext,
convert(comment_text using utf8mb4) cuc_comment,
cuc_comment_id,
cuc_minor,
cuc_page_id,
cuc_this_oldid,
cuc_last_oldid,
cuc_type,
convert(cuc_timestamp using utf8mb4) cuc_timestamp,
convert(cuc_ip using utf8mb4) cuc_ip,
convert(cuc_agent using utf8mb4) cuc_agent
from cu_changes
inner join
actor on actor_id = cuc_actor
inner join
comment on comment_id = cuc_comment_id
where $CONDITIONS
{ts_clause}
'''.format(ts_clause=make_timestamp_clause('cuc_timestamp', from_timestamp, to_timestamp)),
'map-types': '"{}"'.format(','.join([
'cuc_id=Long',
'cuc_namespace=Integer',
'cuc_title=String',
'cuc_user=Long',
'cuc_user_text=String',
'cuc_actor=Long',
'cuc_actiontext=String',
'cuc_comment=String',
'cuc_comment_id=Long',
'cuc_minor=Boolean',
'cuc_page_id=Long',
'cuc_this_oldid=Long',
'cuc_last_oldid=Long',
'cuc_type=Integer',
'cuc_timestamp=String',
'cuc_ip=String',
'cuc_agent=String',
])),
'boundary-query': '''
SELECT MIN(cuc_id),
MAX(cuc_id)
FROM cu_changes
WHERE TRUE
{ts_clause}
'''.format(ts_clause=make_timestamp_clause('cuc_timestamp', from_timestamp, to_timestamp)),
'split-by': 'cuc_id',
'mappers-weight': 0.5,
}