如何在pyspark中应用函数?

如何在pyspark中应用函数?,pyspark,apache-spark-sql,Pyspark,Apache Spark Sql,我有一个返回特定日期的函数,如下所示: def specific_date(date_input): specificdate= """select * from vw where date = {date_1} """.format(date_1 = date_input) day_result = sqlCon

我有一个返回特定日期的函数,如下所示:

def specific_date(date_input):
    specificdate= """select *
                    from vw
                    where date = {date_1}
              """.format(date_1 = date_input)
    day_result = sqlContext.sql(specificdate)
    return day_result
df1_schema = StructType([StructField("Date", StringType(), True),\
                              StructField("col1", IntegerType(), True),\
                             StructField("id", StringType(), True),\
                       StructField("col2", IntegerType(), True),\
                       StructField("col3", IntegerType(), True),\
                       StructField("col4", IntegerType(), True),\
                        StructField("coln", IntegerType(), True)])
df_data = [('2020-08-01',0,'M1',3,3,2,2),('2020-08-02',0,'M1',2,3,0,1),\
           ('2020-08-03',0,'M1',3,3,2,3),('2020-08-04',0,'M1',3,3,2,1),\
            ('2020-08-01',0,'M2',1,3,3,1),('2020-08-02',0,'M2',-1,3,1,2)]
rdd = sc.parallelize(df_data)
df1 = sqlContext.createDataFrame(df_data, df1_schema)
df1 = df1.withColumn("Date",to_date("Date", 'yyyy-MM-dd'))
df1.show()

+----------+----+---+----+----+----+----+
|      Date|col1| id|col2|col3|col4|coln|
+----------+----+---+----+----+----+----+
|2020-08-01|   0| M1|   3|   3|   2|   2|
|2020-08-02|   0| M1|   2|   3|   0|   1|
|2020-08-03|   0| M1|   3|   3|   2|   3|
|2020-08-04|   0| M1|   3|   3|   2|   1|
|2020-08-01|   0| M2|   1|   3|   3|   1|
|2020-08-02|   0| M2|  -1|   3|   1|   2|
+----------+----+---+----+----+----+----+

df1.createOrReplaceTempView("vw")
我有一个数据框,看起来像这样:

def specific_date(date_input):
    specificdate= """select *
                    from vw
                    where date = {date_1}
              """.format(date_1 = date_input)
    day_result = sqlContext.sql(specificdate)
    return day_result
df1_schema = StructType([StructField("Date", StringType(), True),\
                              StructField("col1", IntegerType(), True),\
                             StructField("id", StringType(), True),\
                       StructField("col2", IntegerType(), True),\
                       StructField("col3", IntegerType(), True),\
                       StructField("col4", IntegerType(), True),\
                        StructField("coln", IntegerType(), True)])
df_data = [('2020-08-01',0,'M1',3,3,2,2),('2020-08-02',0,'M1',2,3,0,1),\
           ('2020-08-03',0,'M1',3,3,2,3),('2020-08-04',0,'M1',3,3,2,1),\
            ('2020-08-01',0,'M2',1,3,3,1),('2020-08-02',0,'M2',-1,3,1,2)]
rdd = sc.parallelize(df_data)
df1 = sqlContext.createDataFrame(df_data, df1_schema)
df1 = df1.withColumn("Date",to_date("Date", 'yyyy-MM-dd'))
df1.show()

+----------+----+---+----+----+----+----+
|      Date|col1| id|col2|col3|col4|coln|
+----------+----+---+----+----+----+----+
|2020-08-01|   0| M1|   3|   3|   2|   2|
|2020-08-02|   0| M1|   2|   3|   0|   1|
|2020-08-03|   0| M1|   3|   3|   2|   3|
|2020-08-04|   0| M1|   3|   3|   2|   1|
|2020-08-01|   0| M2|   1|   3|   3|   1|
|2020-08-02|   0| M2|  -1|   3|   1|   2|
+----------+----+---+----+----+----+----+

df1.createOrReplaceTempView("vw")
然后,如果我调用函数
specific\u date(F.date\u add('2020-08-01',1))
这将为我提供日期为“2020-08-02”的数据框

+----------+----+---+----+----+----+----+
|      Date|col1| id|col2|col3|col4|coln|
+----------+----+---+----+----+----+----+
|2020-08-02|   0| M1|   2|   3|   0|   1|
|2020-08-02|   0| M2|  -1|   3|   1|   2|
+----------+----+---+----+----+----+----+

我尝试了许多方法来实现这一点,但似乎不起作用,如果您不需要使用tempview的函数,则可以通过以下方式轻松实现:

import datetime

d = datetime.datetime.strptime("2020-08-01", "%Y-%m-%d")
d += datetime.timedelta(days=+1)
df1.where(col('Date') == d).show()

+----------+----+---+----+----+----+----+
|      Date|col1| id|col2|col3|col4|coln|
+----------+----+---+----+----+----+----+
|2020-08-02|   0| M1|   2|   3|   0|   1|
|2020-08-02|   0| M2|  -1|   3|   1|   2|
+----------+----+---+----+----+----+----+

您提供的代码的一个问题是spark函数
F.date\u add
返回列对象。这不能直接用在
where
语句中。

如果您确实想使用函数向给定的日期时间添加天数,并使用SQL查询:

def specific_date(date_input, days_to_add):
    start_date = datetime.datetime.strptime(date_input, "%Y-%m-%d")
    end_date = start_date + datetime.timedelta(days = days_to_add)
    specificdate= "SELECT * FROM vw WHERE Date = date_format('{date_1}', 'yyyy-MM-dd')".format(date_1 = end_date)
    day_result = sqlContext.sql(specificdate)
    return day_result
只需将其用作输入日期和添加日期即可

specific_date('2020-08-01', 1)
这将为您提供数据帧

+----------+----+---+----+----+----+----+
|      Date|col1| id|col2|col3|col4|coln|
+----------+----+---+----+----+----+----+
|2020-08-02|   0| M1|   2|   3|   0|   1|
|2020-08-02|   0| M2|  -1|   3|   1|   2|
+----------+----+---+----+----+----+----+
但更好的办法是直接使用

day_result = df1.filter(df1.Date == '2020-08-02')

使用函数的具体原因是什么?您可以使用
day\u result=df1.filter(df1.Date=='2020-08-02')
获得想要的日期,您还可以使用
day\u result=spark.sql(“从vw中选择*其中Date=Date\u格式('{Date\u 1}',yyyyyy-MM-dd')。格式(Date\u 1=end\u日期))
,而不是sqlContext.sql