{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/103056768","pull_request_review_id":23836927,"id":103056768,"diff_hunk":"@@ -0,0 +1,119 @@\n+/*\n+ * Licensed to the Apache Software Foundation (ASF) under one or more\n+ * contributor license agreements.  See the NOTICE file distributed with\n+ * this work for additional information regarding copyright ownership.\n+ * The ASF licenses this file to You under the Apache License, Version 2.0\n+ * (the \"License\"); you may not use this file except in compliance with\n+ * the License.  You may obtain a copy of the License at\n+ *\n+ *    http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package org.apache.spark.sql.kafka010\n+\n+import java.{util => ju}\n+\n+import org.apache.kafka.clients.producer._\n+import org.apache.kafka.common.serialization.ByteArraySerializer\n+\n+import org.apache.spark.sql.catalyst.InternalRow\n+import org.apache.spark.sql.catalyst.expressions.{Attribute, Cast, Literal, UnsafeProjection}\n+import org.apache.spark.sql.types.{BinaryType, StringType}\n+import org.apache.spark.unsafe.types.UTF8String\n+\n+/**\n+ * A simple trait for writing out data in a single Spark task, without any concerns about how\n+ * to commit or abort tasks. Exceptions thrown by the implementation of this class will\n+ * automatically trigger task aborts.\n+ */\n+private[kafka010] class KafkaWriteTask(\n+    producerConfiguration: ju.Map[String, Object],\n+    inputSchema: Seq[Attribute],\n+    topic: Option[String]) {\n+  // used to synchronize with Kafka callbacks\n+  @volatile var failedWrite: Exception = null\n+  val topicExpression =\n+    if (topic.isDefined) {\n+      // topic option overrides topic field\n+      Literal(UTF8String.fromString(topic.get), StringType)\n+    } else {\n+    inputSchema.find(p =>\n+      p.name == KafkaWriter.TOPIC_ATTRIBUTE_NAME).getOrElse(\n+        throw new IllegalStateException(s\"topic option required when no \" +\n+          s\"'${KafkaWriter.TOPIC_ATTRIBUTE_NAME}' attribute is present\"))\n+    }\n+  val keyExpression = inputSchema.find(p =>\n+    p.name == KafkaWriter.KEY_ATTRIBUTE_NAME).getOrElse(\n+      Literal(null, BinaryType)\n+    )\n+  keyExpression.dataType match {\n+    case StringType | BinaryType => // good\n+    case t =>\n+      throw new IllegalStateException(s\"${KafkaWriter.KEY_ATTRIBUTE_NAME} \" +\n+        s\"attribute unsupported type $t\")\n+  }\n+  val valueExpression = inputSchema.find(p =>\n+    p.name == KafkaWriter.VALUE_ATTRIBUTE_NAME).getOrElse(\n+      throw new IllegalStateException(s\"Required attribute \" +\n+        s\"'${KafkaWriter.VALUE_ATTRIBUTE_NAME}' not found\")\n+    )\n+  valueExpression.dataType match {\n+    case StringType | BinaryType => // good\n+    case t =>\n+      throw new IllegalStateException(s\"${KafkaWriter.VALUE_ATTRIBUTE_NAME} \" +\n+        s\"attribute unsupported type $t\")\n+  }\n+  val projection = UnsafeProjection.create(\n+    Seq(topicExpression, Cast(keyExpression, BinaryType),\n+      Cast(valueExpression, BinaryType)), inputSchema)\n+\n+  // Create a Kafka Producer\n+  producerConfiguration.put(\"key.serializer\", classOf[ByteArraySerializer].getName)\n+  producerConfiguration.put(\"value.serializer\", classOf[ByteArraySerializer].getName)\n+  val producer = new KafkaProducer[Array[Byte], Array[Byte]](producerConfiguration)","path":"external/kafka-0-10-sql/src/main/scala/org/apache/spark/sql/kafka010/KafkaWriteTask.scala","position":79,"original_position":79,"commit_id":"e6b6dc1035588dfdb8d4a16e2004ad3e0c838adb","original_commit_id":"e6b6dc1035588dfdb8d4a16e2004ad3e0c838adb","user":{"login":"tdas","id":663212,"avatar_url":"https://avatars.githubusercontent.com/u/663212?v=3","gravatar_id":"","url":"https://api.github.com/users/tdas","html_url":"https://github.com/tdas","followers_url":"https://api.github.com/users/tdas/followers","following_url":"https://api.github.com/users/tdas/following{/other_user}","gists_url":"https://api.github.com/users/tdas/gists{/gist_id}","starred_url":"https://api.github.com/users/tdas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tdas/subscriptions","organizations_url":"https://api.github.com/users/tdas/orgs","repos_url":"https://api.github.com/users/tdas/repos","events_url":"https://api.github.com/users/tdas/events{/privacy}","received_events_url":"https://api.github.com/users/tdas/received_events","type":"User","site_admin":false},"body":"In addition, i think we should not create producer until its used, since the creation of KafkaWriterTask is outside the try. If code is moved around, it may so happen that an error in the KafkaWriterTask constructor *after the producer* is created will end up escaping the try-catch and the producer would never close.","created_at":"2017-02-24T23:47:22Z","updated_at":"2017-02-24T23:47:22Z","html_url":"https://github.com/apache/spark/pull/17043#discussion_r103056768","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/17043","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/103056768"},"html":{"href":"https://github.com/apache/spark/pull/17043#discussion_r103056768"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/17043"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/17043","id":107720817,"html_url":"https://github.com/apache/spark/pull/17043","diff_url":"https://github.com/apache/spark/pull/17043.diff","patch_url":"https://github.com/apache/spark/pull/17043.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/17043","number":17043,"state":"open","locked":false,"title":"[SPARK-19719][SS][WIP] Kafka writer for both structured streaming and batch queires","user":{"login":"tcondie","id":1696558,"avatar_url":"https://avatars.githubusercontent.com/u/1696558?v=3","gravatar_id":"","url":"https://api.github.com/users/tcondie","html_url":"https://github.com/tcondie","followers_url":"https://api.github.com/users/tcondie/followers","following_url":"https://api.github.com/users/tcondie/following{/other_user}","gists_url":"https://api.github.com/users/tcondie/gists{/gist_id}","starred_url":"https://api.github.com/users/tcondie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tcondie/subscriptions","organizations_url":"https://api.github.com/users/tcondie/orgs","repos_url":"https://api.github.com/users/tcondie/repos","events_url":"https://api.github.com/users/tcondie/events{/privacy}","received_events_url":"https://api.github.com/users/tcondie/received_events","type":"User","site_admin":false},"body":"## What changes were proposed in this pull request?\r\n\r\nAdd a new Kafka Sink and Kafka Relation for writing streaming and batch queries, respectively, to Apache Kafka. \r\n\r\n@tdas @zsxwing \r\n\r\n## How was this patch tested?\r\n\r\nKafkaSinkSuite.scala contains tests for writing both streaming and batch queries to Kafka.\r\n\r\nPlease review http://spark.apache.org/contributing.html before opening a pull request.\r\n","created_at":"2017-02-23T21:50:38Z","updated_at":"2017-02-24T23:47:22Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1cf40b2e181767078e9f021e7a4c7a7a4e371821","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/17043/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/17043/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/17043/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/e6b6dc1035588dfdb8d4a16e2004ad3e0c838adb","head":{"label":"tcondie:kafka-writer","ref":"kafka-writer","sha":"e6b6dc1035588dfdb8d4a16e2004ad3e0c838adb","user":{"login":"tcondie","id":1696558,"avatar_url":"https://avatars.githubusercontent.com/u/1696558?v=3","gravatar_id":"","url":"https://api.github.com/users/tcondie","html_url":"https://github.com/tcondie","followers_url":"https://api.github.com/users/tcondie/followers","following_url":"https://api.github.com/users/tcondie/following{/other_user}","gists_url":"https://api.github.com/users/tcondie/gists{/gist_id}","starred_url":"https://api.github.com/users/tcondie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tcondie/subscriptions","organizations_url":"https://api.github.com/users/tcondie/orgs","repos_url":"https://api.github.com/users/tcondie/repos","events_url":"https://api.github.com/users/tcondie/events{/privacy}","received_events_url":"https://api.github.com/users/tcondie/received_events","type":"User","site_admin":false},"repo":{"id":71503469,"name":"spark","full_name":"tcondie/spark","owner":{"login":"tcondie","id":1696558,"avatar_url":"https://avatars.githubusercontent.com/u/1696558?v=3","gravatar_id":"","url":"https://api.github.com/users/tcondie","html_url":"https://github.com/tcondie","followers_url":"https://api.github.com/users/tcondie/followers","following_url":"https://api.github.com/users/tcondie/following{/other_user}","gists_url":"https://api.github.com/users/tcondie/gists{/gist_id}","starred_url":"https://api.github.com/users/tcondie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tcondie/subscriptions","organizations_url":"https://api.github.com/users/tcondie/orgs","repos_url":"https://api.github.com/users/tcondie/repos","events_url":"https://api.github.com/users/tcondie/events{/privacy}","received_events_url":"https://api.github.com/users/tcondie/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/tcondie/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/tcondie/spark","forks_url":"https://api.github.com/repos/tcondie/spark/forks","keys_url":"https://api.github.com/repos/tcondie/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tcondie/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tcondie/spark/teams","hooks_url":"https://api.github.com/repos/tcondie/spark/hooks","issue_events_url":"https://api.github.com/repos/tcondie/spark/issues/events{/number}","events_url":"https://api.github.com/repos/tcondie/spark/events","assignees_url":"https://api.github.com/repos/tcondie/spark/assignees{/user}","branches_url":"https://api.github.com/repos/tcondie/spark/branches{/branch}","tags_url":"https://api.github.com/repos/tcondie/spark/tags","blobs_url":"https://api.github.com/repos/tcondie/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tcondie/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tcondie/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/tcondie/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tcondie/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/tcondie/spark/languages","stargazers_url":"https://api.github.com/repos/tcondie/spark/stargazers","contributors_url":"https://api.github.com/repos/tcondie/spark/contributors","subscribers_url":"https://api.github.com/repos/tcondie/spark/subscribers","subscription_url":"https://api.github.com/repos/tcondie/spark/subscription","commits_url":"https://api.github.com/repos/tcondie/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/tcondie/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/tcondie/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/tcondie/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/tcondie/spark/contents/{+path}","compare_url":"https://api.github.com/repos/tcondie/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tcondie/spark/merges","archive_url":"https://api.github.com/repos/tcondie/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tcondie/spark/downloads","issues_url":"https://api.github.com/repos/tcondie/spark/issues{/number}","pulls_url":"https://api.github.com/repos/tcondie/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/tcondie/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/tcondie/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tcondie/spark/labels{/name}","releases_url":"https://api.github.com/repos/tcondie/spark/releases{/id}","deployments_url":"https://api.github.com/repos/tcondie/spark/deployments","created_at":"2016-10-20T20:51:29Z","updated_at":"2016-10-20T20:51:53Z","pushed_at":"2017-02-24T20:16:57Z","git_url":"git://github.com/tcondie/spark.git","ssh_url":"git@github.com:tcondie/spark.git","clone_url":"https://github.com/tcondie/spark.git","svn_url":"https://github.com/tcondie/spark","homepage":null,"size":260843,"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":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"f87a6a59af5037c28d8b3c801c586b347f0ae10c","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}","deployments_url":"https://api.github.com/repos/apache/spark/deployments","created_at":"2014-02-25T08:00:08Z","updated_at":"2017-02-24T21:13:41Z","pushed_at":"2017-02-24T23:40:16Z","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":263792,"stargazers_count":11804,"watchers_count":11804,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":11082,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":450,"forks":11082,"open_issues":450,"watchers":11804,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/17043"},"html":{"href":"https://github.com/apache/spark/pull/17043"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/17043"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/17043/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/17043/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/17043/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/e6b6dc1035588dfdb8d4a16e2004ad3e0c838adb"}}}}
{"actor":{"display_login":"tdas"}}