spark sql 模板 (python版)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


__author__ = 'hh'




import sys
import os
import datetime
import time
from pyspark.sql import SparkSession
from pyspark.sql import Row


spark = SparkSession \
    .builder \
    .appName("123") \
    .enableHiveSupport() \
    .getOrCreate()


def get_format_yesterday(format = '%Y-%m-%d'):
    """
    获取昨天日期(字符串)默认'%Y-%m-%d',
    format =‘%d' 获取昨天是本月中第几天
    """
    yesterday = (datetime.date.today() + datetime.timedelta(-1)).strftime(format)
    return yesterday




def main():


    if(len(sys.argv)>1): # 传了系统参数
        yesterday = sys.argv[1]
    else:
        yesterday = get_format_yesterday()


    yesterday_monthDay =  datetime.datetime.strptime(yesterday, '%Y-%m-%d').strftime('%d')  # 当月几号


    sql = """select * from app.app_ii_sku where dt = '""" + yesterday + """' limit 10"""
    print(sql)
    spark.sql(sql).show()


if __name__ == "__main__":
    main()

你可能感兴趣的:(python)