spark textfile rdd 日记
批量处理模板方法, 核心处理方法为内部方法
def batchProces(sc: SparkContext, locationFlag: Int, minid: Int, maxid: Int, numPartitions: Int) = {
//自定义RDD,此处为demo
val dataRDD = sc.makeRDD(List(1, 2), numPartitions)
dataRDD.mapPartitions(iterator => {
val rawData = iterator.toList
var lstT = new ListBuffer[(Int, Int)]()
rawData.foreach(v => {
if (lstT.size < 50) {
lstT.append((v, 1))
} else {
//每50处理一次
procesData()
}
})
//剩余的继续处理
procesData()
//批量处理逻辑
def procesData() = {
//核心处理逻辑
// doProcess
//很重要
lstT.clear()
}
lstT.iterator
}).map((_, 1)).reduceByKey(_ + _).sortByKey().saveAsTextFile("hdfs://hdfscluster/tmp/logs/")
}
批量处理模板方法, 核心处理方法为外部方法
def process_outer(lst: List[(Int, Int)]) = {
//外部核心处理逻辑,如Request请求等
RequestUtil.postJson("http://xxx", "{paraData}", 1000)
}
def batchProces_processOuter(sc: SparkContext, locationFlag: Int, minid: Int, maxid: Int, numPartitions: Int) = {
val fooCount = sc.longAccumulator("fooCount")
//自定义RDD,此处为demo
val dataRDD = sc.makeRDD(List(1, 2), numPartitions)
dataRDD.foreachPartition(iterator => {
val rawData = iterator.toList
var lstT = new ListBuffer[(Int, Int)]()
rawData.foreach(v => {
if (lstT.size < 50) {
lstT.append((v, 1))
} else {
//每50处理一次
process_outer(lstT.toList)
fooCount.add(lstT.size)
lstT.clear()
}
})
//剩余的继续处理
if (lstT.size > 0) {
process_outer(lstT.toList)
fooCount.add(lstT.size)
lstT.clear()
}
});
println("total =>" + fooCount.value)
}
针对文本文件RDD的一些处理逻辑:
//针对单个文件,每行数据超长的情况, 先对行进行拆分,再重新分区,将数据交给多个executor去执行
def bigLine(sc: SparkContext, locationFlag: Int, minid: Int, maxid: Int, numPartitions: Int) = {
val fileRDD = sc.textFile("hdfs://hdfscluster/tmp/logs/abc.txt", numPartitions) //对于长文本, 先拆分,然后重新分区,提高并发机器利用率, 减少job执行时间
fileRDD.flatMap(_.split(",")).repartition(24).foreach(println(_))
} //针对无规律零散路径,循环内部使用sc
def handlerPath_lingsan(sc: SparkContext, locationFlag: Int, minid: Int, maxid: Int, numPartitions: Int, filep: String) = {
val rawPath: List[String] = List("hdfs://hdfscluster/tmp1/path1", "hdfs://hdfscluster/tmp2/path2", "hdfs://hdfscluster/tmp3/path3")
val lsResult = rawPath.flatMap(v => {
sc.textFile(v).map((_, 1)).collect().toList
}).toList.foreach(println(_))
} //针对文件夹,
def handlerPath_directroy(sc: SparkContext, locationFlag: Int, minid: Int, maxid: Int, numPartitions: Int, filep: String) = {
//按行输出指定文件夹下所有文件,分区有效
val txtRDD = sc.textFile("hdfs://hdfscluster/tmp1/*", numPartitions)
//重新分区,便于输出结果
txtRDD.map((_, 1)).repartition(1)
.saveAsTextFile("hdfs://hdfscluster/tmp/logs/ssoHot3")
} //针对文件夹,且路径下文件数量比较多且比较小的情况
def handlerPath_directroy(sc: SparkContext, locationFlag: Int, minid: Int, maxid: Int, numPartitions: Int, filep: String) = { //返回结果key=文件路径,val=文件内容, 如果content太大的话,容易造成OOM
val dirRDD = sc.wholeTextFiles("hdfs://hdfscluster/tmp1/*", numPartitions)
dirRDD.flatMap(v => {
v._2.split(System.lineSeparator()).map((_, 1))
}).repartition(1).saveAsTextFile("hdfs://hdfscluster/tmp/logs/ssoHot3") }
//java scala转换
def java_scala_collection_convert = {
var lstT = new ListBuffer[Int]()
//注意java,scala转换
import scala.collection.JavaConverters._
val lstBack = SensitiveDevice.batchDecrypt(lstT.toList.asJava).asScala
}
spark textfile rdd 日记的更多相关文章
- [Spark] Spark的RDD编程
本篇博客中的操作都在 ./bin/pyspark 中执行. RDD,即弹性分布式数据集(Resilient Distributed Dataset),是Spark对数据的核心抽象.RDD是分布式元素的 ...
- 理解Spark的RDD
RDD是个抽象类,定义了诸如map().reduce()等方法,但实际上继承RDD的派生类一般只要实现两个方法: def getPartitions: Array[Partition] def com ...
- spark中RDD的转化操作和行动操作
本文主要是讲解spark里RDD的基础操作.RDD是spark特有的数据模型,谈到RDD就会提到什么弹性分布式数据集,什么有向无环图,本文暂时不去展开这些高深概念,在阅读本文时候,大家可以就把RDD当 ...
- Spark核心RDD、什么是RDD、RDD的属性、创建RDD、RDD的依赖以及缓存、
1:什么是Spark的RDD??? RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行 ...
- [转]Spark学习之路 (三)Spark之RDD
Spark学习之路 (三)Spark之RDD https://www.cnblogs.com/qingyunzong/p/8899715.html 目录 一.RDD的概述 1.1 什么是RDD? ...
- Spark学习之路 (三)Spark之RDD
一.RDD的概述 1.1 什么是RDD? RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素 ...
- Spark的RDD原理以及2.0特性的介绍
转载自:http://www.tuicool.com/articles/7VNfyif 王联辉,曾在腾讯,Intel 等公司从事大数据相关的工作.2013 年 - 2016 年先后负责腾讯 Yarn ...
- Spark之 RDD转换成DataFrame的Scala实现
依赖 <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2. ...
- Spark之 RDD
简介 RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行计算的集合. Resilien ...
随机推荐
- 关于windou环境下使用http或者ftp搭建网络hu共享
第一步 右键此电脑进入控制面 第二步:进入程序点击启用或关闭windous功能 第三步进入服务开启界面 点击让windows更新为你下载文件,并保存更改完,然后关闭 四:邮件我的电脑进入管理 四右键添 ...
- Python协程之asyncio
asyncio 是 Python 中的异步IO库,用来编写并发协程,适用于IO阻塞且需要大量并发的场景,例如爬虫.文件读写. asyncio 在 Python3.4 被引入,经过几个版本的迭代,特性. ...
- Kubernetes Operator基础入门
本文转自Rancher Labs 你是否曾经想过SRE团队是如何有效地成功管理复杂的应用?在Kubernetes生态系统中,Kubernetes Operator可以给你答案.在本文中,我们将研究Op ...
- python3笔记-列表
列表去重的两种方式: # 创建列表放数据 a =[1,2,1,4,2] b=[1,3,4,3,1,3] d=[] for i in a: if i not in d: d.append(i) prin ...
- 【jmespath】—3. 进阶 Object Projections
继续,来看Object Projections. 一.Object Projections 上面说的是列表投影,只适用于列表.那么对于json对象,可以用对象投影. 投影最终返回的仍然是个列表,只不过 ...
- 使用java.io.RandomAccessFile更改文本文件的内容
原文本内容: 2020-01-21 08:31:08.520 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-21 08:31:08.521 [main] ER ...
- 深入理解 JVM 的内存区域
深入理解运行时数据区 代码示例: 1. JVM 向操作系统申请内存: JVM 第一步就是通过配置参数或者默认配置参数向操作系统申请内存空间,根据内存大小找到具体的内存分配表,然后把内存段的起始地址和终 ...
- whlie do-whlie
switch语句 用于根据多个不同条件执行不同动作. while 循环 while循环基本语法: 条件初始化; while(条件表达式){ //条件表达式就是判 ...
- SpringMVC-08-SpringMVC层编写
SpringMVC层编写 web.xml DispatcherServlet <!--DispatcherServlet--> <servlet> <servlet-na ...
- sudo提权,ansible批量给所有主机创建系统账户,授权
sudo(superuser or another do)让普通用户可以以超级管理员或其他人的身份执行命令. sudo基本流程如下: 1.管理员需要先授权(修改/etc/sudoers文件) 2.普通 ...