How to change column metadata in pyspark?

五迷三道 提交于 2019-11-28 13:50:32
zero323

In both cases losing metadata is expected:

  • When you call Python udf there is no relationship between input Column and its metadata, and output Column. UserDefinedFunction (both in Python and Scala) are black boxes for the Spark engine.
  • Assigning data directly to the Python schema object:

    df2.schema.fields[1].metadata = extract(df.schema.fields[1].metadata)
    

    is not a valid approach at all. Spark DataFrame is a thing wrapper around JVM object. Any changes in the Python wrappers, are completely opaque for JVM backend, and won't be propagated at all:

    import json 
    
    df = spark.createDataFrame([(1, "foo")], ("k", "v"))
    df.schema[-1].metadata = {"foo": "bar"}
    
    json.loads(df._jdf.schema().json())
    
    ## {'fields': [{'metadata': {}, 'name': 'k', 'nullable': True, 'type': 'long'},
    ##   {'metadata': {}, 'name': 'v', 'nullable': True, 'type': 'string'}],
    ## 'type': 'struct'}
    

    or even preserved in Python:

    df.select("*").schema[-1].metadata
    ## {}
    

With Spark < 2.2 you can use a small wrapper (taken from Spark Gotchas, maintained by me and @eliasah):

def withMeta(self, alias, meta):
    sc = SparkContext._active_spark_context
    jmeta = sc._gateway.jvm.org.apache.spark.sql.types.Metadata
    return Column(getattr(self._jc, "as")(alias, jmeta.fromJson(json.dumps(meta))))

df.withColumn("foo", withMeta(col("foo"), "", {...}))

With Spark >= 2.2 you can use Column.alias:

df.withColumn("foo", col("foo").alias("", metadata={...}))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!