Spark <= 1.5
It is more or less equivalent to SQL table aliases:
SELECT *
FROM table AS alias;
Example usage adapted from PySpark alias
documentation:
import org.apache.spark.sql.functions.col
case class Person(name: String, age: Int)
val df = sqlContext.createDataFrame(
Person("Alice", 2) :: Person("Bob", 5) :: Nil)
val df_as1 = df.as("df1")
val df_as2 = df.as("df2")
val joined_df = df_as1.join(
df_as2, col("df1.name") === col("df2.name"), "inner")
joined_df.select(
col("df1.name"), col("df2.name"), col("df2.age")).show
Output:
+-----+-----+---+
| name| name|age|
+-----+-----+---+
|Alice|Alice| 2|
| Bob| Bob| 5|
+-----+-----+---+
Same thing using SQL query:
df.registerTempTable("df")
sqlContext.sql("""SELECT df1.name, df2.name, df2.age
FROM df AS df1 JOIN df AS df2
ON df1.name == df2.name""")
What is the purpose of this method?
Pretty much avoiding ambiguous column references.
Spark 1.6+
There is also a new as[U](implicit arg0: Encoder[U]): Dataset[U]
which is used to convert a DataFrame
to a DataSet
of a given type. For example:
df.as[Person]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…