{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/35552522","id":35552522,"diff_hunk":"@@ -684,3 +679,53 @@ private class KafkaUtilsPythonHelper {\n     kafkaRDD.offsetRanges.toSeq\n   }\n }\n+\n+private object KafkaUtilsPythonHelper {\n+  private var initialized = false\n+\n+  def initialize(): Unit = {\n+    SerDeUtil.initialize()\n+    synchronized {\n+      if (!initialized) {\n+        new PythonMessageAndMetadataPickler().register()\n+        initialized = true\n+      }\n+    }\n+  }\n+  // will not called in Executor automatically\n+  initialize()\n+\n+  case class PythonMessageAndMetadata(\n+      topic: String,\n+      partition: JInt,\n+      offset: JLong,\n+      key: Array[Byte],\n+      message: Array[Byte])\n+\n+  class PythonMessageAndMetadataPickler extends IObjectPickler {\n+    private val module = \"pyspark.streaming.kafka\"\n+\n+    def register(): Unit = {\n+      Pickler.registerCustomPickler(classOf[PythonMessageAndMetadata], this)\n+      Pickler.registerCustomPickler(this.getClass, this)\n+    }\n+\n+    def pickle(obj: Object, out: OutputStream, pickler: Pickler) {\n+      if (obj == this) {\n+        out.write(Opcodes.GLOBAL)\n+        out.write(s\"$module\\nKafkaMessageAndMetadata\\n\".getBytes(\"utf-8\"))","path":"external/kafka/src/main/scala/org/apache/spark/streaming/kafka/KafkaUtils.scala","position":251,"original_position":251,"commit_id":"a09423ed6ce4ab28d8e52e4ba0a8d80d1ae098a1","original_commit_id":"a09423ed6ce4ab28d8e52e4ba0a8d80d1ae098a1","user":{"login":"zsxwing","id":1000778,"avatar_url":"https://avatars.githubusercontent.com/u/1000778?v=3","gravatar_id":"","url":"https://api.github.com/users/zsxwing","html_url":"https://github.com/zsxwing","followers_url":"https://api.github.com/users/zsxwing/followers","following_url":"https://api.github.com/users/zsxwing/following{/other_user}","gists_url":"https://api.github.com/users/zsxwing/gists{/gist_id}","starred_url":"https://api.github.com/users/zsxwing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zsxwing/subscriptions","organizations_url":"https://api.github.com/users/zsxwing/orgs","repos_url":"https://api.github.com/users/zsxwing/repos","events_url":"https://api.github.com/users/zsxwing/events{/privacy}","received_events_url":"https://api.github.com/users/zsxwing/received_events","type":"User","site_admin":false},"body":"Nit: could you import `com.google.common.base.Charsets.UTF_8` and use `UTF_8` here?","created_at":"2015-07-27T15:57:35Z","updated_at":"2015-07-27T15:57:35Z","html_url":"https://github.com/apache/spark/pull/7410#discussion_r35552522","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/7410","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/35552522"},"html":{"href":"https://github.com/apache/spark/pull/7410#discussion_r35552522"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/7410"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/7410","id":39984599,"html_url":"https://github.com/apache/spark/pull/7410","diff_url":"https://github.com/apache/spark/pull/7410.diff","patch_url":"https://github.com/apache/spark/pull/7410.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/7410","number":7410,"state":"open","locked":false,"title":"[SPARK-9065][Streaming][PySpark] Add MessageHandler for Kafka Python API","user":{"login":"jerryshao","id":850797,"avatar_url":"https://avatars.githubusercontent.com/u/850797?v=3","gravatar_id":"","url":"https://api.github.com/users/jerryshao","html_url":"https://github.com/jerryshao","followers_url":"https://api.github.com/users/jerryshao/followers","following_url":"https://api.github.com/users/jerryshao/following{/other_user}","gists_url":"https://api.github.com/users/jerryshao/gists{/gist_id}","starred_url":"https://api.github.com/users/jerryshao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jerryshao/subscriptions","organizations_url":"https://api.github.com/users/jerryshao/orgs","repos_url":"https://api.github.com/users/jerryshao/repos","events_url":"https://api.github.com/users/jerryshao/events{/privacy}","received_events_url":"https://api.github.com/users/jerryshao/received_events","type":"User","site_admin":false},"body":"Propose a way to add messageHandler for Kafka Python API, offer user a similar pattern to use message like Scala/Java:\r\n\r\n```python\r\n\r\ndef getOffset(m):\r\n    return m and m.offset\r\n  \r\nKafkaUtils.createDirectStream(ssc, topic, kafkaParams, messageHandler=getOffset)\r\n```\r\n\r\nInternally serialize the Kafka `MessageAndMetadata` to Python using Pickler.\r\n\r\nPlease help to review, thanks a lot.\r\n","created_at":"2015-07-15T03:12:20Z","updated_at":"2015-07-27T15:57:35Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e4c1264633535504c3b5ffca95b93a2ea469f8e6","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/7410/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/7410/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/7410/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/a09423ed6ce4ab28d8e52e4ba0a8d80d1ae098a1","head":{"label":"jerryshao:SPARK-8337","ref":"SPARK-8337","sha":"a09423ed6ce4ab28d8e52e4ba0a8d80d1ae098a1","user":{"login":"jerryshao","id":850797,"avatar_url":"https://avatars.githubusercontent.com/u/850797?v=3","gravatar_id":"","url":"https://api.github.com/users/jerryshao","html_url":"https://github.com/jerryshao","followers_url":"https://api.github.com/users/jerryshao/followers","following_url":"https://api.github.com/users/jerryshao/following{/other_user}","gists_url":"https://api.github.com/users/jerryshao/gists{/gist_id}","starred_url":"https://api.github.com/users/jerryshao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jerryshao/subscriptions","organizations_url":"https://api.github.com/users/jerryshao/orgs","repos_url":"https://api.github.com/users/jerryshao/repos","events_url":"https://api.github.com/users/jerryshao/events{/privacy}","received_events_url":"https://api.github.com/users/jerryshao/received_events","type":"User","site_admin":false},"repo":{"id":18259371,"name":"apache-spark","full_name":"jerryshao/apache-spark","owner":{"login":"jerryshao","id":850797,"avatar_url":"https://avatars.githubusercontent.com/u/850797?v=3","gravatar_id":"","url":"https://api.github.com/users/jerryshao","html_url":"https://github.com/jerryshao","followers_url":"https://api.github.com/users/jerryshao/followers","following_url":"https://api.github.com/users/jerryshao/following{/other_user}","gists_url":"https://api.github.com/users/jerryshao/gists{/gist_id}","starred_url":"https://api.github.com/users/jerryshao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jerryshao/subscriptions","organizations_url":"https://api.github.com/users/jerryshao/orgs","repos_url":"https://api.github.com/users/jerryshao/repos","events_url":"https://api.github.com/users/jerryshao/events{/privacy}","received_events_url":"https://api.github.com/users/jerryshao/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jerryshao/apache-spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/jerryshao/apache-spark","forks_url":"https://api.github.com/repos/jerryshao/apache-spark/forks","keys_url":"https://api.github.com/repos/jerryshao/apache-spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jerryshao/apache-spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jerryshao/apache-spark/teams","hooks_url":"https://api.github.com/repos/jerryshao/apache-spark/hooks","issue_events_url":"https://api.github.com/repos/jerryshao/apache-spark/issues/events{/number}","events_url":"https://api.github.com/repos/jerryshao/apache-spark/events","assignees_url":"https://api.github.com/repos/jerryshao/apache-spark/assignees{/user}","branches_url":"https://api.github.com/repos/jerryshao/apache-spark/branches{/branch}","tags_url":"https://api.github.com/repos/jerryshao/apache-spark/tags","blobs_url":"https://api.github.com/repos/jerryshao/apache-spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jerryshao/apache-spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jerryshao/apache-spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/jerryshao/apache-spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jerryshao/apache-spark/statuses/{sha}","languages_url":"https://api.github.com/repos/jerryshao/apache-spark/languages","stargazers_url":"https://api.github.com/repos/jerryshao/apache-spark/stargazers","contributors_url":"https://api.github.com/repos/jerryshao/apache-spark/contributors","subscribers_url":"https://api.github.com/repos/jerryshao/apache-spark/subscribers","subscription_url":"https://api.github.com/repos/jerryshao/apache-spark/subscription","commits_url":"https://api.github.com/repos/jerryshao/apache-spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/jerryshao/apache-spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/jerryshao/apache-spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/jerryshao/apache-spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/jerryshao/apache-spark/contents/{+path}","compare_url":"https://api.github.com/repos/jerryshao/apache-spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jerryshao/apache-spark/merges","archive_url":"https://api.github.com/repos/jerryshao/apache-spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jerryshao/apache-spark/downloads","issues_url":"https://api.github.com/repos/jerryshao/apache-spark/issues{/number}","pulls_url":"https://api.github.com/repos/jerryshao/apache-spark/pulls{/number}","milestones_url":"https://api.github.com/repos/jerryshao/apache-spark/milestones{/number}","notifications_url":"https://api.github.com/repos/jerryshao/apache-spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jerryshao/apache-spark/labels{/name}","releases_url":"https://api.github.com/repos/jerryshao/apache-spark/releases{/id}","created_at":"2014-03-30T07:04:45Z","updated_at":"2014-07-09T01:42:23Z","pushed_at":"2015-07-24T08:40:49Z","git_url":"git://github.com/jerryshao/apache-spark.git","ssh_url":"git@github.com:jerryshao/apache-spark.git","clone_url":"https://github.com/jerryshao/apache-spark.git","svn_url":"https://github.com/jerryshao/apache-spark","homepage":null,"size":142394,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"52de3acca4ce8c36fd4c9ce162473a091701bbc7","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":17165658,"name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/spark","description":"Mirror of Apache Spark","fork":false,"url":"https://api.github.com/repos/apache/spark","forks_url":"https://api.github.com/repos/apache/spark/forks","keys_url":"https://api.github.com/repos/apache/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/spark/teams","hooks_url":"https://api.github.com/repos/apache/spark/hooks","issue_events_url":"https://api.github.com/repos/apache/spark/issues/events{/number}","events_url":"https://api.github.com/repos/apache/spark/events","assignees_url":"https://api.github.com/repos/apache/spark/assignees{/user}","branches_url":"https://api.github.com/repos/apache/spark/branches{/branch}","tags_url":"https://api.github.com/repos/apache/spark/tags","blobs_url":"https://api.github.com/repos/apache/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/spark/languages","stargazers_url":"https://api.github.com/repos/apache/spark/stargazers","contributors_url":"https://api.github.com/repos/apache/spark/contributors","subscribers_url":"https://api.github.com/repos/apache/spark/subscribers","subscription_url":"https://api.github.com/repos/apache/spark/subscription","commits_url":"https://api.github.com/repos/apache/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/spark/contents/{+path}","compare_url":"https://api.github.com/repos/apache/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/spark/merges","archive_url":"https://api.github.com/repos/apache/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/spark/downloads","issues_url":"https://api.github.com/repos/apache/spark/issues{/number}","pulls_url":"https://api.github.com/repos/apache/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/spark/labels{/name}","releases_url":"https://api.github.com/repos/apache/spark/releases{/id}","created_at":"2014-02-25T08:00:08Z","updated_at":"2015-07-27T15:54:45Z","pushed_at":"2015-07-27T15:30:08Z","git_url":"git://github.com/apache/spark.git","ssh_url":"git@github.com:apache/spark.git","clone_url":"https://github.com/apache/spark.git","svn_url":"https://github.com/apache/spark","homepage":null,"size":1580100,"stargazers_count":4871,"watchers_count":4871,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4393,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":356,"forks":4393,"open_issues":356,"watchers":4871,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/7410"},"html":{"href":"https://github.com/apache/spark/pull/7410"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/7410"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/7410/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/7410/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/7410/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/a09423ed6ce4ab28d8e52e4ba0a8d80d1ae098a1"}}}}