spark-shell 测试demo_for_SQL

本文介绍了使用HSDF+Spark模式进行数据分析的具体步骤,包括数据导入、分析、SQL查询及结果保存等内容。通过实例展示了如何读取文件、执行WordCount等常见任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本demo使用hsdf+spark的模式分析数据。

1.导入数据
使用hdfs 命令导入文件到hdfs上。

2.分析数据

进入spark目录执行 spark-shell

示例1

SparkSession类替换了Apache Spark 2.0中的SparkContext和SQLContext,并为spark集群提供了唯一的入口点。

val spark=SparkSession
    .builder()
    .appName("SparkTwoExample")
    .getOrCreate()

为了向后兼容,SparkSession对象包含SparkContext和SQLContext对象。当使用交互式Spark shell 脚本是,为我们创建了一个名为spark的SparkSession对象。

//加载需要分析的文件(数据源可以是json,可以是hdfs,可以是数据库等)
val df=spark.read.parquet("/apps-data/activate_log/activate_log.20170902.parquet");

//指定临时表名,方便使用sql语句 (多个表需要关联查询,创建多个不同df即可)
scala>df.registerTempTable("table")

//查看sql执行结果
scala>spark.sql("SELECT * FROM table limit 1").show()

//保存
df.select("name", "favorite_color").write.save("namesAndFavColors.parquet")

或者

spark.sql("SELECT * FROM table limit 1").write.format("json").save("./syjTable");

示例2 引用自 http://bbs.superwu.cn/forum.php?mod=viewthread&tid=1103

测试文本内容
[hadoop@mycluster ~]$ cat /home/datadev/wc.txt 
hello   me
hello   you
hello   china
hello   you


hdfs dfs -put /home/datadev/wc.txt ./ 

1、读取本地或者HDFS文件

spark启动时候生成SparkContext 对象sc,通过spark的上下文对象sc读取文件

命令:scala> var textFile = sc.textFile("/home/datadev/wc.txt").collect

执行结果:textFile: Array[String] = Array(hello   me, hello       you, hello      china, hello    you)

2、执行文件

2.1 flatMap 对读取的结果通过制表符方式平摊

scala> var textFile = sc.textFile("/user/datadev/wc.txt").flatMap(line => line.split("\t")).collect
或者
scala> var textFile = sc.textFile("/user/datadev/wc.txt").flatMap(_.split("\t")).collect
结果:
textFile: Array[String] = Array(hello, me, hello, you, hello, china, hello, you)


2.2  map(word=>(word,1))   word表示每个单词,每个单词为1

scala> var textFile = sc.textFile("/user/datadev/wc.txt").flatMap(line => line.split("\t")).map(word => (word,1)).collect
或者
scala> var textFile = sc.textFile("/user/datadev/wc.txt").flatMap(_.split("\t")).map((_,1)).collect

结果:
textFile: Array[(String, Int)] = Array((hello,1), (me,1), (hello,1), (you,1), (hello,1), (china,1), (hello,1), (you,1))


2.3 执行reduceByKey函数
scala> var textFile = sc.textFile("/user/datadev/wc.txt").flatMap(_.split("\t")).map((_,1)).reduceByKey( (a,b) => a + b ).collect
或者
var textFile = sc.textFile("/user/datadev/wc.txt").flatMap(_.split("\t")).map((_,1)).reduceByKey( _ + _ ).collect
结果:
textFile: Array[(String, Int)] = Array((hello,4), (me,1), (you,2), (china,1))


2.4 key 字段进行排序
scala> var textFile = sc.textFile("/user/datadev/wc.txt").flatMap(_.split("\t")).map((_,1)).reduceByKey( _ + _ ).sortByKey(true).collect   
结果:
textFile: Array[(String, Int)] = Array((china,4), ("hello ",1), (me,1), (you,2))

2.5 输出结果保存本地或者HDFSvar textFile = sc.textFile("/user/datadev/wc.txt").flatMap(_.split("\t")).map((_,1)).reduceByKey( _ + _ ).sortByKey(true).saveAsTextFile("/user/datadev/output")

执行结果: 
[hadoop@mycluster output]$ more part-00000 
(hello,4)
(me,1)
[hadoop@mycluster output]$ more part-00001 
(you,2)
(china,1)


2.6 让输出结果仅生成一个文件
var textFile = sc.textFile("/user/datadev/wc.txt").flatMap(_.split("\t")).map((_,1)).reduceByKey(_+_).repartition(1).saveAsTextFile("/user/datadev/output")

执行结果:
[hadoop@mycluster output]$ more part-00000 
(hello,4)
(me,1)
(you,2)
(china,1)


以上就是wordcount的例子。下面给出读取hdfs上的数据的案例
var textFile = sc.textFile("hdfs://mycluster:9000/wc.txt").flatMap(_.split("\t")).map((_,1)).reduceByKey(_+_).repartition(1).saveAsTextFile("hdfs://mycluster:9000/output")

执行结果:
[hadoop@mycluster output]$ hdfs dfs -cat hdfs://mycluster:9000/output/part-00000
(hello,4)
(me,1)
(you,2)
(china,1)

当然api也提供了一些示例

  // Create a DataFrame from Parquet files
   val people = sqlContext.parquetFile("...")

   // Create a DataFrame from data sources
   val df = sqlContext.load("...", "json")

      // To create DataFrame using SQLContext
   val people = sqlContext.parquetFile("...")
   val department = sqlContext.parquetFile("...")

   people.filter("age" > 30)
     .join(department, people("deptId") === department("id"))
     .groupBy(department("name"), "gender")
     .agg(avg(people("salary")), max(people("age")))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值