player_checkpoint_query_cluster.sql 20 KB
Newer Older
WangJinfeng committed
1
use emr_doppler_v1;
wang-jinfeng committed
2 3 4 5 6 7 8 9 10 11 12 13

set hive.exec.compress.output=true;
set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;
set mapred.max.split.size=536870912;
set mapred.min.split.size.per.node=536870912;
set mapred.min.split.size.per.rack=536870912;
set mapreduce.map.memory.mb=2048;
set mapreduce.reduce.memory.mb=3072;
set hive.map.aggr = true;
set hive.map.aggr = true;
set hive.optimize.skewjoin=true;

WangJinfeng committed
14
-- ALTER TABLE emr_doppler_v1.player_checkpoint_daily SET LOCATION 's3://mob-emr-test/dataplatform/emr/player_checkpoint_daily';
wang-jinfeng committed
15

WangJinfeng committed
16
-- ALTER TABLE emr_doppler_v1.player_checkpoint SET LOCATION 's3://mob-emr-test/dataplatform/emr/player_checkpoint';
wang-jinfeng committed
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

ALTER TABLE player_checkpoint_daily
    ADD IF NOT EXISTS PARTITION (dt = '${part_pre1_path}') LOCATION 's3://mob-emr-test/dataplatform/emr/${pre1_dateslash}/player_checkpoint';
ALTER TABLE player_checkpoint
    ADD IF NOT EXISTS PARTITION (dt = '${part_pre1_path}') LOCATION 's3://mob-emr-test/dataplatform/emr/${pre1_dateslash}/player_checkpoint';

INSERT overwrite table player_checkpoint_7days
SELECT game_id,
       player_id,
       SUM(revenue)              AS revenue_week,
       SUM(sum_session_length)   AS sum_session_length_week,
       SUM(count_session_length) AS count_session_length_week,
       count(*)                  AS days_played_week
FROM player_checkpoint_daily
WHERE dt <= '${part_pre1_path}'
  AND dt > '${part_pre8_path}'
GROUP BY game_id, player_id;

INSERT overwrite table player_checkpoint_30days
SELECT game_id,
       player_id,
       SUM(revenue)              AS revenue_month,
       SUM(sum_session_length)   AS sum_session_length_month,
       SUM(count_session_length) AS count_session_length_month,
       count(*)                  AS days_played_month
FROM player_checkpoint_daily
WHERE dt <= '${part_pre1_path}'
  AND dt > '${part_pre31_path}'
GROUP BY game_id, player_id;

INSERT overwrite table player_checkpoint_tmp
select t1.game_id,
       t1.checkpoint,
       t1.player_id,
       t1.user_id,
       t1.is_payer,
       t1.is_day_payer,
       t1.first_payed,
       t1.installed,
       t1.install_publisher,
       t1.install_site,
       t1.install_campaign,
       t1.install_adgroup,
       t1.install_ad,
       t1.install_keyword,
       t1.is_acquired,
       t1.revenue,
       t1.count_transaction_num,
       t1.min_transaction_num,
       t1.max_transaction_num,
       t1.country_code,
       t1.facebook_id,
       t1.ios_id,
       t1.ios_idfa,
       t1.ios_idfv,
       t1.android_id,
       t1.google_aid,
       t1.googleplus_id,
       t1.is_ad_tracking_limited,
       t1.is_logged_gamecenter,
       t1.is_logged_googleplay,
       t1.gender,
       t1.birth_year,
       t1.sdk_version,
       t1.engine_version,
       t1.os_version,
       t1.manufacturer,
       t1.device,
       t1.platform,
       t1.build,
       t1.connection_type,
       t1.is_jailbroken,
       t1.count_session_length,
       t1.min_session_length,
       t1.max_session_length,
       t1.sum_session_length,
       t1.sum2_session_length,
       t1.min_session_num,
       t1.max_session_num,
       t1.count_event,
       t1.count_transaction,
       t1.count_debug_error,
       t1.count_info_error,
       t1.count_warning_error,
       t1.count_error_error,
       t1.count_critical_error,
       t7.revenue_week,
       t30.revenue_month,
       t7.sum_session_length_week,
       t30.sum_session_length_month,
       t7.count_session_length_week,
       t30.count_session_length_month,
       t7.days_played_week,
       t30.days_played_month
from player_checkpoint_${date_str_undline} t1
         join player_checkpoint_7days t7 on (t1.game_id = t7.game_id and t1.player_id = t7.player_id)
         join player_checkpoint_30days t30 on (t1.game_id = t30.game_id and t1.player_id = t30.player_id);

insert overwrite table player_checkpoint partition (dt = '${part_pre1_path}')
select coalesce(t2.game_id, t1.game_id),
       coalesce(t2.checkpoint, t1.checkpoint),
       coalesce(t2.player_id, t1.player_id),
       coalesce(t2.user_id, t1.user_id),
       coalesce(t2.is_payer, t1.is_payer),
       coalesce(t2.is_day_payer, t1.is_day_payer),
       coalesce(t2.first_payed, t1.first_payed),
       coalesce(t2.installed, t1.installed),
       coalesce(t2.install_publisher, t1.install_publisher),
       coalesce(t2.install_site, t1.install_site),
       coalesce(t2.install_campaign, t1.install_campaign),
       coalesce(t2.install_adgroup, t1.install_adgroup),
       coalesce(t2.install_ad, t1.install_ad),
       coalesce(t2.install_keyword, t1.install_keyword),
       coalesce(t2.is_acquired, t1.is_acquired),
       coalesce(t2.revenue, t1.revenue),
       coalesce(t2.count_transaction_num, t1.count_transaction_num),
       coalesce(t2.min_transaction_num, t1.min_transaction_num),
       coalesce(t2.max_transaction_num, t1.max_transaction_num),
       coalesce(t2.country_code, t1.country_code),
       coalesce(t2.facebook_id, t1.facebook_id),
       coalesce(t2.ios_id, t1.ios_id),
       coalesce(t2.ios_idfa, t1.ios_idfa),
       coalesce(t2.ios_idfv, t1.ios_idfv),
       coalesce(t2.android_id, t1.android_id),
       coalesce(t2.google_aid, t1.google_aid),
       coalesce(t2.googleplus_id, t1.googleplus_id),
       coalesce(t2.is_ad_tracking_limited, t1.is_ad_tracking_limited),
       coalesce(t2.is_logged_gamecenter, t1.is_logged_gamecenter),
       coalesce(t2.is_logged_googleplay, t1.is_logged_googleplay),
       coalesce(t2.gender, t1.gender),
       coalesce(t2.birth_year, t1.birth_year),
       coalesce(t2.sdk_version, t1.sdk_version),
       coalesce(t2.engine_version, t1.engine_version),
       coalesce(t2.os_version, t1.os_version),
       coalesce(t2.manufacturer, t1.manufacturer),
       coalesce(t2.device, t1.device),
       coalesce(t2.platform, t1.platform),
       coalesce(t2.build, t1.build),
       coalesce(t2.connection_type, t1.connection_type),
       coalesce(t2.is_jailbroken, t1.is_jailbroken),
       coalesce(t2.count_session_length, t1.count_session_length),
       coalesce(t2.min_session_length, t1.min_session_length),
       coalesce(t2.max_session_length, t1.max_session_length),
       coalesce(t2.sum_session_length, t1.sum_session_length),
       coalesce(t2.sum2_session_length, t1.sum2_session_length),
       coalesce(t2.min_session_num, t1.min_session_num),
       coalesce(t2.max_session_num, t1.max_session_num),
       coalesce(t2.count_event, t1.count_event),
       coalesce(t2.count_transaction, t1.count_transaction),
       coalesce(t2.count_debug_error, t1.count_debug_error),
       coalesce(t2.count_info_error, t1.count_info_error),
       coalesce(t2.count_warning_error, t1.count_warning_error),
       coalesce(t2.count_error_error, t1.count_error_error),
       coalesce(t2.count_critical_error, t1.count_critical_error),
       coalesce(t2.revenue_week, t1.revenue_week),
       coalesce(t2.revenue_month, t1.revenue_month),
       coalesce(t2.sum_session_length_week, t1.sum_session_length_week),
       coalesce(t2.sum_session_length_month, t1.sum_session_length_month),
       coalesce(t2.count_session_length_week, t1.count_session_length_week),
       coalesce(t2.count_session_length_month, t1.count_session_length_month),
       coalesce(t2.days_played_week, t1.days_played_week),
       coalesce(t2.days_played_month, t1.days_played_month)
from (select * from player_checkpoint where dt = '${part_pre2_path}') t1
         full join player_checkpoint_tmp t2 on (t1.game_id = t2.game_id and t1.player_id = t2.player_id);

INSERT overwrite TABLE tmp_daily_export_player_checkpoint_device_stats
SELECT device_id,
       platform,
       country,
       first_seen,
       num_transactions,
       total_games,
       action_games,
       arcade_games,
       puzzle_games,
       adventure_games,
       casual_games,
       simulation_games,
       strategy_games,
       educational_games,
       family_games,
       role_playing_games,
       sessions_count_30d,
       cast(sum_sessions_30d as float) / cast(sessions_count_30d as float) as session_length_30d,
       sessions_count_7d,
       cast(sum_sessions_7d as float) / cast(sessions_count_7d as float)   as session_length_7d,
       sessions_count,
       cast(sum_sessions as float) / cast(sessions_count as float)         as session_length,
       facebook_id,
       googleplus_id,
       gender,
       birth_year,
       revenue_week,
       revenue_month,
       device_purchase
FROM (
         SELECT case
                    when ios_idfa IS NOT NULL then ios_idfa
                    when google_aid IS NOT NULL then google_aid
                    end                                                                             as device_id,
                case
                    when ios_idfa IS NOT NULL then 'iOS'
                    when google_aid IS NOT NULL then 'Android'
                    end                                                                             as platform,
                MIN(country_code)                                                                   as country,
                MIN(checkpoint)                                                                     as first_seen,
                SUM(count_transaction)                                                              as num_transactions,
                COUNT(DISTINCT p.game_id)                                                           as total_games,
                COUNT(DISTINCT case when c.category = 'Action' then p.game_id end)                  as action_games,
                COUNT(DISTINCT case when c.category = 'Arcade' then p.game_id end)                  as arcade_games,
                COUNT(DISTINCT case when c.category = 'Puzzle' then p.game_id end)                  as puzzle_games,
                COUNT(DISTINCT case when c.category = 'Adventure' then p.game_id end)               as adventure_games,
                COUNT(DISTINCT case when c.category = 'Casual' then p.game_id end)                  as casual_games,
                COUNT(DISTINCT case when c.category = 'Simulation' then p.game_id end)              as simulation_games,
                COUNT(DISTINCT case when c.category = 'Strategy' then p.game_id end)                as strategy_games,
                COUNT(DISTINCT case when c.category = 'Educational' then p.game_id end)             as educational_games,
                COUNT(DISTINCT case when c.category = 'Family' then p.game_id end)                  as family_games,
                COUNT(DISTINCT case when c.category = 'Role Playing' then p.game_id end)            as role_playing_games,
                SUM(case
                        when DATEDIFF('${cur_date}', checkpoint) <= 30
                            then count_session_length end)                                          as sessions_count_30d,
                SUM(case
                        when DATEDIFF('${cur_date}', checkpoint) <= 30
                            then sum_session_length end)                                            as sum_sessions_30d,
                SUM(case
                        when DATEDIFF('${cur_date}', checkpoint) <= 7
                            then count_session_length end)                                          as sessions_count_7d,
                SUM(case when DATEDIFF('${cur_date}', checkpoint) <= 7 then sum_session_length end) as sum_sessions_7d,
                SUM(count_session_length)                                                           as sessions_count,
                SUM(sum_session_length)                                                             as sum_sessions,
                MIN(facebook_id)                                                                    as facebook_id,
                MIN(googleplus_id)                                                                  as googleplus_id,
                MIN(gender)                                                                         as gender,
                MIN(birth_year)                                                                     as birth_year,
                MIN(revenue_week)                                                                   as revenue_week,
                MIN(revenue_month)                                                                  as revenue_month,
                SUM(case when is_day_payer = '01' then 1 when is_day_payer = 'false' then 0 end)    as device_purchase
         FROM player_checkpoint p
                  JOIN game_categories c
                       ON p.game_id = c.game_id
         WHERE p.dt = '${part_pre1_path}'
           AND cast(p.checkpoint as string) = '${part_pre1_dash}'
           AND (ios_idfa IS NOT NULL OR google_aid IS NOT NULL)
         GROUP BY case
                      when ios_idfa IS NOT NULL then ios_idfa
                      when google_aid IS NOT NULL then google_aid
                      end,
                  case
                      when ios_idfa IS NOT NULL then 'iOS'
                      when google_aid IS NOT NULL then 'Android'
                      end
     ) as p1;

INSERT overwrite TABLE tmp_daily_export_game_fields
SELECT device_id,
       AVG(cast(game_sessions as float)) as average_sessions_per_app,
       AVG(cast(game_days as float))     as average_days_per_app
FROM (SELECT case
                 when ios_idfa IS NOT NULL then ios_idfa
                 when google_aid IS NOT NULL then google_aid
                 end                    as device_id,
             game_id,
             SUM(count_session_length)  as game_sessions,
             COUNT(DISTINCT checkpoint) as game_days
      FROM player_checkpoint p
      WHERE dt = '${part_pre1_path}'
        and cast(p.checkpoint as string) = '${part_pre1_dash}'
      GROUP BY case
                   when ios_idfa IS NOT NULL then ios_idfa
                   when google_aid IS NOT NULL then google_aid
                   end,
               game_id
     ) p
GROUP BY device_id;

DROP TABLE player_device_metrics_${date_str_undline};
CREATE EXTERNAL TABLE `player_device_metrics_${date_str_undline}`
(
    `device_id`                string,
    `platform`                 string,
    `country`                  string,
    `first_seen`               date,
    `num_transactions`         bigint,
    `total_games`              bigint,
    `action_games`             bigint,
    `arcade_games`             bigint,
    `puzzle_games`             bigint,
    `adventure_games`          bigint,
    `casual_games`             bigint,
    `simulation_games`         bigint,
    `strategy_games`           bigint,
    `educational_games`        bigint,
    `family_games`             bigint,
    `role_playing_games`       bigint,
    `sessions_count_30d`       bigint,
    `session_length_30d`       double,
    `sessions_count_7d`        bigint,
    `session_length_7d`        double,
    `sessions_count`           bigint,
    `session_length`           double,
    `facebook_id`              string,
    `googleplus_id`            string,
    `gender`                   string,
    `birth_year`               string,
    `revenue_week`             float,
    `revenue_month`            float,
    `average_sessions_per_app` double,
    `average_days_per_app`     double,
    `device_purchase`          bigint
)
    ROW FORMAT SERDE
        'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
        WITH SERDEPROPERTIES (
        'field.delim' = '|',
        'serialization.format' = '|')
    STORED AS INPUTFORMAT
        'org.apache.hadoop.mapred.TextInputFormat'
        OUTPUTFORMAT
            'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
    LOCATION
        's3://mob-emr-test/dataplatform/datawarehourse/dmp/daily_export/${pre1_dateslash}/device-metrics';

insert overwrite table player_device_metrics_${date_str_undline}
SELECT a.device_id,
       a.platform,
       a.country,
       a.first_seen,
       a.num_transactions,
       a.total_games,
       a.action_games,
       a.arcade_games,
       a.puzzle_games,
       a.adventure_games,
       a.casual_games,
       a.simulation_games,
       a.strategy_games,
       a.educational_games,
       a.family_games,
       a.role_playing_games,
       a.sessions_count_30d,
       a.session_length_30d,
       a.sessions_count_7d,
       a.session_length_7d,
       a.sessions_count,
       a.session_length,
       a.facebook_id,
       a.googleplus_id,
       a.gender,
       a.birth_year,
       a.revenue_week,
       a.revenue_month,
       d.average_sessions_per_app,
       d.average_days_per_app,
       a.device_purchase
FROM tmp_daily_export_player_checkpoint_device_stats a
         JOIN tmp_daily_export_game_fields d
              ON d.device_id = a.device_id;

-- ALTER TABLE player_store_game SET LOCATION 's3://mob-emr-test/dataplatform/emr/store_game';

WangJinfeng committed
377
INSERT OVERWRITE TABLE emr_doppler.all_tmp_game_store
wang-jinfeng committed
378
SELECT game_id, store_id
WangJinfeng committed
379
FROM emr_doppler.player_store_game WHERE store_id IS NOT NULL;
wang-jinfeng committed
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

DROP TABLE player_installation_list_${date_str_undline};
CREATE EXTERNAL TABLE `player_installation_list_${date_str_undline}`
(
    `device_id`    string,
    `platform`     string,
    `package_name` string,
    `store_link`   string
)
    ROW FORMAT SERDE
        'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
        WITH SERDEPROPERTIES (
        'field.delim' = '|',
        'serialization.format' = '|')
    STORED AS INPUTFORMAT
        'org.apache.hadoop.mapred.TextInputFormat'
        OUTPUTFORMAT
            'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
    LOCATION
        's3://mob-emr-test/dataplatform/datawarehourse/dmp/daily_export/${pre1_dateslash}/installation-list';

insert overwrite table player_installation_list_${date_str_undline}
SELECT case
           when ios_idfa IS NOT NULL then ios_idfa
           when google_aid IS NOT NULL then google_aid
           end  as device_id,
       platform,
       store_id as package_name,
       case
           when LOWER(platform) = 'android' then 'https://play.google.com/store/apps/details?id=' || store_id
           when LOWER(platform) = 'ios' then 'https://itunes.apple.com/app/id' || store_id
           end  as store_link
FROM player_checkpoint pc,
WangJinfeng committed
413
     emr_doppler.all_tmp_game_store gs
wang-jinfeng committed
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
WHERE not (ios_idfa is null and google_aid is null)
  AND pc.dt = '${part_pre1_path}'
  AND cast(pc.checkpoint as string) = '${part_pre1_dash}'
  AND pc.game_id = gs.game_id
  AND not gs.store_id is null
GROUP BY case
             when ios_idfa IS NOT NULL then ios_idfa
             when google_aid IS NOT NULL then google_aid
             end,
         platform,
         gs.store_id;

ALTER TABLE player_checkpoint_ga
    SET LOCATION 's3://mob-emr-test/dataplatform/datawarehourse/ga/player_checkpoint_ga';

ALTER TABLE player_checkpoint_ga
    ADD IF NOT EXISTS PARTITION (dt = '${part_pre1_path}') LOCATION 's3://mob-emr-test/dataplatform/datawarehourse/ga/player_checkpoint_ga/${part_pre1_path}';

insert overwrite table player_checkpoint_ga partition (dt = '${part_pre1_path}')
select coalesce(max(ios_idfa), max(google_aid), max(android_id)) as dev_id,
       max(device)                                               as model,
       max(country_code)                                         as country,
       max(os_version)                                           as osversion
from player_checkpoint pc
where pc.dt = '${part_pre1_path}'
  AND cast(pc.checkpoint as string) = '${part_pre1_dash}'
group by coalesce(ios_idfa, google_aid, android_id);

DROP TABLE dau_device_export_${date_str_undline};
create external table IF NOT EXISTS dau_device_export_${date_str_undline}
(
    device_id  string,
    platform   string,
    android_id string
)
    row format delimited fields terminated by '|'
    stored as textfile
    location 's3://mob-emr-test/dataplatform/datawarehourse/dmp/daily_export/${pre1_dateslash}/dau-device-data-export';

insert overwrite table dau_device_export_${date_str_undline}
SELECT COALESCE(ios_idfa, google_aid)                       as device_id,
       platform,
       case when google_aid is not null then android_id end as android_id
FROM player_checkpoint pc
where pc.dt = '${part_pre1_path}'
  AND cast(pc.checkpoint as string) = '${part_pre1_dash}'
  and not (ios_idfa is null and google_aid is null);

drop table dau_device_export_${date_str_undline};
DROP TABLE player_device_metrics_${date_str_undline};
DROP table player_installation_list_${date_str_undline};