Return-Path: X-Original-To: apmail-spark-reviews-archive@minotaur.apache.org Delivered-To: apmail-spark-reviews-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 359F211FF7 for ; Tue, 29 Jul 2014 06:52:19 +0000 (UTC) Received: (qmail 70913 invoked by uid 500); 29 Jul 2014 06:52:19 -0000 Delivered-To: apmail-spark-reviews-archive@spark.apache.org Received: (qmail 70904 invoked by uid 500); 29 Jul 2014 06:52:19 -0000 Mailing-List: contact reviews-help@spark.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: reviews@spark.apache.org Delivered-To: mailing list reviews@spark.apache.org Received: (qmail 70886 invoked by uid 99); 29 Jul 2014 06:52:18 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 29 Jul 2014 06:52:18 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 6EC069B988F; Tue, 29 Jul 2014 06:52:18 +0000 (UTC) From: JoshRosen To: reviews@spark.apache.org Reply-To: reviews@spark.apache.org References: In-Reply-To: Subject: [GitHub] spark pull request: Example pyspark-inputformat for Avro file form... Content-Type: text/plain Message-Id: <20140729065218.6EC069B988F@tyr.zones.apache.org> Date: Tue, 29 Jul 2014 06:52:18 +0000 (UTC) Github user JoshRosen commented on a diff in the pull request: https://github.com/apache/spark/pull/1536#discussion_r15508267 --- 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 { --- End diff -- Unless I'm overlooking something, isn't this method essentially a no-op since it's a function Any => Any that only performs casts? --- 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 infrastructure@apache.org or file a JIRA ticket with INFRA. ---