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

    https://github.com/apache/spark/pull/14119#discussion_r70257720
  
    --- Diff: 
examples/src/main/scala/org/apache/spark/examples/sql/SparkSqlExample.scala ---
    @@ -0,0 +1,201 @@
    +/*
    + * 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.sql
    +
    +// $example on:schema_inferring$
    +import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
    +import org.apache.spark.sql.Encoder
    +// $example off:schema_inferring$
    +import org.apache.spark.sql.Row
    +// $example on:init_session$
    +import org.apache.spark.sql.SparkSession
    +// $example off:init_session$
    +// $example on:programmatic_schema$
    +import org.apache.spark.sql.types.StringType
    +import org.apache.spark.sql.types.StructField
    +import org.apache.spark.sql.types.StructType
    +// $example off:programmatic_schema$
    +
    +object SparkSqlExample {
    +
    +  // $example on:create_ds$
    +  // Note: Case classes in Scala 2.10 can support only up to 22 fields. To 
work around this limit,
    +  // you can use custom classes that implement the Product interface
    +  case class Person(name: String, age: Long)
    +  // $example off:create_ds$
    +
    +  def main(args: Array[String]) {
    +    // $example on:init_session$
    +    val spark = SparkSession
    +        .builder()
    +        .appName("Spark SQL Example")
    +        .config("spark.some.config.option", "some-value")
    +        .getOrCreate()
    +
    +    // For implicit conversions like converting RDDs to DataFrames
    +    import spark.implicits._
    +    // $example off:init_session$
    +
    +    runBasicDataFrameExample(spark)
    +    runDatasetCreationExample(spark)
    +    runInferSchemaExample(spark)
    +    runProgrammaticSchemaExample(spark)
    +
    +    spark.stop()
    +  }
    +
    +  private def runBasicDataFrameExample(spark: SparkSession): Unit = {
    +    // $example on:create_df$
    +    val df = spark.read.json("examples/src/main/resources/people.json")
    +
    +    // Displays the content of the DataFrame to stdout
    +    df.show()
    +    // age  name
    +    // null Michael
    +    // 30   Andy
    +    // 19   Justin
    +    // $example off:create_df$
    +
    +    // $example on:untyped_ops$
    +    // Print the schema in a tree format
    +    df.printSchema()
    +    // root
    +    // |-- age: long (nullable = true)
    +    // |-- name: string (nullable = true)
    +
    +    // Select only the "name" column
    +    df.select("name").show()
    +    // name
    +    // Michael
    +    // Andy
    +    // Justin
    +
    +    // Select everybody, but increment the age by 1
    +    df.select(df("name"), df("age") + 1).show()
    --- End diff --
    
    I'd prefer `$"name"` instead of `df("name")` through out all Scala 
examples. The latter is not recommended because it may introduce ambiguous 
query plans when dealing with self-joins. The `$`-notation needs `import 
spark.implicits._` though.


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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to