Spark Week1 HomeWork
package wikipedia
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.rdd.RDD
import org.apache.log4j.{Level,Logger}
case class WikipediaArticle(title: String, text: String) {
/**
* @return Whether the text of this article mentions `lang` or not
* @param lang Language to look for (e.g. "Scala")
*/
def mentionsLanguage(lang: String): Boolean = text.split(' ').contains(lang)
}
object WikipediaRanking {
// 设置日志
Logger.getLogger("org").setLevel(Level.ERROR)
val langs = List(
"JavaScript", "Java", "PHP", "Python", "C#", "C++", "Ruby", "CSS",
"Objective-C", "Perl", "Scala", "Haskell", "MATLAB", "Clojure", "Groovy")
val conf: SparkConf = new SparkConf()
val sc: SparkContext = new SparkContext("local[*]", "Wikipedia")
// Hint: use a combination of `sc.textFile`, `WikipediaData.filePath` and `WikipediaData.parse`
val wikiRdd: RDD[WikipediaArticle] = sc.textFile(WikipediaData.filePath).map(WikipediaData.parse)
/** Returns the number of articles on which the language `lang` occurs. 返回lang语言出现的文章篇数
* Hint1: consider using method `aggregate` on RDD[T].
* Hint2: consider using method `mentionsLanguage` on `WikipediaArticle`
*/
def occurrencesOfLang(lang: String, rdd: RDD[WikipediaArticle]): Int =
rdd.filter(_.mentionsLanguage(lang)).count().toInt
/* (1) Use `occurrencesOfLang` to compute the ranking of the languages
* (`val langs`) by determining the number of Wikipedia articles that
* mention each language at least once. Don't forget to sort the
* languages by their occurrence, in decreasing order!
*
* Note: this operation is long-running. It can potentially run for
* several seconds.
*/
def rankLangs(langs: List[String], rdd: RDD[WikipediaArticle]): List[(String, Int)] = {
rdd.cache() // 允许数据存储在内存
langs.map(lang => (lang, occurrencesOfLang(lang, rdd))).sortBy(_._2).reverse
/*
对于langs的每一个元素找到包含它的文章篇数。
其中sortBy(_._2)指根据occurrencesOfLang(lang, rdd))来排序,
如果是sortBy(_._1)则根据lang来排序
默认从小到大排序,所以加上.reverse
*/
}
/* Compute an inverted index of the set of articles, mapping each language
* to the Wikipedia pages in which it occurs.
*/
def makeIndex(langs: List[String], rdd: RDD[WikipediaArticle]): RDD[(String, Iterable[WikipediaArticle])] = {
val articles_Languages = rdd.flatMap(article => {
langs.filter(lang => article.mentionsLanguage(lang))
.map(lang => (lang, article))
})
articles_Languages.groupByKey
}
/* (2) Compute the language ranking again, but now using the inverted index. Can you notice
* a performance improvement?
*
* Note: this operation is long-running. It can potentially run for
* several seconds.
*/
def rankLangsUsingIndex(index: RDD[(String, Iterable[WikipediaArticle])]): List[(String, Int)] =
index.mapValues(_.size).sortBy(-_._2).collect().toList
/* (3) Use `reduceByKey` so that the computation of the index and the ranking are combined.
* Can you notice an improvement in performance compared to measuring *both* the computation of the index
* and the computation of the ranking? If so, can you think of a reason?
*
* Note: this operation is long-running. It can potentially run for
* several seconds.
*/
def rankLangsReduceByKey(langs: List[String], rdd: RDD[WikipediaArticle]): List[(String, Int)] = {
rdd.flatMap(article => {
langs.filter(article.mentionsLanguage) // 相当于langs.filter(lang => article.mentionsLanguage(lang)) 或者 langs.filter(article.mentionsLanguage(_))
.map((_, 1))
}).reduceByKey(_ + _)
.sortBy(_._2)
.collect()
.toList
.reverse
}
def main(args: Array[String]) {
/* Languages ranked according to (1) */
val langsRanked: List[(String, Int)] = timed("Part 1: naive ranking", rankLangs(langs, wikiRdd))
/* An inverted index mapping languages to wikipedia pages on which they appear */
def index: RDD[(String, Iterable[WikipediaArticle])] = makeIndex(langs, wikiRdd)
/* Languages ranked according to (2), using the inverted index */
val langsRanked2: List[(String, Int)] = timed("Part 2: ranking using inverted index", rankLangsUsingIndex(index))
/* Languages ranked according to (3) */
val langsRanked3: List[(String, Int)] = timed("Part 3: ranking using reduceByKey", rankLangsReduceByKey(langs, wikiRdd))
/* Output the speed of each ranking */
println(timing)
sc.stop()
}
val timing = new StringBuffer
def timed[T](label: String, code: => T): T = {
val start = System.currentTimeMillis()
val result = code
val stop = System.currentTimeMillis()
timing.append(s"Processing $label took ${stop - start} ms.\n")
result
}
}
Spark Week1 HomeWork的更多相关文章
- CentOS7 安装spark集群
Spark版本 1.6.0 Scala版本 2.11.7 Zookeeper版本 3.4.7 配置虚拟机 3台虚拟机,sm,sd1,sd2 1. 关闭防火墙 systemctl stop firewa ...
- 【cs229-Lecture2】Linear Regression with One Variable (Week 1)(含测试数据和源码)
从Ⅱ到Ⅳ都在讲的是线性回归,其中第Ⅱ章讲得是简单线性回归(simple linear regression, SLR)(单变量),第Ⅲ章讲的是线代基础,第Ⅳ章讲的是多元回归(大于一个自变量). 本文的 ...
- Spark小课堂Week1 Hello Spark
Spark小课堂Week1 Hello Spark 看到Spark这个词,你的第一印象是什么? 这是一朵"火花",官方的定义是Spark是一个高速的.通用的.分布式计算系统!!! ...
- Week1 Team Homework #2 Introduction of team member with photos
小组成员介绍 组长:黄剑锟 11061164 组员:顾泽鹏 11061160 组员:周辰光 11061154 组员:龚少波 11061167 组 ...
- 团队博客作业Week1 Team Homework #3软件工程在北航
这次我们采访了一位大四的学姐,让她简单地谈了谈去年学习软件工程的经历和感受. 在完成软件工程大作业的过程中,由于计划安排与实际脱节,导致时间前松后紧,平均每周花在这门课上的时间大约有8个小时. 项目完 ...
- Week1 Team Homework #1: Study the projects done by previous student groups
我们研究了学长的项目:百度3D地图API的调用.下面是我们对该项目的一些看法: 优点: 界面清晰 各类之间调用及其他关系容易理清. 缺点: 前段html代码过于冗杂,很多(div)块间的层次关系不 ...
- Week1 Team Homework #3: 软件工程在北航
在组内成员的共同努力,我们采访了几个学长学姐,顺利完成任务.反馈信息如下: 平均每周花在这门课上的时间 平均写的代码总行数 学到的最有用的部分 最没用的部分 <软件工程>最应该改进的地方 ...
- Week1 Team Homework #2: Introduction of each team member
王洛书 我是来自浙江的王洛书.热爱历史,热爱美食,热爱代码,热爱博物馆.很喜欢软件工程这门课的上课方式,也很喜欢组里的这些同学.希望能大家一起努力,在这门课上真正地收获能力上的提升! 陈睿翊
- Week1 Team Homework #1 from Z.XML-对于学长项目《shield star》的思考和看法
试用了一下学长黄杨等人开发的<shield star>游戏. 其实作为一个学弟,我对cocos2d-x引擎还算是比较了解,所以对于这样一款很“典型 ...
随机推荐
- Java MaxDirectMemorySize
Refer to for detail: https://dzone.com/articles/default-hotspot-maximum-direct-memory-size 1. Java d ...
- 用python的curl和lxml来抓取和分析网页内容
Curl是一个强大的URL语法的客户端,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, PO ...
- 10个有趣的javascript和css库(2019年5月最新)
我们的使命是让您了解最新和最酷的Web开发趋势.这就是为什么我们每个月都会发布一些精选的资源,这些资源是我们偶然发现并认为值得您关注的. 1.Tessaract.js 强大的javascript(节点 ...
- Java基础(六) static五大应用场景
static和final是两个我们必须掌握的关键字.不同于其他关键字,他们都有多种用法,而且在一定环境下使用,可以提高程序的运行性能,优化程序的结构.上一个章节我们讲了final关键字的原理及用法,本 ...
- Django之F、Q查询,事务,自定义char字段
F查询 from django.db.models import F,Q # 当查询条件来自于数据库的某个字段,这个时候就必须使用F # 查询卖出数大于库存数的商品 res = models.Prod ...
- Angular4.0从入门到实战打造在线竞拍网站学习笔记之一--组件
Angular4.0基础知识之组件 Angular4.0基础知识之路由 Angular4.0依赖注入 Angular4.0数据绑定&管道 最近搞到手了一部Angular4的视频教程,这几天正好 ...
- 前端Vue基础学习
Vue基础 对与vue的简洁明了的干货分享,适合小白学习观看,如果有笔误或者想了解更多的,请给笔者留言,后续会继续分享,从零开始的vue相关经验 1.创建vue实例 <div id=" ...
- 【nginx】反向代理
反向代理事项负载均衡 是什么:百度 版本 :tomcat8 x2 nginx 1.8.1 保证两台tomcat正常启动. 配置文件:D:\nginx-1.8.1\conf\nginx.conf ,红色 ...
- css之vw布局
vw,vh是视口单位,是相对视口单位,与百分百布局不一样的是,百分百是相对于父及元素,而vw布局是相对与窗口. 而rem布局是要与js一起配合 // 以iphone6设计稿 @function px2 ...
- 提升——树形DP
这里讲提高一点的内容,所以没有树形DP基础的,先看一下基础部分: 浅说——树形DP 闲言不表,看第一题. 这道题是典型的树上最长链问题.(就是一个模板题) 给定一棵树,树上共有N个节点(N<=5 ...