首页 文章 精选 留言 我的

精选列表

搜索[EdgeOneMakersAgent案例],共10000篇文章
优秀的个人博客,低调大师

MongoDB mapReduce案例分析二

例2、查找订单中一个商品对应的订单总数和这个商品每个订单的平均销售数量 1、先看订单表orders的数据结构如下图 这样的数据共有6条,如下,只列出了items这个字段 2、下面给出map函数 1 2 3 4 5 6 7 8 9 10 var mapFunction2 = function () { for ( var idx = 0; idx < this .items.length; idx++) { var key = this .items[idx].sku; var value = { count: 1, qty: this .items[idx].qty }; emit(key, value); } }; 3、写reduce函数 1 2 3 4 5 6 7 8 var reduceFunction2 = function (keySKU, countObjVals) { reducedVal = { count: 0, qty: 0 }; for ( var idx = 0; idx < countObjVals.length; idx++) { reducedVal.count += countObjVals[idx].count; reducedVal.qty += countObjVals[idx].qty; } return reducedVal; }; 4、写finalize函数 1 2 3 4 var finalizeFunction2 = function (key, reducedVal) { reducedVal.avg = reducedVal.qty/reducedVal.count; return reducedVal; }; 5、执行mapReduce函数 6、查看结果 本文转自shayang8851CTO博客,原文链接:http://blog.51cto.com/janephp/1292033,如需转载请自行联系原作者

优秀的个人博客,低调大师

实战案例:操作系统

一、busybox BusyBox 是一个集成了一百多个最常用Linux命令和工具的软件。BusyBox 包含了一些简单的工具, 例如ls、cat和echo等等,还包含了一些更大、更复杂的工具,例grep、find、mount以及telnet。 有些人将 BusyBox 称为 Linux 工具里的瑞士军刀。简单的说BusyBox就好像是个大工具箱,它集 成压缩了 Linux 的许多工具和命令,也包含了 Android 系统的自带的shell。 1、在dockerhub中搜索busybox相关的镜像 root@docker-test:~# docker search busybox NAME DESCRIPTION STARS OFFICIAL AUTOMATED busybox Busybox base image. 1075 [OK] progrium/busybox 67 [OK] hypriot/rpi-busybox-httpd Raspberry Pi compatible Docker Image with ... 36 hypriot/armhf-busybox Busybox base image for ARM. 7 armhf/busybox Busybox base image. 3 armel/busybox Busybox base image. 2 onsi/grace-busybox 2 s390x/busybox Busybox base image. 2 p7ppc64/busybox Busybox base image for ppc64. 2 prom/busybox Prometheus Busybox Docker base images 2 [OK] aarch64/busybox Busybox base image. 2 arm32v7/busybox Busybox base image. 2 i386/busybox Busybox base image. 1 arm32v6/busybox Busybox base image. 1 ppc64le/busybox Busybox base image. 1 spotify/busybox Spotify fork of https://hub.docker.com/_/b... 1 flynn/busybox Busybox from Ubuntu 13.10 with libc 1 estesp/busybox Test repo of a 'manifest list' to encapsul... 0 podshumok/busybox 0 trollin/busybox 0 nranjan/busybox 0 arm64v8/busybox Busybox base image. 0 ddn0/busybox fork of official busybox 0 [OK] arm32v5/busybox Busybox base image. 0 himanshu0503/busybox 0 root@docker-test:~# 提示:带有OFFICIAL标记的说明是官方镜像 2、查看镜像 root@docker-test:~# docker images busybox latest e3826c3cb1d4 4 weeks ago 1.129 MB 3、运行busybox root@docker-test:~# docker run -it busybox 运行后随便执行几个命令试试 / # pwd / / # ifconfig eth0 Link encap:Ethernet HWaddr 02:42:C0:A8:C8:02 inet addr:192.168.200.2 Bcast:0.0.0.0 Mask:255.255.255.0 inet6 addr: fe80::42:c0ff:fea8:c802/64 Scope:Link UP BROADCAST RUNNING MTU:1500 Metric:1 RX packets:7 errors:0 dropped:0 overruns:0 frame:0 TX packets:7 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:578 (578.0 B) TX bytes:578 (578.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) / # busybox虽然小巧,但提供了常见的linux命令 二、CoreOS 这是一个基于docker的Linux发行版 下载地址:https://stable.release.core-os.net/amd64-usr/current/coreos_production_vmware_ova.ova 用vmware workstation运行这个虚拟机 本文转自 TtrToby 51CTO博客,原文链接:http://blog.51cto.com/freshair/1958352

优秀的个人博客,低调大师

MySQL水平分割案例

水平分割:根据一列或者多列的值把数据行放到多个独立的表里,水平分表方式可以通过多个低配置主机整合起来,实现高性能。 根据公司的业务,两套重复的架构,部署在不同的IDC上,需要对数据进行合并操作,数据库采用水平方式实现业务数据的存储,如架构图: 说明:路由表A作用:md5key作为分表策略的依据,对md5key值进行取模运算,需要把数据写入具体表位置。 一般对于业务需求,可以对此字段进行做相应的处理,对数据的保护,如这样的语句: SELECT id,MOD(CRC32(keymd5),16) FROM user_index实现对user_*分表实现,在路由表和分表关联的字段为id字段,id与uid对应起来,ac表与路由表的关系也是id与uid对应,即路由表新增一行记录,为其他各表的唯一的UID值,实现数据的水平扩展。 本文转自 DBAspace 51CTO博客,原文链接:http://blog.51cto.com/dbaspace/1895617

优秀的个人博客,低调大师

MongoDB mapReduce案例分析一

例子1:找出集合中的所有键以及每个键对应的次数 MongoDB没有模式,所以并不知道每个文档有多少个键。通常找到集合中的所有键的最好方法就是利用MapReduce。好了,接下来看例子吧。 1、首先通过mongo->shell生成几条测试数据记录。 2、写map函数 3、写reduce函数 4、调用MapReduce看结果 5、查看结果 本文转自shayang8851CTO博客,原文链接:http://blog.51cto.com/janephp/1290604,如需转载请自行联系原作者

优秀的个人博客,低调大师

MapReduce初级案例——数据排序

“ 数据排序”是许多实际任务执行时要完成的第一项工作,比如学生成绩评比、 数据建立索引等。这个实例和数据去重类似,都是先对原始数据进行初步处理,为进一步的数据操作打好基础。下面进入这个示例。 1 实例描述 对输入文件中数据进行排序。 输入文件中的每行内容均为一个数字, 即一个数据。要求在输出中每行有两个间隔的数字,其中, 第一个代表原始数据在原始数据集中的位次, 第二个代表原始数据。 样例输入: (1) file1: (2) file2: (3) file3: 样例输出: 2 设计思路 这个实例仅仅要求对输入数据进行排序,熟悉 MapReduce 过程的读者会很快想到在MapReduce 过程中就有排序,是否可以利用这个默认的排序,而不需要自己再实现具体的排序呢?答案是肯定的。 但是在使用之前首先需要了解它的默认排序规则。它是按照 key 值进行排序的,如果key 为封装 int 的 IntWritable 类型,那么 MapReduce 按照数字大小对 key 排序,如果 key为封装为 String 的 Text 类型,那么 MapReduce 按照字典顺序对字符串排序。 了解了这个细节,我们就知道应该使用封装 int 的 IntWritable 型数据结构了。也就是在map 中将读入的数据转化成 IntWritable 型,然后作为 key 值输出( value 任意)。 reduce 拿到之后,将输入的 key 作为 value 输出,并根据 value-list 中元素的个数决定输出的次数。输出的 key(即代码中的 linenum)是一个全局变量,它统计当前 key 的位次。 需要注意的是这个程序中没有配置 Combiner,也就是在 MapReduce 过程中不使用 Combiner。这主要是因为使用 map 和 reduce 就已经能够完成任务了。 3 程序代码 程序代码如下所示: 4 代码结果 (1)准备测试数据 通过 Eclipse 下面的“ DFS Locations”在“ /user/hadoop”目录下创建输入文件“ sort_in”文件夹( 备注:“ sort_out”不需要创建。)如图 2.4-1 所示,已经成功创建。 然后在本地建立三个 txt 文件,通过 Eclipse 上传到“ /user/hadoop/sort_in”文件夹中,三个 txt 文件的内容如“实例描述”那三个文件一样。如图 2.4-2 所示,成功上传之后。从 SecureCRT 远处查看“ Master.Hadoop”的也能证实我们上传的三个文件。 查看两个文件的内容如图 2.4-3 所示: (2)查看运行结果 这时我们右击 Eclipse 的“ DFS Locations”中“ /user/hadoop”文件夹进行刷新,这时会发现多出一个“ sort_out”文件夹,且里面有 3 个文件,然后打开双其“ part-r-00000”文件,会在 Eclipse 中间把内容显示出来。如图 2.4-4 所示。 本文作者:虾皮 来源:51CTO

优秀的个人博客,低调大师

案例 - 误删千万的表

某天下午,正在给测试库录入数据,看到有截断的warnings,就把表delete掉,重新导入,在做这个导入操作的同时,还在线上给业务做一个update数据的操作,突然之间,误把测试库的secureCRT当做测试的窗口,进行了一个delete操作,2秒后还没删除成功,再看一下表名字,原来是线上业务的表,当场懵逼了 马上ctrl + c 取消delete语句,但因为用的是myshard中间件,索引 ctrl + v 没用有,delete的语句,再就传递到下面挂在的mysql里执行,于是2000万的表就这样被删除了,全球一共9个节点,也因为同步dml sql,而导致所有节点的该表被删除 不过myshard的有一个保护机制,就是每行数据都会加入一个__deleted字段,myshard的delete数据,相当于mysql层面的 update deleted = 1 而已,3天之后,才会真正把数据删掉,目的是防止人为误操作 于是我选择业务使用最多的其中一个节点,进入mysql层面,SET sql_log_bin = 0 ,然后进行 update 表 set deleted=0 where deleted=1 恢复一个节点需要5分钟,把第一个节点恢复以后,马上通知业务,把全球使用改表的业务,都切到已经恢复的那个节点,其他节点先不要使用,于是逐步恢复其他节点,最后一共消耗20分钟把数据恢复回来 当然这5分钟期间,虽然所有节点的这个表都是没有数据,但不一定查询不到数据,因为有redis在扛着,redis的命中率有95%,只有在redis上查询不到数据,接着往DB上查询,才会出现取不到数据的情况 当时以为这样就完事了,于是去开会,开会回来后,业务说数据库的同步有问题,数据不一致了...... 与内核开发的同事调查,发现进行误操作的那个节点的数据,不能顺利同步到其他节点,原因2千万的表,delete语句一下去,就生成了18G的binlog,而myshard的sync同步进程,识别不了超过4G以上的binlog文件,把位置点变成一个负数,而负数位置点的监控是我们监控脚本没有覆盖到的 -rw-rw----.1mysqlmysql101MMay3116:33mysql-bin.270756 -rw-rw----.1mysqlmysql18GMay3116:38mysql-bin.270757 -rw-rw----.1mysqlmysql101MMay3116:38mysql-bin.270758 做法是跳过那个18G的binlog,直接去连下一个binlog,先恢复同步,同步落后了20G,等同步追完以后,大概造成了4个小时的数据延迟,充值延迟导致 这个mysql-bin.270757的二进制日志,用了5分钟生成,其实当时还可以做一个补救措施,就是进入myshard下面的MySQL层面,直接把唯一一条delete语句kill掉,那就可以减少binlog的大小,以及数据的生成时间,不过当时太急没有反应过来 18G的binlog,99%是那条delete SQL生成的row模式的数据文件,但期间还有其他业务正常的SQL,于是使用了大众点评的binlog2sql恢复工作,目的是把二进制文件,解析成具体的SQL语句 使用mysqlbinlog去解析也可以,只不过看不到具体的SQL语句,但业务是需要看到具体的SQL,才能进行恢复,因为比如一些update a = a + 100 类似充值的语句,就不能直接更新了,它是依赖过去的值,如果过去的值都不准确(数据延迟),那么更新回去会出问题的,这样要让业务去衡量怎么处理,所以得把具体的SQL语句提取出来 https://github.com/danfengcao/binlog2sql 其实阿里也有一个binlog解析工具,只不过阿里最后输出的结果是反向解析,比如delete的语句,阿里的工具会生成insert,我的场景只需要提取正常的sql即可,不需要反向解析,于是没有采用 但不管是大众点评,还是阿里的工具,都需要在线去连接数据库的,目的是为了获取information_schema.columns表,去获取表的字段名字,再替换到binlog解析后的@1,@2,@3字段,但我并不想在online的DB上去解析这个18G的binlog,担心消耗cpu资源,我把这个18G的binlog拷贝一个测试库上 想了很久才想到一个方法,就是把测试库,伪装成online的数据库,把测试库的schema都删掉,然后把当前的binlog名字,替换成18G的binlog名字,觉得脑洞有点大,但工具能用了,当时写的脚本关键部分是这样子的 able_array=( 表1 表2 表3 ....... 数据库上所有的表名 ) fortable_namein"${table_array[@]}";do pythonbinlog2sql.py-h127.0.0.1-P端口-u账号-p密码-d数据库--start-file='18G的binlog文件'-t${table_name}>/data1/flashback/"${table_name}".sql done 于是就把18G的sql都解析出来了,比如像这样的格式,有SQL语句,有时间,每条SQL语句占用1行,于是就通知业务,再继续做恢复的事情 INSERTINTO`myshard`.`xxxxxxxxx`(`status`,`actor_uid`,`style`,`ctime`,`__deleted`,`__version`,`sid`,`date`,`utime`,`type`,`uid`)VALUES(0,xxxxxxxxx,0,1496219633,0,xxxxxxxxx,xxxxxxxxx,20170531,1496219633,1,1605227422);#start106192end106420time2017-05-3116:33:53 INSERTINTO`myshard`.`xxxxxxxxx`(`status`,`actor_uid`,`style`,`ctime`,`__deleted`,`__version`,`sid`,`date`,`utime`,`type`,`uid`)VALUES(0,xxxxxxxxx,0,1496219633,0,xxxxxxxxx,xxxxxxxxx,20170531,1496219633,3,1573543992);#start258936end259164time2017-05-3116:33:53 INSERTINTO`myshard`.`xxxxxxxxx`(`status`,`actor_uid`,`style`,`ctime`,`__deleted`,`__version`,`sid`,`date`,`utime`,`type`,`uid`)VALUES(0,xxxxxxxxx,0,1496219634,0,xxxxxxxxx,xxxxxxxxx,20170531,1496219634,3,1509357246);#start473268end473496time2017-05-3116:33:54 INSERTINTO`myshard`.`xxxxxxxxx`(`status`,`actor_uid`,`style`,`ctime`,`__deleted`,`__version`,`sid`,`date`,`utime`,`type`,`uid`)VALUES(0,xxxxxxxxx,0,1496219636,0,xxxxxxxxx,xxxxxxxxx,20170531,1496219636,1,1610811581);#start1114346end1114574time2017-05-3116:33:56 INSERTINTO`myshard`.`xxxxxxxxx`(`status`,`actor_uid`,`style`,`ctime`,`__deleted`,`__version`,`sid`,`date`,`utime`,`type`,`uid`)VALUES(0,xxxxxxxxx,0,1496219636,0,xxxxxxxxx,xxxxxxxxx,20170531,1496219636,1,1593006833);#start1295075end1295303time2017-05-3116:33:56 本次最大的教训是,在执行 delete ,rm ,rsync ,mv ,update的操作之前,要确认好ip,实例,心里要默念个5秒再执行操作,小心谨慎

优秀的个人博客,低调大师

Spark算子总结及案例

spark算子大致上可分三大类算子: 1、Value数据类型的Transformation算子,这种变换不触发提交作业,针对处理的数据项是Value型的数据。 2、Key-Value数据类型的Transformation算子,这种变换不触发提交作业,针对处理的数据项是Key-Value型的数据。 3、Action算子,这类算子会触发SparkContext提交作业。 一、Value型Transformation算子 1)map val a = sc.parallelize(List("dog", "salmon", "salmon", "rat", "elephant"), 3) val b = a.map(_.length) val c = a.zip(b) c.collect res0: Array[(String, Int)] = Array((dog,3), (salmon,6), (salmon,6), (rat,3), (elephant,8)) 2)flatMap val a = sc.parallelize(1 to 10, 5) a.flatMap(1 to _).collect res47: Array[Int] = Array(1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) sc.parallelize(List(1, 2, 3), 2).flatMap(x => List(x, x, x)).collect res85: Array[Int] = Array(1, 1, 1, 2, 2, 2, 3, 3, 3) 3)mapPartiions val x = sc.parallelize(1 to 10, 3) x.flatMap(List.fill(scala.util.Random.nextInt(10))(_)).collect res1: Array[Int] = Array(1, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10) 4)glom(形成一个Array数组) val a = sc.parallelize(1 to 100, 3) a.glom.collect res8: Array[Array[Int]] = Array(Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33), Array(34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66), Array(67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)) 5)union val a = sc.parallelize(1 to 3, 1) val b = sc.parallelize(5 to 7, 1) (a ++ b).collect res0: Array[Int] = Array(1, 2, 3, 5, 6, 7) 6)cartesian(笛卡尔操作) val x = sc.parallelize(List(1,2,3,4,5)) val y = sc.parallelize(List(6,7,8,9,10)) x.cartesian(y).collect res0: Array[(Int, Int)] = Array((1,6), (1,7), (1,8), (1,9), (1,10), (2,6), (2,7), (2,8), (2,9), (2,10), (3,6), (3,7), (3,8), (3,9), (3,10), (4,6), (5,6), (4,7), (5,7), (4,8), (5,8), (4,9), (4,10), (5,9), (5,10)) 7)groupBy(生成相应的key,相同的放在一起) val a = sc.parallelize(1 to 9, 3) a.groupBy(x => { if (x % 2 == 0) "even" else "odd" }).collect res42: Array[(String, Seq[Int])] = Array((even,ArrayBuffer(2, 4, 6, 8)), (odd,ArrayBuffer(1, 3, 5, 7, 9))) 8)filter val a = sc.parallelize(1 to 10, 3) val b = a.filter(_ % 2 == 0) b.collect res3: Array[Int] = Array(2, 4, 6, 8, 10) 9)distinct(去重) val c = sc.parallelize(List("Gnu", "Cat", "Rat", "Dog", "Gnu", "Rat"), 2) c.distinct.collect res6: Array[String] = Array(Dog, Gnu, Cat, Rat) 10)subtract(去掉含有重复的项) val a = sc.parallelize(1 to 9, 3) val b = sc.parallelize(1 to 3, 3) val c = a.subtract(b) c.collect res3: Array[Int] = Array(6, 9, 4, 7, 5, 8) 11)sample val a = sc.parallelize(1 to 10000, 3) a.sample(false, 0.1, 0).count res24: Long = 960 12)takesample val x = sc.parallelize(1 to 1000, 3) x.takeSample(true, 100, 1) res3: Array[Int] = Array(339, 718, 810, 105, 71, 268, 333, 360, 341, 300, 68, 848, 431, 449, 773, 172, 802, 339, 431, 285, 937, 301, 167, 69, 330, 864, 40, 645, 65, 349, 613, 468, 982, 314, 160, 675, 232, 794, 577, 571, 805, 317, 136, 860, 522, 45, 628, 178, 321, 482, 657, 114, 332, 728, 901, 290, 175, 876, 227, 130, 863, 773, 559, 301, 694, 460, 839, 952, 664, 851, 260, 729, 823, 880, 792, 964, 614, 821, 683, 364, 80, 875, 813, 951, 663, 344, 546, 918, 436, 451, 397, 670, 756, 512, 391, 70, 213, 896, 123, 858) 13)cache、persist val c = sc.parallelize(List("Gnu", "Cat", "Rat", "Dog", "Gnu", "Rat"), 2) c.getStorageLevel res0: org.apache.spark.storage.StorageLevel = StorageLevel(false, false, false, false, 1) c.cache c.getStorageLevel res2: org.apache.spark.storage.StorageLevel = StorageLevel(false, true, false, true, 1) 二、Key-Value型Transformation算子 1)mapValues val a = sc.parallelize(List("dog", "tiger", "lion", "cat", "panther", "eagle"), 2) val b = a.map(x => (x.length, x)) b.mapValues("x" + _ + "x").collect res5: Array[(Int, String)] = Array((3,xdogx), (5,xtigerx), (4,xlionx), (3,xcatx), (7,xpantherx), (5,xeaglex)) 2)combineByKey val a = sc.parallelize(List("dog","cat","gnu","salmon","rabbit","turkey","wolf","bear","bee"), 3) val b = sc.parallelize(List(1,1,2,2,2,1,2,2,2), 3) val c = b.zip(a) val d = c.combineByKey(List(_), (x:List[String], y:String) => y :: x, (x:List[String], y:List[String]) => x ::: y) d.collect res16: Array[(Int, List[String])] = Array((1,List(cat, dog, turkey)), (2,List(gnu, rabbit, salmon, bee, bear, wolf))) 3)reduceByKey val a = sc.parallelize(List("dog", "cat", "owl", "gnu", "ant"), 2) val b = a.map(x => (x.length, x)) b.reduceByKey(_ + _).collect res86: Array[(Int, String)] = Array((3,dogcatowlgnuant)) val a = sc.parallelize(List("dog", "tiger", "lion", "cat", "panther", "eagle"), 2) val b = a.map(x => (x.length, x)) b.reduceByKey(_ + _).collect res87: Array[(Int, String)] = Array((4,lion), (3,dogcat), (7,panther), (5,tigereagle)) 4)partitionBy (对RDD进行分区操作) 5)cogroup val a = sc.parallelize(List(1, 2, 1, 3), 1) val b = a.map((_, "b")) val c = a.map((_, "c")) b.cogroup(c).collect res7: Array[(Int, (Iterable[String], Iterable[String]))] = Array( (2,(ArrayBuffer(b),ArrayBuffer(c))), (3,(ArrayBuffer(b),ArrayBuffer(c))), (1,(ArrayBuffer(b, b),ArrayBuffer(c, c))) ) 6)join val a = sc.parallelize(List("dog", "salmon", "salmon", "rat", "elephant"), 3) val b = a.keyBy(_.length) val c = sc.parallelize(List("dog","cat","gnu","salmon","rabbit","turkey","wolf","bear","bee"), 3) val d = c.keyBy(_.length) b.join(d).collect res0: Array[(Int, (String, String))] = Array((6,(salmon,salmon)), (6,(salmon,rabbit)), (6,(salmon,turkey)), (6,(salmon,salmon)), (6,(salmon,rabbit)), (6,(salmon,turkey)), (3,(dog,dog)), (3,(dog,cat)), (3,(dog,gnu)), (3,(dog,bee)), (3,(rat,dog)), (3,(rat,cat)), (3,(rat,gnu)), (3,(rat,bee))) 7)leftOutJoin val a = sc.parallelize(List("dog", "salmon", "salmon", "rat", "elephant"), 3) val b = a.keyBy(_.length) val c = sc.parallelize(List("dog","cat","gnu","salmon","rabbit","turkey","wolf","bear","bee"), 3) val d = c.keyBy(_.length) b.leftOuterJoin(d).collect res1: Array[(Int, (String, Option[String]))] = Array((6,(salmon,Some(salmon))), (6,(salmon,Some(rabbit))), (6,(salmon,Some(turkey))), (6,(salmon,Some(salmon))), (6,(salmon,Some(rabbit))), (6,(salmon,Some(turkey))), (3,(dog,Some(dog))), (3,(dog,Some(cat))), (3,(dog,Some(gnu))), (3,(dog,Some(bee))), (3,(rat,Some(dog))), (3,(rat,Some(cat))), (3,(rat,Some(gnu))), (3,(rat,Some(bee))), (8,(elephant,None))) 8)rightOutJoin val a = sc.parallelize(List("dog", "salmon", "salmon", "rat", "elephant"), 3) val b = a.keyBy(_.length) val c = sc.parallelize(List("dog","cat","gnu","salmon","rabbit","turkey","wolf","bear","bee"), 3) val d = c.keyBy(_.length) b.rightOuterJoin(d).collect res2: Array[(Int, (Option[String], String))] = Array((6,(Some(salmon),salmon)), (6,(Some(salmon),rabbit)), (6,(Some(salmon),turkey)), (6,(Some(salmon),salmon)), (6,(Some(salmon),rabbit)), (6,(Some(salmon),turkey)), (3,(Some(dog),dog)), (3,(Some(dog),cat)), (3,(Some(dog),gnu)), (3,(Some(dog),bee)), (3,(Some(rat),dog)), (3,(Some(rat),cat)), (3,(Some(rat),gnu)), (3,(Some(rat),bee)), (4,(None,wolf)), (4,(None,bear))) 三、Actions算子 1)foreach val c = sc.parallelize(List("cat", "dog", "tiger", "lion", "gnu", "crocodile", "ant", "whale", "dolphin", "spider"), 3) c.foreach(x => println(x + "s are yummy")) lions are yummy gnus are yummy crocodiles are yummy ants are yummy whales are yummy dolphins are yummy spiders are yummy 2)saveAsTextFile val a = sc.parallelize(1 to 10000, 3) a.saveAsTextFile("mydata_a") 14/04/03 21:11:36 INFO FileOutputCommitter: Saved output of task 'attempt_201404032111_0000_m_000002_71' to file:/home/cloudera/Documents/spark-0.9.0-incubating-bin-cdh4/bin/mydata_a 3)saveAsObjectFile val x = sc.parallelize(1 to 100, 3) x.saveAsObjectFile("objFile") val y = sc.objectFile[Int]("objFile") y.collect res52: Array[Int] = Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100) 4)collect val c = sc.parallelize(List("Gnu", "Cat", "Rat", "Dog", "Gnu", "Rat"), 2) c.collect res29: Array[String] = Array(Gnu, Cat, Rat, Dog, Gnu, Rat) 5)collectAsMap val a = sc.parallelize(List(1, 2, 1, 3), 1) val b = a.zip(a) b.collectAsMap res1: scala.collection.Map[Int,Int] = Map(2 -> 2, 1 -> 1, 3 -> 3) 6)reduceByKeyLocally val a = sc.parallelize(List("dog", "cat", "owl", "gnu", "ant"), 2) val b = a.map(x => (x.length, x)) b.reduceByKey(_ + _).collect res86: Array[(Int, String)] = Array((3,dogcatowlgnuant)) 7)lookup val a = sc.parallelize(List("dog", "tiger", "lion", "cat", "panther", "eagle"), 2) val b = a.map(x => (x.length, x)) b.lookup(5) res0: Seq[String] = WrappedArray(tiger, eagle) 8)count val c = sc.parallelize(List("Gnu", "Cat", "Rat", "Dog"), 2) c.count res2: Long = 4 9)top val c = sc.parallelize(Array(6, 9, 4, 7, 5, 8), 2) c.top(2) res28: Array[Int] = Array(9, 8) 10)reduce val a = sc.parallelize(1 to 100, 3) a.reduce(_ + _) res41: Int = 5050 11)fold val a = sc.parallelize(List(1,2,3), 3) a.fold(0)(_ + _) res59: Int = 6 12)aggregate val z = sc.parallelize(List(1,2,3,4,5,6), 2) // lets first print out the contents of the RDD with partition labels def myfunc(index: Int, iter: Iterator[(Int)]) : Iterator[String] = { iter.toList.map(x => "[partID:" + index + ", val: " + x + "]").iterator } z.mapPartitionsWithIndex(myfunc).collect res28: Array[String] = Array([partID:0, val: 1], [partID:0, val: 2], [partID:0, val: 3], [partID:1, val: 4], [partID:1, val: 5], [partID:1, val: 6]) z.aggregate(0)(math.max(_, _), _ + _) res40: Int = 9 参考:http://homepage.cs.latrobe.edu.au/zhe/ZhenHeSparkRDDAPIExamples.html当神已无能为力,那便是魔渡众生

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

用户登录
用户注册