spark 腳本

self.spark.sql('''

? ? ? ? ? ? ? ? ? ? ? create or replace temporary view? temp_all_month as

? ? ? ? ? ? ? ? ? ? ? select? distinct expns_month_num

? ? ? ? ? ? ? ? ? ? ? from? ? temp_rdt_fin_Dashboard_ctr_anlys a

? ? ? ? ? ? ? ? ? ? ? where? year(to_date(substring(a.expns_month_num,1,6),'yyyyMM')) >= year(add_months(date_add(current_date(),-25),-12))?

? ? ? ? ? ? ? ? ? ? ? and? ? expns_month_num not like '%13'

? ? ? ? ? ? ? ? ? ? ? ''')

? ? ? ? #month_list = ['201902','201903' ,'201904','201905','201906','201907','201908','201909','201910','201911','201912'\

? ? ? ? #? ? ? ? ? ? ? ,'202001','202002','202003' ,'202004','202005','202006','202007','202008','202009','202010','202011','202012']

? ? ? ? #

? ? ? ? month_df =self.spark.sql('''

? ? ? ? ? ? ? ? ? ? select? cast(substring(expns_month_num,1,6) as int)? as? expns_month_num

? ? ? ? ? ? ? ? ? ? from? ? temp_all_month? a

? ? ? ? ? ? ? ? ? ? order by expns_month_num asc ''')

? ? ? ? #數據框轉化為列表行

? ? ? ? rows=month_df.select('expns_month_num').collect()

? ? ? ? #轉化為列表

? ? ? ? month_list = [row.expns_month_num? for row in rows]

? ? ? ? #先初始化第一個月份

? ? ? ? df1=self.spark.sql('''select * from temp_rdt_fin_Dashboard_ctr_anlys_basic? ''')

? ? ? ? df1.printSchema()

? ? ? ? df0 = df1.withColumn('expns_month_num', lit(month_list[0]))

? ? ? ? #從列表中扣除第一個

? ? ? ? month_list = month_list[1:]

? ? ? ? for item in month_list:

? ? ? ? ? ? df2 = df1.withColumn('expns_month_num', lit(item))

? ? ? ? ? ? df0 = df0.union(df2)

? ? ? ? dt = df0.createOrReplaceTempView('temp_rdt_fin_Dashboard_ctr_anlys_month')

?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容