spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yhuai <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-5938][SPARK-5443][SQL] Improve JsonRDD ...
Date Mon, 04 May 2015 04:25:23 GMT
Github user yhuai commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5801#discussion_r29565616
  
    --- Diff: sql/core/src/main/scala/org/apache/spark/sql/json/JsonRDD2.scala ---
    @@ -0,0 +1,409 @@
    +/*
    + * 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.sql.json
    +
    +import java.io.ByteArrayOutputStream
    +import java.sql.Timestamp
    +
    +import scala.collection.Map
    +
    +import com.fasterxml.jackson.core._
    +
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.sql.catalyst.analysis.HiveTypeCoercion
    +import org.apache.spark.sql.catalyst.expressions._
    +import org.apache.spark.sql.types._
    +import org.apache.spark.Logging
    +
    +private[sql] object JsonRDD2 extends Logging {
    +  def jsonStringToRow(
    +      json: RDD[String],
    +      schema: StructType,
    +      columnNameOfCorruptRecords: String): RDD[Row] = {
    +    parseJson(json, schema, columnNameOfCorruptRecords)
    +  }
    +
    +  def inferSchema(
    +      json: RDD[String],
    +      samplingRatio: Double = 1.0,
    +      columnNameOfCorruptRecords: String): StructType = {
    +    require(samplingRatio > 0, s"samplingRatio ($samplingRatio) should be greater
than 0")
    +    val schemaData = if (samplingRatio > 0.99) {
    +      json
    +    } else {
    +      json.sample(withReplacement = false, samplingRatio, 1)
    +    }
    +
    +    // perform schema inference on each row and merge afterwards
    +    schemaData.mapPartitions { iter =>
    +      val factory = new JsonFactory()
    +      iter.map { row =>
    +        try {
    +          val parser = factory.createParser(row)
    +          parser.nextToken()
    +          inferField(parser)
    +        } catch {
    +          case _: JsonParseException =>
    +            StructType(Seq(StructField(columnNameOfCorruptRecords, StringType)))
    +        }
    +      }
    +    }.treeAggregate[DataType](StructType(Seq()))(compatibleRootType, compatibleRootType)
match {
    +      case st: StructType => nullTypeToStringType(st)
    +    }
    +  }
    +
    +  /**
    +   * Infer the type of a json document from the parser's token stream
    +   */
    +  private def inferField(parser: JsonParser): DataType = {
    +    import com.fasterxml.jackson.core.JsonToken._
    +    parser.getCurrentToken match {
    +      case null | VALUE_NULL => NullType
    +
    +      case FIELD_NAME =>
    +        parser.nextToken()
    +        inferField(parser)
    +
    +      case VALUE_STRING if parser.getTextLength < 1 => NullType
    --- End diff --
    
    OK, I see. It is for those datasets that use a `""` as a null. Since it is for inferring
the type, it is same to use `NullType` because we can always get the `StringType` from other
records. Actually, can we add a comment at there to explain its purpose?


---
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.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message