-
Notifications
You must be signed in to change notification settings - Fork 10
/
load_cassandra_commons_top_editors_monthly.hql
162 lines (153 loc) · 4.97 KB
/
load_cassandra_commons_top_editors_monthly.hql
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
-- Load commons.top_editors_monthly dataset to cassandra.
--
-- Parameters:
-- destination_table -- Cassandra table to write query output.
-- source_table -- Fully qualified iceberg table to compute from.
-- year_month -- YYYY-MM month of partition to compute from.
-- coalesce_partitions -- number of partitions for destination data.
-- Usage:
-- spark3-submit \
-- --master yarn \
-- --conf spark.sql.catalog.aqs=com.datastax.spark.connector.datasource.CassandraCatalog \
-- --conf spark.sql.catalog.aqs.spark.cassandra.connection.host=cassandra-dev2001-a.codfw.wmnet:9042 \
-- --conf spark.sql.catalog.aqs.spark.cassandra.auth.username=aqsloader \
-- --conf spark.sql.catalog.aqs.spark.cassandra.auth.password=cassandra \
-- --conf spark.sql.catalog.aqs.spark.cassandra.output.batch.size.rows=1024 \
-- --jars hdfs:///wmf/cache/artifacts/airflow/analytics/spark-cassandra-connector-assembly-3.2.0-WMF-1.jar,hdfs:///wmf/cache/artifacts/airflow/analytics/refinery-job-0.2.17-shaded.jar \
-- --conf spark.dynamicAllocation.maxExecutors=128 \
-- --conf spark.yarn.maxAppAttempts=1 \
-- --conf spark.executor.memoryOverhead=3072 \
-- --executor-memory 8G \
-- --executor-cores 2 \
-- --driver-memory 4G \
-- --driver-cores 1 \
-- --class org.apache.spark.sql.hive.thriftserver.WMFSparkSQLCLIDriver \
-- --deploy-mode client \
-- hdfs:///wmf/cache/artifacts/airflow/analytics/wmf-sparksqlclidriver-1.0.0.jar \
-- -f load_cassandra_commons_top_editors_monthly.hql \
-- -d destination_table=aqs.commons.top_editors_monthly \
-- -d source_table=wmf_contributors.commons_edits \
-- -d coalesce_partitions=3 \
-- -d year_month=2023-11
WITH date_filtered_source_table AS (
SELECT *
FROM ${source_table}
WHERE dt >= TO_TIMESTAMP('${year_month}')
AND dt < TO_TIMESTAMP('${year_month}') + INTERVAL 1 MONTH
),
total_edits_per_editor AS (
SELECT
user_name,
edit_type,
categories,
primary_categories,
COUNT(1) as edit_count
FROM date_filtered_source_table
GROUP BY
user_name,
edit_type,
categories,
primary_categories
),
exploded_categories AS (
SELECT
user_name,
edit_type,
category,
SUM(edit_count) as edit_count
FROM (
SELECT
user_name,
edit_type,
EXPLODE(categories) AS category,
edit_count
FROM total_edits_per_editor)
GROUP BY CUBE(user_name, edit_type, category)
-- remove 'all-categories' and 'all-user-names' nulls as they are not aggregatable
HAVING user_name IS NOT NULL AND category IS NOT NULL
),
exploded_primary_categories AS (
SELECT
user_name,
edit_type,
category,
SUM(edit_count) as edit_count
FROM (
SELECT
user_name,
edit_type,
EXPLODE(primary_categories) AS category,
edit_count
FROM total_edits_per_editor)
GROUP BY CUBE(user_name, edit_type, category)
-- remove 'all-categories' and 'all-user-names' nulls as they are not aggregatable
HAVING user_name IS NOT NULL AND category IS NOT NULL
),
top_ranked_shallow_edits_per_category_per_edit_type AS (
SELECT *
FROM (
SELECT
user_name,
edit_type,
category,
'shallow' as category_scope,
edit_count,
RANK() over (PARTITION BY category, edit_type ORDER BY edit_count DESC) AS rank,
ROW_NUMBER() over (PARTITION BY category, edit_type ORDER BY edit_count DESC) AS row_number
FROM exploded_categories
)
WHERE row_number <= 100
),
top_ranked_primary_edits_per_category_per_edit_type AS (
SELECT *
FROM (
SELECT
user_name,
edit_type,
category,
'deep' as category_scope,
edit_count,
RANK() over (PARTITION BY category, edit_type ORDER BY edit_count DESC) AS rank,
ROW_NUMBER() over (PARTITION BY category, edit_type ORDER BY edit_count DESC) AS row_number
FROM exploded_primary_categories
)
WHERE row_number <= 100
),
top_ranked AS (
SELECT
category,
category_scope,
COALESCE(edit_type, 'all-edit-types') as edit_type, -- GROUP BY CUBE will set value to null when it is all-edit-types
user_name,
edit_count,
rank,
YEAR(TO_TIMESTAMP('${year_month}')) as year,
MONTH(TO_TIMESTAMP('${year_month}')) as month
FROM (
SELECT *
FROM top_ranked_shallow_edits_per_category_per_edit_type
UNION ALL
SELECT *
FROM top_ranked_primary_edits_per_category_per_edit_type
)
)
INSERT INTO ${destination_table}
SELECT
/*+ COALESCE(${coalesce_partitions}) */
category,
category_scope,
edit_type,
user_name,
edit_count,
rank,
year,
month
FROM top_ranked
ORDER BY
category,
category_scope,
edit_type,
year,
month,
rank
;