本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 输出结果保存本地或者HDFS上
var 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")))