Spark 和 hive 的新手。目前,我可以运行spark 1.5.2,也可以从命令行访问hive。我希望能够以编程方式连接到配置单元数据库,运行查询并将数据提取到数据框,所有这些都在spark内部。我认为这种工作流程是非常标准的。但是我不知道该怎么做。

现在,我知道可以在 Spark 中获得HiveContext了:

import org.apache.spark.sql.hive.HiveContext;

我可以在 hive 内进行所有查询
SHOW TABLES;
>>customers
  students
  ...

然后,我可以从表中获取数据:
SELECT * FROM customers limit 100;

我该如何将这2个 Spark 塞在一起?

谢谢。

最佳答案

// sc是现有的SparkContext。

val sqlContext = new org.apache.spark.sql.hive.HiveContext(sc)

//查询以HiveQL表示
val tablelist = sqlContext.sql("show tables")
val custdf = sqlContext.sql("SELECT * FROM customers limit 100")

tablelist.collect().foreach(println)
custdf.collect().foreach(println)

关于hadoop - 如何在Spark内部设置Hive数据库连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40349290/

10-11 03:59
查看更多