Spark - 创建表并插入常量值

时间:2018-03-13 15:26:08

标签: apache-spark apache-spark-sql

使用spark shell创建单行临时表的最简单方法是什么?我试过插入,但RDD只写一次,对吧?我尝试了以下几行,但火花对第二行很不满意。

spark.sql("CREATE TEMPORARY VIEW CAT (PAWS INT) USING PARQUET")
spark.sql("INSERT INTO CAT VALUES 4")

我该怎么做?我想要一个内存表,就像我从csv读取它一样。任何提示?

2 个答案:

答案 0 :(得分:0)

这对你有用吗?

scala> val res = List(4).toDF("Paws")
res: org.apache.spark.sql.DataFrame = [Paws: int]

scala> res.show()
+----+
|Paws|
+----+
|   4|
+----+
scala> res.createOrReplaceTempView("CAT")

答案 1 :(得分:0)

使用DataFrame首先创建表 -

 val df = spark.createDataFrame(Seq(
        ("gyg1", 1, 1, 1, 1),
        ("gyg1", 2, 2, 2, 2),
        ("gyg2", 3, 3, 3, 3),
        ("gyg3", 4, 4, 4, 4),
        ("gyg3", 5, 5, 5, 5),
        ("gyg5", 6, 6, 6, 6)
      )).toDF("id", "a", "b", "c", "d")
// Creates a temporary view using the DataFrame.
df.createOrReplaceTempView("df")

// SQL statements can be run by using the sql methods provided by sqlContext.
val results = sqlContext.sql("SELECT name FROM df")

点击此处查看https://spark.apache.org/docs/preview/sql-programming-guide.html