0

I started using spark-kernel recently.

As given in tutorial and sample code, I was able to set up client and use it for executing code snippets on spark-kernel and retrieving back results as given in this example code.

Now, I need to use comm API provided with spark-kernel. I tried this tutorial, but I am not able to make it work. In fact, I have no understanding of how to make that work.

I tried the following code, but when I run this code, I get this error "Received invalid target for Comm Open: my_target" on the kernel.

package examples
import scala.runtime.ScalaRunTime._
import scala.collection.mutable.ListBuffer
import com.ibm.spark.kernel.protocol.v5.MIMEType
import com.ibm.spark.kernel.protocol.v5.client.boot.ClientBootstrap
import com.ibm.spark.kernel.protocol.v5.client.boot.layers.{StandardHandlerInitialization, StandardSystemInitialization}
import com.ibm.spark.kernel.protocol.v5.content._
import com.typesafe.config.{Config, ConfigFactory}
import Array._

object commclient extends App{
val profileJSON: String = """
{
      "stdin_port" : 48691,
      "control_port" : 44808,
      "hb_port" : 49691,
      "shell_port" : 40544,
      "iopub_port" : 43462,
      "ip" : "127.0.0.1",
      "transport" : "tcp",
      "signature_scheme" : "hmac-sha256",
      "key" : ""
}
""".stripMargin

val config: Config = ConfigFactory.parseString(profileJSON)
val client = (new ClientBootstrap(config)
    with StandardSystemInitialization
    with StandardHandlerInitialization).createClient()

def printResult(result: ExecuteResult) = {
    println(s"${result.data.get(MIMEType.PlainText).get}")
}
def printStreamContent(content:StreamContent) = {
    println(s"${content.text}")
}
def printError(reply:ExecuteReplyError) = {
    println(s"Error was: ${reply.ename.get}")
}

client.comm.register("my_target").addMsgHandler {
(commWriter, commId, data) =>
    println(data)
    commWriter.close()
}

// Initiate the Comm connection
client.comm.open("my_target")

}

Can someone tell me how shall I run this piece of code:

// Register the callback to respond to being opened from the client
kernel.comm.register("my target").addOpenHandler { 
    (commWriter, commId, targetName, data) =>
        commWriter.writeMsg(Map("response" -> "Hello World!"))
}

I would really appreciate if someone can point me to complete working example on usage of comm API.

Any help will be appreciated. Thanks

1 Answers1

0

You can use your client to run this server (kernel) side registration once in one program. Then your other programs can communicate to kernel using this channel. Here is a way I ran my registration in the first program I mentioned above:

client.execute(
"""
// Register the callback to respond to being opened from the client
kernel.comm.register("my target").
    addOpenHandler {
        (commWriter, commId, targetName, data) =>
            commWriter.writeMsg(org.apache.toree.kernel.protocol.v5.MsgData("response" -> "Toree Hello World!"))
    }.
    addMsgHandler {
        (commWriter, _, data) =>
            if (!data.toString.contains("closing")) {
                commWriter.writeMsg(data)
            } else {
                commWriter.writeMsg(org.apache.toree.kernel.protocol.v5.MsgData("closing" -> "done"))
            }
    }
""".stripMargin
)
Pei
  • 1