如何在类中注册python函数作为sparksql的udf

时间:2018-06-08 07:03:18

标签: python apache-spark pyspark

这是我的问题:

我有一个带有一些nlp处理函数的类,我也必须使用sparksql数据帧来处理这个问题

这是一个例子

class nlpcut:

    def __init__(self,content):
         self.content = content

    @staticmethod
    def __spark_session(filePath):
        filePath = SparkSession.builder.appName('test').master('yarn').getOrCreate()
        dataFrameReader = session.read
        data = dataFrameReader.option('header','true') \
                              .option('inferSchema',value=True) \
                              .csv(filePath)
        return data

     def __nlp_cut(self):
         # processing some nlp cut..
         cut = nlp.cut(self.content)

         return cut

     def spark_nlp_cut(self):

         # here i want to use sparksql UDF 
         data_session = DPP().__spark_session('/project/test.csv')

         # i have problem at this part, how can i insert column 'address' content into class self.content in to class DPP?

         my_udf = udf(DPP.__nlp_cut, StringType())

         data_session.select('address',my_udf('address').alias('address_cut')).show()

是否有任何方法可以将列#34;地址"从sparkSQL dataFrame到class作为全局值,它作为self.content插入到udf函数中? 谢谢

0 个答案:

没有答案
相关问题