|
3 | 3 |
|
4 | 4 | On the roadmap is to write a task that runs validation queries on the aggregated Hive data pre-load.
|
5 | 5 | """
|
| 6 | +import datetime |
6 | 7 | import logging
|
7 | 8 |
|
8 | 9 | import luigi.date_interval
|
9 | 10 |
|
| 11 | +from edx.analytics.tasks.common.spark import SparkJobTask |
10 | 12 | from edx.analytics.tasks.common.vertica_load import VerticaCopyTask, VerticaCopyTaskMixin
|
11 | 13 | from edx.analytics.tasks.insights.database_imports import ImportAuthUserTask
|
12 |
| -from edx.analytics.tasks.insights.user_activity import InsertToMysqlCourseActivityTask, UserActivityTableTask |
| 14 | +from edx.analytics.tasks.insights.user_activity import InsertToMysqlCourseActivityTask, UserActivityTableTask, \ |
| 15 | + UserActivityTaskSpark |
13 | 16 | from edx.analytics.tasks.util.hive import (
|
14 | 17 | BareHiveTableTask, HivePartition, HivePartitionTask, WarehouseMixin, hive_database_name
|
15 | 18 | )
|
@@ -42,6 +45,111 @@ def columns(self):
|
42 | 45 | ]
|
43 | 46 |
|
44 | 47 |
|
| 48 | +class InternalReportingUserActivityPartitionTaskSpark(WarehouseMixin, SparkJobTask): |
| 49 | + """Spark alternative of InternalReportingUserActivityPartitionTask""" |
| 50 | + |
| 51 | + date = luigi.DateParameter() |
| 52 | + overwrite_n_days = luigi.IntParameter( |
| 53 | + config_path={'section': 'user-activity', 'name': 'overwrite_n_days'}, |
| 54 | + significant=False, |
| 55 | + ) |
| 56 | + |
| 57 | + def run(self): |
| 58 | + self.remove_output_on_overwrite() |
| 59 | + super(InternalReportingUserActivityPartitionTaskSpark, self).run() |
| 60 | + |
| 61 | + def requires(self): |
| 62 | + required_tasks = [ |
| 63 | + ImportAuthUserTask(overwrite=False, destination=self.warehouse_path) |
| 64 | + ] |
| 65 | + if self.overwrite_n_days > 0: |
| 66 | + overwrite_from_date = self.date - datetime.timedelta(days=self.overwrite_n_days) |
| 67 | + overwrite_interval = luigi.date_interval.Custom(overwrite_from_date, self.date) |
| 68 | + required_tasks.append( |
| 69 | + UserActivityTaskSpark( |
| 70 | + interval=overwrite_interval, |
| 71 | + warehouse_path=self.warehouse_path, |
| 72 | + output_root=self._get_user_activity_hive_table_path(), |
| 73 | + overwrite=True, |
| 74 | + ) |
| 75 | + ) |
| 76 | + yield required_tasks |
| 77 | + |
| 78 | + def _get_auth_user_hive_table_path(self): |
| 79 | + import_date = datetime.datetime.utcnow().date() # we only need to join import date's data with user activity |
| 80 | + return url_path_join( |
| 81 | + self.warehouse_path, |
| 82 | + 'auth_user', |
| 83 | + 'dt={}'.format(import_date.isoformat()) |
| 84 | + ) |
| 85 | + |
| 86 | + def _get_auth_user_table_schema(self): |
| 87 | + from pyspark.sql.types import StructType, StringType |
| 88 | + schema = StructType().add("id", StringType(), True) \ |
| 89 | + .add("username", StringType(), True) \ |
| 90 | + .add("last_login", StringType(), True) \ |
| 91 | + .add("date_joined", StringType(), True) \ |
| 92 | + .add("is_active", StringType(), True) \ |
| 93 | + .add("is_superuser", StringType(), True) \ |
| 94 | + .add("is_staff", StringType(), True) \ |
| 95 | + .add("email", StringType(), True) \ |
| 96 | + .add("dt", StringType(), True) |
| 97 | + return schema |
| 98 | + |
| 99 | + def _get_user_activity_hive_table_path(self, *args): |
| 100 | + return url_path_join( |
| 101 | + self.warehouse_path, |
| 102 | + 'user_activity' |
| 103 | + ) |
| 104 | + |
| 105 | + def _get_user_activity_table_schema(self): |
| 106 | + from pyspark.sql.types import StructType, StringType |
| 107 | + schema = StructType().add("course_id", StringType(), True) \ |
| 108 | + .add("username", StringType(), True) \ |
| 109 | + .add("date", StringType(), True) \ |
| 110 | + .add("category", StringType(), True) \ |
| 111 | + .add("count", StringType(), True) \ |
| 112 | + .add("dt", StringType(), True) |
| 113 | + return schema |
| 114 | + |
| 115 | + def spark_job(self, *args): |
| 116 | + auth_user_df = self._spark.read.csv( |
| 117 | + self._get_auth_user_hive_table_path(), |
| 118 | + schema=self._get_auth_user_table_schema(), |
| 119 | + sep='\x01', |
| 120 | + nullValue='\N' |
| 121 | + ) |
| 122 | + user_activity_df = self._spark.read.csv( |
| 123 | + self._get_user_activity_hive_table_path(*args), |
| 124 | + sep='\t', |
| 125 | + schema=self._get_user_activity_table_schema() |
| 126 | + ) |
| 127 | + self._sql_context.registerDataFrameAsTable(auth_user_df, 'auth_user') |
| 128 | + self._sql_context.registerDataFrameAsTable(user_activity_df, 'user_activity') |
| 129 | + query = """ |
| 130 | + SELECT |
| 131 | + au.id, |
| 132 | + ua.course_id, |
| 133 | + ua.date, |
| 134 | + ua.category, |
| 135 | + ua.count |
| 136 | + FROM auth_user au |
| 137 | + JOIN user_activity ua |
| 138 | + ON au.username = ua.username |
| 139 | + """ |
| 140 | + result = self._sql_context.sql(query) |
| 141 | + result.coalesce(2).write.csv(self.output().path, mode='overwrite', sep='\t') |
| 142 | + |
| 143 | + def output(self): |
| 144 | + return get_target_from_url( |
| 145 | + url_path_join( |
| 146 | + self.warehouse_path, |
| 147 | + 'internal_reporting_user_activity', |
| 148 | + 'dt={}'.format(self.date.isoformat()) |
| 149 | + ) |
| 150 | + ) |
| 151 | + |
| 152 | + |
45 | 153 | class InternalReportingUserActivityPartitionTask(HivePartitionTask):
|
46 | 154 | """Aggregate the user activity table in Hive."""
|
47 | 155 |
|
|
0 commit comments