开发者社区> 问答> 正文

Spark程序中使用SQL语句查询Hive的表数据的方法是什么?

Spark程序中使用SQL语句查询Hive的表数据的方法是什么?

展开
收起
游客qzzytmszf3zhq 2021-12-09 20:18:17 929 0
1 条回答
写回答
取消 提交回答
  • 我们在使用spark-shell登录时,默认初始化了一个spark实例:
    
    Spark session available as 'spark'.
    
    我们可以使用spark访问Hive的表数据。
    
     
    
    scala> val tableDF =spark.sql("select * from yahoo_stocks_orc limit 10")
    
    tableDF: org.apache.spark.sql.DataFrame = [date: string,open: float ... 5 more fields]
    
     
    
    查看10行数据:
    
    scala> tableDF.take(10).foreach(println)
    
    [2015-04-28,44.34,44.57,43.94,44.34,7188300,44.34]
    
    [2015-04-27,44.65,45.1,44.25,44.36,10840900,44.36]
    
    [2015-04-24,43.73,44.71,43.69,44.52,11267500,44.52]
    
    [2015-04-23,43.92,44.06,43.58,43.7,14274900,43.7]
    
    [2015-04-22,44.58,44.85,43.67,43.98,32241200,43.98]
    
    [2015-04-21,45.15,45.18,44.45,44.49,16103700,44.49]
    
    [2015-04-20,44.73,44.91,44.41,44.66,10052900,44.66]
    
    [2015-04-17,45.3,45.44,44.25,44.45,13305700,44.45]
    
    [2015-04-16,45.82,46.13,45.53,45.78,13800300,45.78]
    
    [2015-04-15,45.46,45.83,45.23,45.73,15033500,45.73]
    
    
    2021-12-09 20:18:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Hive Bucketing in Apache Spark 立即下载
spark替代HIVE实现ETL作业 立即下载
2019大数据技术公开课第五季—Hive迁移到MaxCompute最佳实践 立即下载