spark数据处理示例一:分类
@(SPARK)[spark, ML]
参考spark高级数据分析第2章
知识点
1、slice
2、NaN
3、mapValue\
4、groupBy
5、state
6、isNaN
7、scala的range结构
本项目根据训练数据,找出2个某个数据的类型(应该是true还是false),并用于下一步的预测。详细见第二部分的分析。
这里只使用了spark的基本API,没有使用mllib的算法。
(一)REPL测试
1、数据准备
下载并解压至~/Downloads/donation中
https://archive.ics.uci.edu/ml/machine-learning-databases/00210/donation.zip
2、启动spark
本例先在local模式下运行
bin/spark-shell
或者将文件上传至hdfs
hadoop fs -put ./donation/ /tmp/
再使用:
bin/spark-shell --master yarn-client
3、读入数据并简单验证读入情况
scala> val rawblocks = sc.textFile("/Users/liaoliuqing/Downloads/donation")
rawblocks: org.apache.spark.rdd.RDD[String] = MapPartitionsRDD[1] at textFile at <console>:21
scala> rawblocks.first
res0: String = "id_1","id_2","cmp_fname_c1","cmp_fname_c2","cmp_lname_c1","cmp_lname_c2","cmp_sex","cmp_bd","cmp_bm","cmp_by","cmp_plz","is_match"
scala> rawblocks.take(5).foreach(println)
"id_1","id_2","cmp_fname_c1","cmp_fname_c2","cmp_lname_c1","cmp_lname_c2","cmp_sex","cmp_bd","cmp_bm","cmp_by","cmp_plz","is_match"
37291,53113,0.833333333333333,?,1,?,1,1,1,1,0,TRUE
39086,47614,1,?,1,?,1,1,1,1,1,TRUE
70031,70237,1,?,1,?,1,1,1,1,1,TRUE
84795,97439,1,?,1,?,1,1,1,1,1,TRUE
scala> rawblocks.count
res2: Long = 5749142
4、去除标题行
从上面的数据输出中可以看到第一行是标题行,表明每个列分别是什么意思。但在实际数据分析中,我们并不需要这一行,因此将其删除。
scala> val noheader = rawblocks.filter(line => !line.contains("id_1"))
noheader: org.apache.spark.rdd.RDD[String] = MapPartitionsRDD[4] at filter at <console>:23
scala> noheader.count
res6: Long = 5749132
将行中有”id_1”字段的行去掉,这一般是标题行,当做也可以以其它字段作标准。去除后发现少了10行数据,目录中刚好有10个文件,每个文件去除第一行,即去除了10行。
5、提取行中的信息
(1)定义缺失值的处理
数据中存在数据丢失,这些数据以?代替,因此要先处理,否则直接调用toDouble会出错:
def myToDouble(s:String) = {
if("?".equals(s)) Double.NaN else s.toDouble
}
关于NaN: In computing, NaN, standing for not a number, is a numeric data type value representing an undefined or unrepresentable value, especially in floating-point calculations.
验证一下上面的方法:
scala> myToDouble("4")
res10: Double = 4.0
scala>
scala> myToDouble("?")
res11: Double = NaN
(2)提取行中的字段
def parse(line: String) = {
val pieces = line.split(",")
val id1 = pieces(0).toInt
val id2 = pieces(1).toInt
val scores = pieces.slice(2,11).map(myToDouble)
val matched = pieces(11).toBoolean
(id1,id2,scores,matched)
}
parse: (line: String)(Int, Int, Array[Double], Boolean)
这个方法将第1、2个字段作为id提供出来,中间9个字段作为double值组成一个array,最后是一个是否match的布尔值,它的返回是:
parse: (line: String)(Int, Int, Array[Double], Boolean)
验证一下上面的函数:
scala> noheader.take(5).map(parse).foreach(println)
(37291,53113,[D@2138bd8c,true)
(39086,47614,[D@1424435e,true)
(70031,70237,[D@58c2daa6,true)
(84795,97439,[D@60a0f5d0,true)
(36950,42116,[D@676a5c3f,true)
上面的返回是一个有4个元素的元组。下面我们将其封闭成一个对象返回。
(3)以case类对象的形式返回分析结果
scala> case class MatchData(id1: Int, id2: Int, scores: Array[Double], matched: Boolean)
defined class MatchData
def parse(line: String) = {
val pieces = line.split(",")
val id1 = pieces(0).toInt
val id2 = pieces(1).toInt
val scores = pieces.slice(2,11).map(myToDouble)
val matched = pieces(11).toBoolean
MatchData(id1,id2,scores,matched)
}
再看一下返回的结果:
scala> noheader.take(5).map(parse).foreach(println)
MatchData(37291,53113,[D@dd278c2,true)
MatchData(39086,47614,[D@74f60fa4,true)
MatchData(70031,70237,[D@467d13f9,true)
MatchData(84795,97439,[D@3daa6496,t