Github user JoshRosen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1536#discussion_r15508369
  
    --- Diff: 
examples/src/main/scala/org/apache/spark/examples/pythonconverters/AvroGenericConverter.scala
 ---
    @@ -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 {   
    +      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] 
    +      case _ => ""
    +    }
    +
    +    def unpack(value: Any, schema: Schema): Any = schema.getType match {
    --- End diff --
    
    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.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to