Last active
May 20, 2018 15:12
-
-
Save SuoXC/fa84c5d47b05e681bf15cd76a07cac3d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| from pyspark import SparkContext | |
| from pyspark import HiveContext | |
| from pyspark.sql.types import * | |
| from _collections import defaultdict | |
| from datetime import date | |
| from operator import add | |
| from datetime import datetime | |
| sc = SparkContext() | |
| sql=''' | |
| select imei as key, collect_list(distinct day) as days from ana_login.ana_user_login where day > 20180101 group by imei | |
| ''' | |
| # sqlCtx = HiveContext(sc) | |
| df = sqlCtx.sql(sql) | |
| def handle(row): | |
| ''' | |
| row: (key,days) | |
| flatmap | |
| (key,days) => [(interval_days,times),(interval_days,times)] | |
| ''' | |
| result = [] | |
| tid = row['key'] | |
| day = row['days'] | |
| day.sort() | |
| distribution = defaultdict(int) | |
| for before,after in zip(day[0:],day[1:]): | |
| interval = (datetime.strptime(str(after),"%Y%m%d") - datetime.strptime(str(before),"%Y%m%d")).days | |
| distribution[interval] += 1 | |
| for k,v in distribution.items(): | |
| result.append((k,v)) | |
| return result | |
| df.rdd.flatMap(handle).reduceByKey(add).collect() | |
| # print(rdd.collect()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment