您的位置:首页 > 编程语言

51.Scala中链式调用风格的实现代码实战及其在Spark编程中的广泛运用

2017-09-26 13:46 513 查看
Spark中链式调用的例子:

sc.textFile(“hdfs://...”).flatMap(_.split(“
”)).map(_, 1).reduceByKey(_ +_).map(x => (x._2, x._1)).sortByKey(false).map(x=> (x._2, x_1)).saveAsTextFile(“hdfs://…")

class Animal {def breathe : this.type = this} //在scala中,任何类和对象都有个type属性,可能返回类、对象或者空。这里的type返回的是cat对象
class Cat extends Animal{def eat : this.type = this}

object Singleton_Types_51 {
def main(args: Array[String]): Unit = {
val cat = new Cat
cat.breathe.eat //如果把Animal里的breathe方法的": this.type"去掉的话,这里会报错。
//因为Animal返回了this,而this是Animal,Animal中没有eat方法,所以会报错。
//说value eat is not a member of Animal。
}
}

参考资料来源于大数据梦工厂 深入浅出scala 第51讲 由王家林老师讲解
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐