基本用法主要掌握一点就行:

master slave模式运用:driver 就是master,executor就是slave。

如果executor要想和driver交互必须拿到driver的EndpointRef,通过driver的EndpointRef来调接口访问。

driver启动时,会在driver中注册一个Endpoint服务,并暴露自己的ip和端口。executor端生成driver的EndpointRef,就主要需要两个参数就行:driver的host(ip)和port。

导入Maven依赖

        <dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.4.0</version>
</dependency>

定义RPC Server端的ip(localhost)。port(57992)、服务名称(hello-rpc-service)

object HelloRpcSettings {
val rpcName = "hello-rpc-service"
val port = 57992
val hostname="localhost" def getName() = {
rpcName
} def getPort(): Int = {
port
} def getHostname():String={
hostname
}
}

定义RPC的Endpoint类和发送数据类SayHi/SayBye

case class SayHi(msg: String)

case class SayBye(msg: String)

import org.apache.spark.rpc.{RpcCallContext, RpcEndpoint, RpcEnv}

class HelloEndpoint(override val rpcEnv: RpcEnv) extends RpcEndpoint {
override def onStart(): Unit = {
println(rpcEnv.address)
println("start hello endpoint")
} override def receive: PartialFunction[Any, Unit] = {
case SayHi(msg) =>
println(s"receive $msg" )
} override def receiveAndReply(context: RpcCallContext): PartialFunction[Any, Unit] = {
case SayHi(msg) => {
println(s"receive $msg")
context.reply(s"hi, $msg")
}
case SayBye(msg) => {
println(s"receive $msg")
context.reply(s"bye, $msg")
}
} override def onStop(): Unit = {
println("stop hello endpoint")
}
}

定义RPC 服务提供者

import org.apache.spark.{SecurityManager, SparkConf, SparkContext, SparkEnv}
import org.apache.spark.rpc._
import org.apache.spark.sql.SparkSession object RpcServerTest {
def main(args: Array[String]): Unit = {
val conf: SparkConf = new SparkConf()
val sparkSession = SparkSession.builder().config(conf).master("local[*]").appName("test rpc").getOrCreate()
val sparkContext: SparkContext = sparkSession.sparkContext
val sparkEnv: SparkEnv = sparkContext.env val rpcEnv = RpcEnv.create(HelloRpcSettings.getName(), HelloRpcSettings.getHostname(), HelloRpcSettings.getHostname(), HelloRpcSettings.getPort(), conf,
sparkEnv.securityManager, 1, false) val helloEndpoint: RpcEndpoint = new HelloEndpoint(rpcEnv)
rpcEnv.setupEndpoint(HelloRpcSettings.getName(), helloEndpoint) rpcEnv.awaitTermination()
}
}

定义RPC服务使用者

import org.apache.spark.{SecurityManager, SparkConf, SparkContext, SparkEnv}
import org.apache.spark.rpc.{RpcAddress, RpcEndpointRef, RpcEnv, RpcEnvConfig}
import org.apache.spark.sql.{Dataset, Row, SparkSession} import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future} object RpcClientTest {
def main(args: Array[String]): Unit = {
val conf: SparkConf = new SparkConf()
val sparkSession = SparkSession.builder().config(conf).master("local[*]").appName("test rpc").getOrCreate()
val sparkContext: SparkContext = sparkSession.sparkContext
val sparkEnv: SparkEnv = sparkContext.env val rpcEnv: RpcEnv = RpcEnv.create(HelloRpcSettings.getName(),HelloRpcSettings.getHostname(),HelloRpcSettings.getPort(),conf,sparkEnv.securityManager,false)
val endPointRef: RpcEndpointRef = rpcEnv.setupEndpointRef(RpcAddress(HelloRpcSettings.getHostname(), HelloRpcSettings.getPort()), HelloRpcSettings.getName()) import scala.concurrent.ExecutionContext.Implicits.global endPointRef.send(SayHi("test send")) val future: Future[String] = endPointRef.ask[String](SayHi("neo"))
future.onComplete {
case scala.util.Success(value) => println(s"Got the result = $value")
case scala.util.Failure(e) => println(s"Got error: $e")
}
Await.result(future, Duration.apply("30s")) val res = endPointRef.askSync[String](SayBye("test askSync"))
println(res) sparkSession.stop()
} }

启动RPC 服务提供者

Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
// :: INFO SparkContext: Running Spark version 2.4.
// :: WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
// :: INFO SparkContext: Submitted application: test rpc
// :: INFO SecurityManager: Changing view acls to: boco
// :: INFO SecurityManager: Changing modify acls to: boco
// :: INFO SecurityManager: Changing view acls groups to:
// :: INFO SecurityManager: Changing modify acls groups to:
// :: INFO SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users with view permissions: Set(boco); groups with view permissions: Set(); users with modify permissions: Set(boco); groups with modify permissions: Set()
// :: INFO Utils: Successfully started service 'sparkDriver' on port .
// :: INFO SparkEnv: Registering MapOutputTracker
// :: INFO SparkEnv: Registering BlockManagerMaster
// :: INFO BlockManagerMasterEndpoint: Using org.apache.spark.storage.DefaultTopologyMapper for getting topology information
// :: INFO BlockManagerMasterEndpoint: BlockManagerMasterEndpoint up
// :: INFO DiskBlockManager: Created local directory at C:\Users\boco\AppData\Local\Temp\blockmgr-7128dde8-9c46--bb72-c2161ba65bf7
// :: INFO MemoryStore: MemoryStore started with capacity 901.8 MB
// :: INFO SparkEnv: Registering OutputCommitCoordinator
// :: INFO Utils: Successfully started service 'SparkUI' on port .
// :: INFO SparkUI: Bound SparkUI to 0.0.0.0, and started at http://DESKTOP-JL4FSCV:4040
// :: INFO Executor: Starting executor ID driver on host localhost
// :: INFO Utils: Successfully started service 'org.apache.spark.network.netty.NettyBlockTransferService' on port .
// :: INFO NettyBlockTransferService: Server created on DESKTOP-JL4FSCV:
// :: INFO BlockManager: Using org.apache.spark.storage.RandomBlockReplicationPolicy for block replication policy
// :: INFO BlockManagerMaster: Registering BlockManager BlockManagerId(driver, DESKTOP-JL4FSCV, , None)
// :: INFO BlockManagerMasterEndpoint: Registering block manager DESKTOP-JL4FSCV: with 901.8 MB RAM, BlockManagerId(driver, DESKTOP-JL4FSCV, , None)
// :: INFO BlockManagerMaster: Registered BlockManager BlockManagerId(driver, DESKTOP-JL4FSCV, , None)
// :: INFO BlockManager: Initialized BlockManager: BlockManagerId(driver, DESKTOP-JL4FSCV, , None)
// :: INFO Utils: Successfully started service 'hello-rpc-service' on port .
localhost:
start hello endpoint

启动RPC 服务使用者

Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
// :: INFO SparkContext: Running Spark version 2.4.
// :: WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
// :: INFO SparkContext: Submitted application: test rpc
// :: INFO SecurityManager: Changing view acls to: boco
// :: INFO SecurityManager: Changing modify acls to: boco
// :: INFO SecurityManager: Changing view acls groups to:
// :: INFO SecurityManager: Changing modify acls groups to:
// :: INFO SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users with view permissions: Set(boco); groups with view permissions: Set(); users with modify permissions: Set(boco); groups with modify permissions: Set()
// :: INFO Utils: Successfully started service 'sparkDriver' on port .
// :: INFO SparkEnv: Registering MapOutputTracker
// :: INFO SparkEnv: Registering BlockManagerMaster
// :: INFO BlockManagerMasterEndpoint: Using org.apache.spark.storage.DefaultTopologyMapper for getting topology information
// :: INFO BlockManagerMasterEndpoint: BlockManagerMasterEndpoint up
// :: INFO DiskBlockManager: Created local directory at C:\Users\boco\AppData\Local\Temp\blockmgr-6a0b8e7f-86d2-4bb8-b45c-7c04deabcb91
// :: INFO MemoryStore: MemoryStore started with capacity 901.8 MB
// :: INFO SparkEnv: Registering OutputCommitCoordinator
// :: WARN Utils: Service 'SparkUI' could not bind on port . Attempting port .
// :: INFO Utils: Successfully started service 'SparkUI' on port .
// :: INFO SparkUI: Bound SparkUI to 0.0.0.0, and started at http://DESKTOP-JL4FSCV:4041
// :: INFO Executor: Starting executor ID driver on host localhost
// :: INFO Utils: Successfully started service 'org.apache.spark.network.netty.NettyBlockTransferService' on port .
// :: INFO NettyBlockTransferService: Server created on DESKTOP-JL4FSCV:
// :: INFO BlockManager: Using org.apache.spark.storage.RandomBlockReplicationPolicy for block replication policy
// :: INFO BlockManagerMaster: Registering BlockManager BlockManagerId(driver, DESKTOP-JL4FSCV, , None)
// :: INFO BlockManagerMasterEndpoint: Registering block manager DESKTOP-JL4FSCV: with 901.8 MB RAM, BlockManagerId(driver, DESKTOP-JL4FSCV, , None)
// :: INFO BlockManagerMaster: Registered BlockManager BlockManagerId(driver, DESKTOP-JL4FSCV, , None)
// :: INFO BlockManager: Initialized BlockManager: BlockManagerId(driver, DESKTOP-JL4FSCV, , None)
// :: WARN Utils: Service 'hello-rpc-service' could not bind on port . Attempting port .
// :: INFO Utils: Successfully started service 'hello-rpc-service' on port .
// :: INFO TransportClientFactory: Successfully created connection to localhost/127.0.0.1: after ms ( ms spent in bootstraps)
bye, test askSync
Got the result = hi, neo
// :: INFO SparkUI: Stopped Spark web UI at http://DESKTOP-JL4FSCV:4041
// :: INFO MapOutputTrackerMasterEndpoint: MapOutputTrackerMasterEndpoint stopped!
// :: INFO MemoryStore: MemoryStore cleared
// :: INFO BlockManager: BlockManager stopped
// :: INFO BlockManagerMaster: BlockManagerMaster stopped
// :: INFO OutputCommitCoordinator$OutputCommitCoordinatorEndpoint: OutputCommitCoordinator stopped!
// :: INFO SparkContext: Successfully stopped SparkContext
// :: INFO ShutdownHookManager: Shutdown hook called
// :: INFO ShutdownHookManager: Deleting directory

此时 RPC 服务提供者打印信息如下:

receive test send
receive neo
receive test askSync
// :: WARN TransportChannelHandler: Exception in connection from /127.0.0.1:
java.io.IOException: 远程主机强迫关闭了一个现有的连接。
at sun.nio.ch.SocketDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:)
at sun.nio.ch.IOUtil.read(IOUtil.java:)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:)
at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:)
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:)
at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:)
at io.netty.util.concurrent.SingleThreadEventExecutor$.run(SingleThreadEventExecutor.java:)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:)
at java.lang.Thread.run(Thread.java:)

Spark(五十三):Spark RPC初尝试使用的更多相关文章

  1. Spark学习之路 (五)Spark伪分布式安装

    一.JDK的安装 JDK使用root用户安装 1.1 上传安装包并解压 [root@hadoop1 soft]# tar -zxvf jdk-8u73-linux-x64.tar.gz -C /usr ...

  2. Spark(五) -- Spark Streaming介绍与基本执行过程

    Spark Streaming作为Spark上的四大子框架之一,肩负着实时流计算的重大责任 而相对于另外一个当下十分流行的实时流计算处理框架Storm,Spark Streaming有何优点?又有何不 ...

  3. Spark2.2(三十三):Spark Streaming和Spark Structured Streaming更新broadcast总结(一)

    背景: 需要在spark2.2.0更新broadcast中的内容,网上也搜索了不少文章,都在讲解spark streaming中如何更新,但没有spark structured streaming更新 ...

  4. Spark入门(五)--Spark的reduce和reduceByKey

    reduce和reduceByKey的区别 reduce和reduceByKey是spark中使用地非常频繁的,在字数统计中,可以看到reduceByKey的经典使用.那么reduce和reduceB ...

  5. 【Spark 内核】 Spark 内核解析-上

    Spark内核泛指Spark的核心运行机制,包括Spark核心组件的运行机制.Spark任务调度机制.Spark内存管理机制.Spark核心功能的运行原理等,熟练掌握Spark内核原理,能够帮助我们更 ...

  6. 【Spark 内核】 Spark 内核解析-下

    Spark内核泛指Spark的核心运行机制,包括Spark核心组件的运行机制.Spark任务调度机制.Spark内存管理机制.Spark核心功能的运行原理等,熟练掌握Spark内核原理,能够帮助我们更 ...

  7. 初步了解Spark生态系统及Spark Streaming

    一.        场景 ◆ Spark[4]: Scope:  a MapReduce-like cluster computing framework designed for low-laten ...

  8. R语言爬虫初尝试-基于RVEST包学习

    注意:这文章是2月份写的,拉勾网早改版了,代码已经失效了,大家意思意思就好,主要看代码的使用方法吧.. 最近一直在用且有维护的另一个爬虫是KINDLE 特价书爬虫,blog地址见此: http://w ...

  9. 【译】Spark官方文档——Spark Configuration(Spark配置)

    注重版权,尊重他人劳动 转帖注明原文地址:http://www.cnblogs.com/vincent-hv/p/3316502.html   Spark主要提供三种位置配置系统: 环境变量:用来启动 ...

随机推荐

  1. 报错:failed to get the task for process XXX(解决方案)

    引文: iOS真机调试程序,报如下错误信息: 原因: 证书问题,project和targets的证书都必须是开发证书,ADHOC的证书会出现此问题. 解决方案: project和targets的证书使 ...

  2. zookeeper介绍(4)zookeeper的完整分布式

    参考: zookeeper的单机和伪分布式教程请参考:zookeeper介绍(1)zookeeper介绍与安装 Zookeeper的完整分布式集群搭建: 准备好三台centos主机:(在这我使用的是z ...

  3. ACAG 0x01-4 最短Hamilton路径

    ACAG 0x01-4 最短Hamilton路径 论为什么书上标程跑不过这道题-- 首先,这道题与今年CSP-S2的D1T3有着异曲同工之妙,那就是--都有$O(n!)$的做法!(大雾) 这道题的正解 ...

  4. PHP-FPM的知识点

    https://blog.csdn.net/resilient/article/details/82420863 这个URL,将php的各种模式与知识点说清楚了. 因为php-fpm默认编译进了php ...

  5. Proxmox初步了解

    Proxmox不分主从,所有节点同步信息 创建集群 pvecm(可通过web界面创建.添加至集群) pvecm create cluster01 pvecm status 添加节点 pvecm add ...

  6. Markdwon入门2

    插入表情 这里是指广义的表情包,包括表情.物体.动物等. :+1: :smile: :s :scream: :kissing_heart: :yum: :cry: :blush: :frog: :co ...

  7. Wiki with Herbal Medicine

    Problem H. Wiki with Herbal MedicineInput file: standard input Time limit: 1 secondOutput file: stan ...

  8. 【笔记】ROS常用命令

    环境相关 查看当前环境下包含的包路径echo $ROS_PACKAGE_PATH查看包含的包的路径roscd package TF树相关 查看所有坐标系的状态rosrun tf tf_monitor ...

  9. PHP截取字符串函数substr()函数实例用法详解

    在PHP中有一项非常重要的技术,就是截取指定字符串中指定长度的字符.PHP对于字符串截取可以使用PHP预定义函数substr()函数来实现.下面就来介绍一下substr()函数的语法及其应用. sub ...

  10. js解决大文件断点续传

    最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...