测试代码:

 import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.hive.HiveContext /**
* Created by Administrator on 2017/1/7.
*/
object TestMain {
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("Hangzhou_Test")
//.setMaster("local[1]").setMaster("spark://172.21.7.10:7077").setJars(List("xxx.jar")).set("spark.executor.memory", "10g")
val sc = new SparkContext(conf)
val hiveContext = new HiveContext(sc)
// use rc_hive_db;
hiveContext.sql("use rc_hive_db") import hiveContext.implicits._ hiveContext.setConf("mapred.max.split.size", "")
hiveContext.setConf("mapred.min.split.size.per.node", "")
hiveContext.setConf("mapred.min.split.size.per.rack", "")
hiveContext.setConf("hive.input.format", "org.apache.hadoop.hive.ql.io.CombineHiveInputFormat")
hiveContext.setConf("hive.merge.mapfiles", "true")
hiveContext.setConf("hive.merge.mapredfiles", "true")
hiveContext.setConf("hive.merge.size.per.task", "")
hiveContext.setConf("hive.merge.smallfiles.avgsize", "")
hiveContext.setConf("hive.groupby.skewindata", "true") hiveContext.sql("create table if not exists tb_id_vs_name(id int,name string)")
hiveContext.sql("create table if not exists tb_id_vs_name2(id int,name string)") println("-------------------------word count:------------------------------------")
// http://blog.csdn.net/t1dmzks/article/details/70189509
var words = "When building the vocabulary ignore terms that have a document frequency strictly lower than the given threshold. This value is also called cut-off in the literature. If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None."
val textFile = sc.parallelize(words.split(" "), )
textFile.flatMap(line => line.split(" "))
.map(word => (word, ))
.reduceByKey(_ + _)
.foreach(println) println("-------------------------map(func):------------------------------------")
// 1.map(func)
val rdd = sc.parallelize( to ) //创建RDD
val map = rdd.map(_ * ) //对RDD中的每个元素都乘于2
map.foreach(x => print(x + " ")) println("-------------------------flatMap(func):------------------------------------")
// 2.flatMap(func)
val fm = rdd.flatMap(x => ( to x)).collect()
fm.foreach(x => print(x + " ")) println("-------------------------mapPartitions(func) 1:------------------------------------")
// 3.mapPartitions(func)
val mp = sc.parallelize(List(("kpop", "female"), ("zorro", "male"), ("mobin", "male"), ("lucy", "female")), ).mapPartitions(x => {
var woman = List[String]()
while (x.hasNext) {
val next = x.next()
next match {
case (_, "female") => woman = next._1 :: woman
case _ =>
}
}
woman.iterator
})
/*val mp = rdd.mapPartitionsWithIndex(partitionsFun)*/
mp.collect.foreach(x => (print(x + " "))) //将分区中的元素转换成Aarray再输出 println("-------------------------mapPartitions(func) 2:------------------------------------")
sc.parallelize(List(("kpop", "female"), ("zorro", "male"), ("mobin", "male"), ("lucy", "female")), )
.mapPartitions(x => x.filter(_._2 == "female"))
.map(x => x._1)
.foreach(x => (print(x + " "))) println("-------------------------mapPartitionsWithIndex(func) :------------------------------------")
// 4.mapPartitionsWithIndex(func)
sc.parallelize(List(("kpop", "female"), ("zorro", "male"), ("mobin", "male"), ("lucy", "female")), )
.mapPartitionsWithIndex((index: Int, iter: Iterator[(String, String)]) => {
var woman = List[String]()
while (iter.hasNext) {
val next = iter.next()
next match {
case (_, "female") => woman = "[" + index + "]" + next._1 :: woman
case _ =>
}
}
woman.iterator
})
.collect.foreach(x => (print(x + " "))) //将分区中的元素转换成Aarray再输出 println("-------------------------simple(withReplacement,fraction,seed) :------------------------------------")
// 5.simple(withReplacement,fraction,seed)
val sample1 = rdd.sample(true, 0.5, )
sample1.collect.foreach(x => print(x + " ")) println("-------------------------union(ortherDataset) :将两个RDD中的数据集进行合并,最终返回两个RDD的并集,若RDD中存在相同的元素也不会去重------------------------------------")
// 6.union(ortherDataset)
val rdd1 = sc.parallelize( to )
val rdd2 = sc.parallelize( to )
rdd1.union(rdd2).collect.foreach(x => print(x + " ")) println("-------------------------union(ortherDataset) :返回两个RDD的交集------------------------------------")
// 7.intersection(otherDataset)
rdd1.intersection(rdd2).collect.foreach(x => print(x + " ")) println("-------------------------distinct([numTasks]) :对RDD中的元素进行去重------------------------------------")
// 8.distinct([numTasks])
sc.parallelize(List(, , , , , , , )).distinct().collect.foreach(x => print(x + " ")) println("-------------------------cartesian(otherDataset):对两个RDD中的所有元素进行笛卡尔积操作------------------------------------")
// 9.cartesian(otherDataset)
sc.parallelize( to ).cartesian(sc.parallelize( to )).foreach(x => println(x + " ")) println("-------------------------coalesce(numPartitions,shuffle):对RDD的分区进行重新分区,shuffle默认值为false,当shuffle=false时,不能增加分区数------------------------------------")
// 10.coalesce(numPartitions,shuffle)
val coalesceRDD = sc.parallelize( to , ).coalesce() //当suffle的值为false时,不能增加分区数(即分区数不能从5->7)
println("重新分区后的分区个数:" + coalesceRDD.partitions.size) val coalesceRDD2 = sc.parallelize( to , ).coalesce(, true)
println("重新分区后的分区个数:" + coalesceRDD2.partitions.size)
println("RDD依赖关系:" + coalesceRDD2.toDebugString) println("-------------------------repartition(numPartition):是函数coalesce(numPartition,true)的实现,效果和例9.1的coalesce(numPartition,true)的一样------------------------------------")
// 11.repartition(numPartition) // 12.glom()glom():将RDD的每个分区中的类型为T的元素转换换数组Array[T]
// 13.randomSplit(weight:Array[Double],seed):根据weight权重值将一个RDD划分成多个RDD,权重越高划分得到的元素较多的几率就越大 println("-------------------------repartition(numPartition)-----------------------------") sc.parallelize(List((, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""),
(, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""),
(, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""),
(, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""),
(, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""),
(, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""),
(, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""),
(, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, "")
)).map(s => (s._1, s._2)).toDF().registerTempTable("temp_tb_id_vs_name") hiveContext.sql("insert into tb_id_vs_name select * from temp_tb_id_vs_name") sc.parallelize(List((, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, ""), (, "")
)).map(s => (s._1, s._2)).toDF().registerTempTable("temp_tb_id_vs_name2") hiveContext.sql("insert into tb_id_vs_name2 select * from temp_tb_id_vs_name2") var result = hiveContext.sql("select t10.id as t10_id,t10.name as t10_name from tb_id_vs_name t10 inner join tb_id_vs_name2 t11 on t10.id=t11.id")
result.map(s => (s.getAs[Int]("t10_id"), s.getAs[String]("t10_name"))).foreach(s => {
println(s._1 + ":" + s._2)
}) sc.stop()
}
}

测试结果:

 -------------------------word count:------------------------------------
-------------------------map(func):------------------------------------
-------------------------flatMap(func):------------------------------------

-------------------------mapPartitions(func) :------------------------------------
kpop lucy
-------------------------mapPartitions(func) :------------------------------------
-------------------------mapPartitionsWithIndex(func) :------------------------------------
[]kpop []lucy -------------------------simple(withReplacement,fraction,seed) :------------------------------------

-------------------------union(ortherDataset) :灏嗕袱涓猂DD涓殑鏁版嵁闆嗚繘琛屽悎骞讹紝鏈€缁堣繑鍥炰袱涓猂DD鐨勫苟闆嗭紝鑻DD涓瓨鍦ㄧ浉鍚岀殑鍏冪礌涔熶笉浼氬幓閲-----------------------------------

-------------------------union(ortherDataset) :杩斿洖涓や釜RDD鐨勪氦闆-----------------------------------

-------------------------distinct([numTasks]) :瀵筊DD涓殑鍏冪礌杩涜鍘婚噸------------------------------------

-------------------------cartesian(otherDataset):瀵逛袱涓猂DD涓殑鎵€鏈夊厓绱犺繘琛岀瑳鍗″皵绉搷浣-----------------------------------
-------------------------coalesce(numPartitions锛宻huffle):瀵筊DD鐨勫垎鍖鸿繘琛岄噸鏂板垎鍖猴紝shuffle榛樿鍊间负false,褰搒huffle=false鏃讹紝涓嶈兘澧炲姞鍒嗗尯鏁-----------------------------------
閲嶆柊鍒嗗尯鍚庣殑鍒嗗尯涓暟:
閲嶆柊鍒嗗尯鍚庣殑鍒嗗尯涓暟:
RDD渚濊禆鍏崇郴:() MapPartitionsRDD[] at coalesce at TestMain.scala: []
| CoalescedRDD[] at coalesce at TestMain.scala: []
| ShuffledRDD[] at coalesce at TestMain.scala: []
+-() MapPartitionsRDD[] at coalesce at TestMain.scala: []
| ParallelCollectionRDD[] at parallelize at TestMain.scala: []
-------------------------repartition(numPartition):鏄嚱鏁癱oalesce(numPartition,true)鐨勫疄鐜帮紝鏁堟灉鍜屼緥9.1鐨刢oalesce(numPartition,true)鐨勪竴鏍-----------------------------------
-------------------------repartition(numPartition)-----------------------------

Spark测试代码的更多相关文章

  1. Hadoop基础-MapReduce入门篇之编写简单的Wordcount测试代码

    Hadoop基础-MapReduce入门篇之编写简单的Wordcount测试代码 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本文主要是记录一写我在学习MapReduce时的一些 ...

  2. Idea 编写 Spark 示例代码并打包成Jar

    说明:本人是在Linux下搭建的单机Spark环境,也是在Linux下使用Idea14.02进行代码编辑 1. 打开IDEA,在欢迎界面从右下角的Configure -> Plugins进入,安 ...

  3. 整合Kafka到Spark Streaming——代码示例和挑战

    作者Michael G. Noll是瑞士的一位工程师和研究员,效力于Verisign,是Verisign实验室的大规模数据分析基础设施(基础Hadoop)的技术主管.本文,Michael详细的演示了如 ...

  4. .NET单元测试的艺术-3.测试代码

    开篇:上一篇我们学习单元测试和核心技术:存根.模拟对象和隔离框架,它们是我们进行高质量单元测试的技术基础.本篇会集中在管理和组织单元测试的技术,以及如何确保在真实项目中进行高质量的单元测试. 系列目录 ...

  5. mysql锁 实战测试代码

    存储引擎 支持的锁定 MyISAM 表级锁 MEMORY 表级锁 InnoDB 行级锁 BDB 页面锁 表级锁:开销小,加锁快:不会出现死锁:锁定粒度大,发生锁冲突的概率最高,并发度最低.行级锁:开销 ...

  6. 使用Microsoft Fakes隔离测试代码

    在单元测试(Unit Test)中我们遇到的问题之一是:假如被测试组件(类或项目)为A,组件A依赖于组件B,那么在组件A的单元测试ATest中测试A时,也需要依赖于B,在B发生改动后,就可能影响到A的 ...

  7. iOS开发:XCTest单元测试(附上一个单例的测试代码)

    测试驱动开发并不是一个很新鲜的概念了.在我最开始学习程序编写时,最喜欢干的事情就是编写一段代码,然后运行观察结果是否正确.我所学习第一门语言是c语言,用的最多的是在算法设计上,那时候最常做的事情就是编 ...

  8. 在内核中异步请求设备固件firmware的测试代码

    在内核中异步请求设备固件firmware的测试代码 static void ghost_load_firmware_callback(const struct firmware *fw, void * ...

  9. x264测试代码

    建立一个工程,将头文件,库文件加载到工程,测试代码如下:#include <iostream>#include <string>#include "stdint.h& ...

随机推荐

  1. 20165230 《Java程序设计》第1周学习总结

    20165230 2017-2018-2 <Java程序设计>第1周学习总结 教材学习内容总结 本周通过学习了解了java的历史,地位,特点以及java的应用和基本的开发步骤,对Java有 ...

  2. 初探云服务器ECS(Linux系统)

    PS: 购买的阿里云服务器(ECS,Linux系统),使用的弹性公网IP(EIP). 一.使用Xshell链接ECS 1.将公网IP填入主机即可 2.用户名一般为root,密码是自己设置的,填入即可. ...

  3. Python进阶_面对对象&面对过程

    这节主要讲面对对象与面对过程两种编程思想的主要区别. 一. 简单对比 面向过程是一种基础的方法,它考虑的是实际的实现步骤,一般情况下,面向过程是自顶向下逐步求精,其最重要的是模块化的思想方法. 面向对 ...

  4. Mycat 读写分离详解

    Mycat 的读写分离是依赖数据库级别的数据主从同步的基础上来实现的(Mysql 的主从配置链接),Mycat 的读写分离是在 schema.xml 配置的 dataHost 节点的 balance ...

  5. 使用Mifare卡加密数据 笔记

    Mifare 是最常用的射频卡,具体介绍网上太多,我就不说了.,很多城市的最早的地铁公交卡都是用这种卡,后来被破解后都换成智能卡了. 但是由于技术成熟,使用方便,成本低,现在很多小区门禁卡,停车卡,食 ...

  6. 【jQuery】 jQuery基础

    jQuery 之前在JS的文章中提到过,JS虽然功能全面但是仍然比较接近底层,代码写起来很麻烦,而以jQuery为代表的JS库包装了很多功能,可以让代码更加简单.接下来就来简单地记录一下我学习和所知道 ...

  7. Mysql的执行计划各个参数详细说明

    执行计划各个参数的说明 1.id 主要是用来标识sql的执行顺序,如果没有子查询,一般来说id只有一个,执行顺序也是从上到下 2.select_type 每个select子句的类型 a:  simpl ...

  8. 记一次sqoop同步到mysql

    工作中需要用到将hive的数据导一份到mysql中,需求是这样的:hive每天会产生一份用户标签(tag)发生变化的结果表user_tag,这份结果同步到mysql中,并且保持一份全量表,存储当前用户 ...

  9. 团队作业4——第一次项目冲刺(Alpha版本)11.14

    a. 提供当天站立式会议照片一张 举行站立式会议,讨论项目安排: PM对整个项目的需求进行讲解: 全队对整个项目的细节进行沟通: 对整个项目的开发计划进行分析,分配每天的任务: 统一确定项目的开发环境 ...

  10. 2017-2018-1 我爱学Java 第八周 作业

    团队六七周作业 团队分工 UML图 工具选择 小编(金立清)有话说 参考资料 团队分工 返回目录 UML图 用例图 类图 活动图 状态图 返回目录 工具选择 ProcessOn - 免费在线作图,实时 ...