当前位置:网站首页>Spark 算子之filter使用
Spark 算子之filter使用
2022-04-23 15:45:00 【逆风飞翔的小叔】
前言
filter,可以理解为过滤,直观来说,就是对一组数据按照指定的规则做过滤,filter这个算子在Java或者其他语言中多有使用,能够很方便的帮我们从一组数据中过滤得到期望的数据;
函数签名
def filter(f: T => Boolean ): RDD[T]
函数说明
将数据根据指定的规则进行筛选过滤,符合规则的数据保留,不符合规则的数据丢弃。 当数据进行筛选过滤后,分区不变,但是分区内的数据可能不均衡,生产环境下,可能会出现 数据倾斜;
案例一,从一组数据中过滤出偶数
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
object Filter_Test {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
val sc = new SparkContext(sparkConf)
val rdd = sc.makeRDD(List(1,2,3,4,5,6))
val result = rdd.filter(
item => item % 2 ==0
)
result.collect().foreach(println)
sc.stop()
}
}
运行这段代码,观察控制台输出结果
案例二,从日志文件中过滤出2015年5月17的数据
日志文件内容如下:
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
object Filter_Test {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
val sc = new SparkContext(sparkConf)
val rdd: RDD[String] = sc.textFile("E:\\code-self\\spi\\datas\\apache.log")
rdd.filter(
line =>{
val datas = line.split(" ")
val time = datas(3)
time.contains("17/05/2015")
}
).collect().foreach(println)
sc.stop()
}
}
运行上面的代码,观察控制台输出结果,
版权声明
本文为[逆风飞翔的小叔]所创,转载请带上原文链接,感谢
https://blog.csdn.net/congge_study/article/details/124355911
边栏推荐
- Multi level cache usage
- 一刷314-剑指 Offer 09. 用两个栈实现队列(e)
- Special analysis of China's digital technology in 2022
- PHP 的运算符
- Cookie&Session
- Single architecture system re architecture
- How do you think the fund is REITs? Is it safe to buy the fund through the bank
- C language --- advanced pointer
- php函数
- The El tree implementation only displays a certain level of check boxes and selects radio
猜你喜欢
ICE -- 源码分析
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
What if the server is poisoned? How does the server prevent virus intrusion?
Neodynamic Barcode Professional for WPF V11. 0
For examination
IronPDF for . NET 2022.4.5455
移动金融(自用)
Treatment of idempotency
大厂技术实现 | 行业解决方案系列教程
Cap theorem
随机推荐
负载均衡器
通过 PDO ODBC 将 PHP 连接到 MSSQL
php函数
字符串最后一个单词的长度
Calculate the number of occurrences of a character
提取不重复的整数
Named in pytoch_ parameters、named_ children、named_ Modules function
Pgpool II 4.3 Chinese Manual - introductory tutorial
Control structure (I)
gps北斗高精度卫星时间同步系统应用案例
PHP operators
Application of Bloom filter in 100 million flow e-commerce system
PHP classes and objects
Go concurrency and channel
CVPR 2022 优质论文分享
MySQL optimistic lock to solve concurrency conflict
vim指定行注释和解注释
utils.DeprecatedIn35 因升级可能取消,该如何办
王启亨谈Web3.0与价值互联网“通证交换”
c语言---指针进阶