Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.
*/


package org.apache.spark.examples.pythonconverters

import org.apache.spark.api.python.Converter
import collection.JavaConversions._

import org.apache.avro.generic.GenericRecord
import org.apache.avro.mapred.AvroKey
import org.apache.avro.mapreduce.AvroKeyInputFormat
import org.apache.avro.Schema.Field
import org.apache.avro.Schema
import org.apache.avro.Schema.Type._

/*
Example usage in pyspark:

avroRdd = sc.newAPIHadoopFile("/tmp/data.avro",
"org.apache.avro.mapreduce.AvroKeyInputFormat",
"org.apache.avro.mapred.AvroKey",
"org.apache.hadoop.io.NullWritable",
keyConverter="org.apache.spark.examples.pythonconverters.AvroGenericConverter")
*/
class AvroGenericConverter extends Converter[AvroKey[GenericRecord], java.util.Map[String, Any]] {
override def convert(obj: AvroKey[GenericRecord]): java.util.Map[String, Any] = {

def unpackRecord(record: GenericRecord): java.util.Map[String,Any] = {
mapAsJavaMap(record.getSchema.getFields.map( f => (f.name, unpack(record.get(f.name), f.schema) ) ).toMap)
}

def unpackArray(value: Any, schema: Schema): java.util.List[Any] = {
bufferAsJavaList(value.asInstanceOf[java.util.List[Any]].map( v => unpack(v, schema)))
}

def unpackUnion(value: Any): Any = value match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm overlooking something, isn't this method essentially a no-op since it's a function Any => Any that only performs casts?

case v:java.lang.Double => value.asInstanceOf[java.lang.Double]
case v:java.lang.Float => value.asInstanceOf[java.lang.Float]
case v:java.lang.Integer => value.asInstanceOf[java.lang.Integer]
case v:java.lang.Long => value.asInstanceOf[java.lang.Long]
case v:java.lang.String => value.asInstanceOf[java.lang.String]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding case null => null for null handling?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kanzhang I couldn't get Java/Scala null passed into Python. It crashed when I tried to do this so I encoded it as an empty string instead. If someone else can figure this out, that would be cool.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing you could try to avoid NullPointerException is to wrap any intermediate value that could be null into Option(value) and only at the end of transformation call .orNull on it to get the value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that #1551 will address this issue with passing nulls from Java/Scala to Python. I'll update this thread once I've merged that PR.

case _ => ""
}

def unpack(value: Any, schema: Schema): Any = schema.getType match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like you can eliminate most of the cases in this unpack method since most of them don't need special handling. It looks like three distinct cases are Records, which we want to unpack into Maps, arrays, in which we want to recursively unpack any Records, and primitive types, which we pass through unchanged.

case ARRAY => unpackArray(value, schema.getElementType)
// case BOOLEAN
// case BYTES
case DOUBLE => value.asInstanceOf[java.lang.Double]
case ENUM => value.toString
// case FIXED
case FLOAT => value.asInstanceOf[java.lang.Float]
case INT => value.asInstanceOf[java.lang.Integer]
case LONG => value.asInstanceOf[java.lang.Long]
// case MAP
case RECORD => unpackRecord(value.asInstanceOf[GenericRecord])
case STRING => value.asInstanceOf[java.lang.String]
case UNION => unpackUnion(value)
case _ => value.toString
}

unpackRecord(obj.datum())
}
}