Back to home page

OSCL-LXR

 
 

    


0001 #
0002 # Licensed to the Apache Software Foundation (ASF) under one or more
0003 # contributor license agreements.  See the NOTICE file distributed with
0004 # this work for additional information regarding copyright ownership.
0005 # The ASF licenses this file to You under the Apache License, Version 2.0
0006 # (the "License"); you may not use this file except in compliance with
0007 # the License.  You may obtain a copy of the License at
0008 #
0009 #    http://www.apache.org/licenses/LICENSE-2.0
0010 #
0011 # Unless required by applicable law or agreed to in writing, software
0012 # distributed under the License is distributed on an "AS IS" BASIS,
0013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014 # See the License for the specific language governing permissions and
0015 # limitations under the License.
0016 #
0017 
0018 """
0019 Read data file users.avro in local Spark distro:
0020 
0021 $ cd $SPARK_HOME
0022 $ ./bin/spark-submit --driver-class-path /path/to/example/jar \
0023 > ./examples/src/main/python/avro_inputformat.py \
0024 > examples/src/main/resources/users.avro
0025 {u'favorite_color': None, u'name': u'Alyssa', u'favorite_numbers': [3, 9, 15, 20]}
0026 {u'favorite_color': u'red', u'name': u'Ben', u'favorite_numbers': []}
0027 
0028 To read name and favorite_color fields only, specify the following reader schema:
0029 
0030 $ cat examples/src/main/resources/user.avsc
0031 {"namespace": "example.avro",
0032  "type": "record",
0033  "name": "User",
0034  "fields": [
0035      {"name": "name", "type": "string"},
0036      {"name": "favorite_color", "type": ["string", "null"]}
0037  ]
0038 }
0039 
0040 $ ./bin/spark-submit --driver-class-path /path/to/example/jar \
0041 > ./examples/src/main/python/avro_inputformat.py \
0042 > examples/src/main/resources/users.avro examples/src/main/resources/user.avsc
0043 {u'favorite_color': None, u'name': u'Alyssa'}
0044 {u'favorite_color': u'red', u'name': u'Ben'}
0045 """
0046 from __future__ import print_function
0047 
0048 import sys
0049 
0050 from functools import reduce
0051 from pyspark.sql import SparkSession
0052 
0053 if __name__ == "__main__":
0054     if len(sys.argv) != 2 and len(sys.argv) != 3:
0055         print("""
0056         Usage: avro_inputformat <data_file> [reader_schema_file]
0057 
0058         Run with example jar:
0059         ./bin/spark-submit --driver-class-path /path/to/example/jar \
0060         /path/to/examples/avro_inputformat.py <data_file> [reader_schema_file]
0061         Assumes you have Avro data stored in <data_file>. Reader schema can be optionally specified
0062         in [reader_schema_file].
0063         """, file=sys.stderr)
0064         sys.exit(-1)
0065 
0066     path = sys.argv[1]
0067 
0068     spark = SparkSession\
0069         .builder\
0070         .appName("AvroKeyInputFormat")\
0071         .getOrCreate()
0072 
0073     sc = spark.sparkContext
0074 
0075     conf = None
0076     if len(sys.argv) == 3:
0077         schema_rdd = sc.textFile(sys.argv[2], 1).collect()
0078         conf = {"avro.schema.input.key": reduce(lambda x, y: x + y, schema_rdd)}
0079 
0080     avro_rdd = sc.newAPIHadoopFile(
0081         path,
0082         "org.apache.avro.mapreduce.AvroKeyInputFormat",
0083         "org.apache.avro.mapred.AvroKey",
0084         "org.apache.hadoop.io.NullWritable",
0085         keyConverter="org.apache.spark.examples.pythonconverters.AvroWrapperToJavaConverter",
0086         conf=conf)
0087     output = avro_rdd.map(lambda x: x[0]).collect()
0088     for k in output:
0089         print(k)
0090 
0091     spark.stop()