Spark Strcutured Streaming中使用Dataset的groupBy agg 与 join 示例(java api)
Dataset的groupBy agg示例
Dataset<Row> resultDs = dsParsed
.groupBy("enodeb_id", "ecell_id")
.agg(
functions.first("scan_start_time").alias("scan_start_time1"),
functions.first("insert_time").alias("insert_time1"),
functions.first("mr_type").alias("mr_type1"),
functions.first("mr_ltescphr").alias("mr_ltescphr1"),
functions.first("mr_ltescpuschprbnum").alias("mr_ltescpuschprbnum1"),
functions.count("enodeb_id").alias("rows1"))
.selectExpr(
"ecell_id",
"enodeb_id",
"scan_start_time1 as scan_start_time",
"insert_time1 as insert_time",
"mr_type1 as mr_type",
"mr_ltescphr1 as mr_ltescphr",
"mr_ltescpuschprbnum1 as mr_ltescpuschprbnum",
"rows1 as rows");
Dataset Join示例:
Dataset<Row> ncRes = sparkSession.read().option("delimiter", "|").option("header", true).csv("/user/csv");
Dataset<Row> mro=sparkSession.sql("。。。"); Dataset<Row> ncJoinMro = ncRes
.join(mro, mro.col("id").equalTo(ncRes.col("id")).and(mro.col("calid").equalTo(ncRes.col("calid"))), "left_outer")
.select(ncRes.col("id").as("int_id"),
mro.col("vendor_id"),
。。。
);
join condition另外一种方式:
leftDfWithWatermark.join(rightDfWithWatermark,
expr(""" leftDfId = rightDfId AND leftDfTime >= rightDfTime AND leftDfTime <= rightDfTime + interval 1 hour"""),
joinType = "leftOuter" )
BroadcastHashJoin示例:
package com.dx.testbroadcast; import org.apache.spark.SparkConf;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.functions; import java.io.*; public class Test {
public static void main(String[] args) {
String personPath = "E:\\person.csv";
String personOrderPath = "E:\\personOrder.csv";
//writeToPersion(personPath);
//writeToPersionOrder(personOrderPath); SparkConf conf = new SparkConf();
SparkSession sparkSession = SparkSession.builder().config(conf).appName("test-broadcast-app").master("local[*]").getOrCreate(); Dataset<Row> person = sparkSession.read()
.option("header", "true")
.option("inferSchema", "true") //是否自动推到内容的类型
.option("delimiter", ",").csv(personPath).as("person");
person.printSchema(); Dataset<Row> personOrder = sparkSession.read()
.option("header", "true")
.option("inferSchema", "true") //是否自动推到内容的类型
.option("delimiter", ",").csv(personOrderPath).as("personOrder");
personOrder.printSchema(); // Default `inner`. Must be one of:`inner`, `cross`, `outer`, `full`, `full_outer`, `left`, `left_outer`,`right`, `right_outer`, `left_semi`, `left_anti`.
Dataset<Row> resultDs = personOrder.join(functions.broadcast(person), personOrder.col("personid").equalTo(person.col("id")),"left");
resultDs.explain();
resultDs.show(10);
} private static void writeToPersion(String personPath) {
BufferedWriter personWriter = null;
try {
personWriter = new BufferedWriter(new FileWriter(personPath));
personWriter.write("id,name,age,address\r\n");
for (int i = ; i < ; i++) {
personWriter.write("" + i + ",person-" + i + "," + i + ",address-address-address-address-address-address-address" + i + "\r\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (personWriter != null) {
try {
personWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} private static void writeToPersionOrder(String personOrderPath) {
BufferedWriter personWriter = null;
try {
personWriter = new BufferedWriter(new FileWriter(personOrderPath));
personWriter.write("personid,name,age,address\r\n");
for (int i = ; i < ; i++) {
personWriter.write("" + i + ",person-" + i + "," + i + ",address-address-address-address-address-address-address" + i + "\r\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (personWriter != null) {
try {
personWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
打印结果:
== Physical Plan ==
*() BroadcastHashJoin [personid#], [id#], LeftOuter, BuildRight
:- *() FileScan csv [personid#,name#,age#,address#] Batched: false, Format: CSV, Location: InMemoryFileIndex[file:/E:/personOrder.csv], PartitionFilters: [], PushedFilters: [], ReadSchema: struct<personid:int,name:string,age:int,address:string>
+- BroadcastExchange HashedRelationBroadcastMode(List(cast(input[, int, true] as bigint)))
+- *() Project [id#, name#, age#, address#]
+- *() Filter isnotnull(id#)
+- *() FileScan csv [id#,name#,age#,address#] Batched: false, Format: CSV, Location: InMemoryFileIndex[file:/E:/person.csv], PartitionFilters: [], PushedFilters: [IsNotNull(id)], ReadSchema: struct<id:int,name:string,age:int,address:string> +--------+--------+---+--------------------+---+--------+---+--------------------+
|personid| name|age| address| id| name|age| address|
+--------+--------+---+--------------------+---+--------+---+--------------------+
| |person-| |address-address-a...| |person-| |address-address-a...|
| |person-| |address-address-a...| |person-| |address-address-a...|
| |person-| |address-address-a...| |person-| |address-address-a...|
| |person-| |address-address-a...| |person-| |address-address-a...|
| |person-| |address-address-a...| |person-| |address-address-a...|
| |person-| |address-address-a...| |person-| |address-address-a...|
| |person-| |address-address-a...| |person-| |address-address-a...|
| |person-| |address-address-a...| |person-| |address-address-a...|
| |person-| |address-address-a...| |person-| |address-address-a...|
| |person-| |address-address-a...| |person-| |address-address-a...|
+--------+--------+---+--------------------+---+--------+---+--------------------+
only showing top rows
SparkSQL Broadcast HashJoin
person.createOrReplaceTempView("temp_person");
personOrder.createOrReplaceTempView("temp_person_order"); Dataset<Row> sqlResult = sparkSession.sql(
" SELECT /*+ BROADCAST (t11) */" +
" t11.id,t11.name,t11.age,t11.address," +
" t10.personid as person_id,t10.name as persion_order_name" +
" FROM temp_person_order as t10 " +
" inner join temp_person as t11" +
" on t11.id = t10.personid ");
sqlResult.show();
sqlResult.explain();
打印日志
+---+--------+---+--------------------+---------+------------------+
| id| name|age| address|person_id|persion_order_name|
+---+--------+---+--------------------+---------+------------------+
| |person-| |address-address-a...| | person-|
| |person-| |address-address-a...| | person-|
| |person-| |address-address-a...| | person-|
| |person-| |address-address-a...| | person-|
| |person-| |address-address-a...| | person-|
| |person-| |address-address-a...| | person-|
| |person-| |address-address-a...| | person-|
| |person-| |address-address-a...| | person-|
| |person-| |address-address-a...| | person-|
| |person-| |address-address-a...| | person-|
+---+--------+---+--------------------+---------+------------------+
only showing top rows // :: INFO FileSourceStrategy: Pruning directories with:
// :: INFO FileSourceStrategy: Post-Scan Filters: isnotnull(personid#)
// :: INFO FileSourceStrategy: Output Data Schema: struct<personid: int, name: string>
// :: INFO FileSourceScanExec: Pushed Filters: IsNotNull(personid)
// :: INFO FileSourceStrategy: Pruning directories with:
// :: INFO FileSourceStrategy: Post-Scan Filters: isnotnull(id#)
// :: INFO FileSourceStrategy: Output Data Schema: struct<id: int, name: string, age: int, address: string ... more fields>
// :: INFO FileSourceScanExec: Pushed Filters: IsNotNull(id)
== Physical Plan ==
*() Project [id#, name#, age#, address#, personid# AS person_id#, name# AS persion_order_name#]
+- *() BroadcastHashJoin [personid#], [id#], Inner, BuildRight
:- *() Project [personid#, name#]
: +- *() Filter isnotnull(personid#)
: +- *() FileScan csv [personid#,name#] Batched: false, Format: CSV, Location: InMemoryFileIndex[file:/E:/personOrder.csv], PartitionFilters: [], PushedFilters: [IsNotNull(personid)], ReadSchema: struct<personid:int,name:string>
+- BroadcastExchange HashedRelationBroadcastMode(List(cast(input[, int, true] as bigint)))
+- *() Project [id#, name#, age#, address#]
+- *() Filter isnotnull(id#)
+- *() FileScan csv [id#,name#,age#,address#] Batched: false, Format: CSV, Location: InMemoryFileIndex[file:/E:/person.csv], PartitionFilters: [], PushedFilters: [IsNotNull(id)], ReadSchema: struct<id:int,name:string,age:int,address:string>
// :: INFO SparkContext: Invoking stop() from shutdown hook
Spark Strcutured Streaming中使用Dataset的groupBy agg 与 join 示例(java api)的更多相关文章
- Java中访问控制修饰符的详解和示例——Java学习
Java中的四个访问控制修饰符 简述 在Java中共有四个: public -- 对外部完全可见 protected -- 对本包和所有子类可见 默认(不需要修饰符)-- 对本包可见 private ...
- Spark(十六)DataSet
Spark最吸引开发者的就是简单易用.跨语言(Scala, Java, Python, and R)的API. 本文主要讲解Apache Spark 2.0中RDD,DataFrame和Dataset ...
- Spark2.2(三十三):Spark Streaming和Spark Structured Streaming更新broadcast总结(一)
背景: 需要在spark2.2.0更新broadcast中的内容,网上也搜索了不少文章,都在讲解spark streaming中如何更新,但没有spark structured streaming更新 ...
- Spark2.3(三十四):Spark Structured Streaming之withWaterMark和windows窗口是否可以实现最近一小时统计
WaterMark除了可以限定来迟数据范围,是否可以实现最近一小时统计? WaterMark目的用来限定参数计算数据的范围:比如当前计算数据内max timestamp是12::00,waterMar ...
- Spark Streaming中的操作函数分析
根据Spark官方文档中的描述,在Spark Streaming应用中,一个DStream对象可以调用多种操作,主要分为以下几类 Transformations Window Operations J ...
- Spark2.3(三十五)Spark Structured Streaming源代码剖析(从CSDN和Github中看到别人分析的源代码的文章值得收藏)
从CSDN中读取到关于spark structured streaming源代码分析不错的几篇文章 spark源码分析--事件总线LiveListenerBus spark事件总线的核心是LiveLi ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十六)Structured Streaming中ForeachSink的用法
Structured Streaming默认支持的sink类型有File sink,Foreach sink,Console sink,Memory sink. ForeachWriter实现: 以写 ...
- Spark Streaming中的操作函数讲解
Spark Streaming中的操作函数讲解 根据根据Spark官方文档中的描述,在Spark Streaming应用中,一个DStream对象可以调用多种操作,主要分为以下几类 Transform ...
- Spark2.x(六十一):在Spark2.4 Structured Streaming中Dataset是如何执行加载数据源的?
本章主要讨论,在Spark2.4 Structured Streaming读取kafka数据源时,kafka的topic数据是如何被执行的过程进行分析. 以下边例子展开分析: SparkSession ...
随机推荐
- 导入导出CSV
const string dataPath = @"D:\Users\jin_h\Documents\Visual Studio 2013\Projects\ConsoleApplicati ...
- CentOS下多网卡绑定多IP段时导致只有一个会通的问题解决
原因:Linux默认开启了反向路由检查导致的,比如说外面访问eth0的网卡,而网关在eth1上,又或者从eth0出的流量,而网关在eth1上,此时会检查到网关不在同一个网卡上导致出不去,进不来的问题. ...
- bash编程之xargs实用技巧
xargs结合管道操作符|,可以完成很多看似复杂的问题: 1.快速删除所有.log日志文件 机器运行久了,就会有各式各样的日志文件,散落在各个目录下,可以利用下面的方法: find ./ -name ...
- Lodop客户端本地和集中打印 [是否安装][操作系统]
前面有些博文或图片介绍了这两种方式,可能文字太多,耐心看完的人不多,这里简略描述+图片的方式解释一下. 客户端本地打印: 用户访问某网站,用自己本地的打印机打出来. 集中打印方式: 用户访问某网页,用 ...
- DTrace Probes in HotSpot VM
http://docs.oracle.com/javase/6/docs/technotes/guides/vm/dtrace.html
- Xamarin.Forms.Xaml.XamlParseException: MarkupExtension not found for trans:Translate using a PCL in Release Mode
I'm pretty desperate finding the solution for the problem stated below. I have a cross platform solu ...
- C++学习笔记23,类内函数重载
该博文仅用于交流学习.请慎用于不论什么商业用途,本博主保留对该博文的一切权利. 博主博客:http://blog.csdn.net/qq844352155 转载请注明出处: 在一个类内,最常见的就是构 ...
- 使用Axure RP原型设计实践03,制作一个登录界面的原型
本篇体验做一个登录界面的原型. 登录页 首先在Page Style里为页面设置背景色. 如果想在页面中加图片,就把Image部件拖入页面,并设置x和y轴.双击页面中的Image部件可以导入图片.在Im ...
- 窗体的Alpha通道透明色支持
参考: http://www.delphibbs.com/delphibbs/dispq.asp?lid=2190768 Windows 2000后,为了支持类似MAC界面的Alpha通道混合效果,提 ...
- 支持xp风格的manifest
MSDN 和一些网站上的manifest 有问题 , 自己修改了一下加上Microsoft.VC80.DebugCRT 和 Microsoft.VC80.DebugMFC 就可以了.如果是relea ...