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 ...
随机推荐
- Spring 的Controller 是单例or多例
Spring 的Controller 是单例or多例 你什么也不肯放弃,又得到了什么? 背景:今天写代码遇到一个Controller 中的线程安全问题,那么Spring 的Controller 是单例 ...
- quartz表(11张)
/* Navicat Premium Data Transfer Source Server : 本地连接 Source Server Type : MySQL Source Server Versi ...
- 通过DatabaseMetaData数据库元信息类,获取特定数据库的元信息
数据库版本:mysql8.0.18 ide:idea 2019.3 可以看到代码中连接的数据库为course_select,是一个学生的选课系统的数据库 然后通过DatabaseMetaData的ge ...
- vs使用fscanf和fprintf错误警告处理
严重性代码说明项目文件行 禁止显示状态错误 C4996 fopen('fscanf'.strcmp):This function or variable may be unsafe. 最全解决办法(转 ...
- 文件属性及find命令总结
第1章 文件属性 1.1 文件的属性 1.1.1 查看文件的详细属性 PS:ls查看的文件或目录默认的是按照名字的第一个字母进行正序排序 ls 参数选项: -t ...
- Java 获取一段时间内的每一天
有时候我们会遇到一些业务场景,需要去获取一段时间内的每一天日期 public static List<Date> findDates(Date dBegin, Date dEnd) { L ...
- 【深入理解Java虚拟机】Java虚拟机运行时数据区
Java虚拟机运行时数据区 线程私有 程序计数器 1.当前线程所执行的字节码的行号指示器. 2.唯一不会发生OutOfMemoryError的区域 3.如果执行的是java方法,计数器值为虚拟机字节码 ...
- MySql 错误:In aggregated query without GROUP BY, expression #1 of SELECT list contains....
前段时间做sql注入的时候 使用group_concat时,出现标题上的错误.经查阅一位大佬的博客,成功解决!故写此博文! 当mysql的sql_mode是only_full_group_by的时候 ...
- 挂载磁盘不成功显示mount: /mnt: wrong fs type, bad option, bad superblock..............
[23:25:32 root@8 ~]#mount /dev/sdb2 /mntmount: /mnt: wrong fs type, bad option, bad superblock on /d ...
- 3D坐标系
在3D渲染中,首先要确定的就是坐标系,坐标系根据实际情况,分为两种: 左手坐标系 右手坐标系 在3D空间中,这两个坐标系是没有办法重合到一起的: Unity使用的坐标系为左手坐标系. 在确定左手坐标系 ...