HyukjinKwon commented on a change in pull request #23797: [WIP][SPARK-26856][PYSPARK] Python support for from_avro and to_avro APIs URL: https://github.com/apache/spark/pull/23797#discussion_r259312592
########## File path: python/pyspark/sql/avro/functions.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +A collections of builtin avro functions +""" + + +from pyspark import since, SparkContext +from pyspark.sql.column import Column, _to_java_column +from pyspark.util import _print_missing_jar + + +@since(3.0) +def from_avro(data, jsonFormatSchema, options={}): + """ + Converts a binary column of avro format into its corresponding catalyst value. The specified + schema must match the read data, otherwise the behavior is undefined: it may fail or return + arbitrary result. + + Avro is built-in but external data source module since Spark 2.4. Please deploy the application + as per the deployment section of "Apache Avro Data Source Guide". + + :param data: the binary column. + :param jsonFormatSchema: the avro schema in JSON string format. + :param options: options to control how the Avro record is parsed. + + >>> from pyspark.sql import Row + >>> from pyspark.sql.avro.functions import from_avro, to_avro + >>> data = [(1, Row(name='Alice', age=2))] + >>> df = spark.createDataFrame(data, ("key", "value")) + >>> avroDf = df.select(to_avro(df.value).alias("avro")) + >>> avroDf.collect() + [Row(avro=bytearray(b'\\x00\\x00\\x04\\x00\\nAlice'))] + >>> jsonFormatSchema = '''{"type":"record","name":"topLevelRecord","fields": + ... [{"name":"avro","type":[{"type":"record","name":"value","namespace":"topLevelRecord", + ... "fields":[{"name":"age","type":["long","null"]}, + ... {"name":"name","type":["string","null"]}]},"null"]}]}''' + >>> avroDf.select(from_avro(avroDf.avro, jsonFormatSchema).alias("value")).collect() + [Row(value=Row(avro=Row(age=2, name=u'Alice')))] + """ + + sc = SparkContext._active_spark_context + try: + jc = sc._jvm.org.apache.spark.sql.avro.functions.from_avro( + _to_java_column(data), jsonFormatSchema, options) + except TypeError as e: Review comment: Hm, but okay. Let's leave it as is and fix it later together since I found other places that use similar approaches. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
