{"id":30,"date":"2016-08-07T11:30:05","date_gmt":"2016-08-07T11:30:05","guid":{"rendered":"http:\/\/buki.io\/?p=30"},"modified":"2016-10-04T13:28:48","modified_gmt":"2016-10-04T13:28:48","slug":"apache-spark-2-0-select-from","status":"publish","type":"post","link":"http:\/\/buki.io\/?p=30","title":{"rendered":"Apache Spark 2.0: select * from"},"content":{"rendered":"<p>How to query a &#8222;regular&#8220; databases (DB2 in this example) via JDBC in Apache Spark 2.0.<\/p>\n<p><code>final Dataset<Row> result = this.sparkSession.read().format(\"jdbc\").options(this.options).load();<\/code><\/p>\n<p>Where &#8222;this.options&#8220; is filled like<\/p>\n<p><code>this.options.put(\"driver\", \"com.ibm.db2.jcc.DB2Driver\");<br \/>\nthis.options.put(\"url\", \"jdbc:db2:\/\/host:port\/db2_database\");<br \/>\nthis.options.put(\"user\",USER_STRING);<br \/>\nthis.options.put(\"password\",PWD_STRING);<br \/>\nthis.options.put(\"dbtable\", sql_statement\");<\/code><\/p>\n<p>Important to notice is to wrap <code>sql_statement<\/code> like this:<br \/>\n<code>(select * from table) as tmp_table<\/code><\/p>\n<p>because Spark converts this to <\/p>\n<p><code>select * from (select * from table) as tmp_table<\/code><\/p>\n<p>Using the select without brackets does not work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to query a &#8222;regular&#8220; databases (DB2 in this example) via JDBC in Apache Spark 2.0. final Dataset result = this.sparkSession.read().format(&#8222;jdbc&#8220;).options(this.options).load(); Where &#8222;this.options&#8220; is filled like this.options.put(&#8222;driver&#8220;, &#8222;com.ibm.db2.jcc.DB2Driver&#8220;); this.options.put(&#8222;url&#8220;, &#8222;jdbc:db2:\/\/host:port\/db2_database&#8220;); this.options.put(&#8222;user&#8220;,USER_STRING); this.options.put(&#8222;password&#8220;,PWD_STRING); this.options.put(&#8222;dbtable&#8220;, sql_statement&#8220;); Important to notice is to wrap sql_statement like this: (select * from table) as tmp_table because Spark converts this to select &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/buki.io\/?p=30\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8222;Apache Spark 2.0: select * from&#8220;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[3],"tags":[13,15,14],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7N2pi-u","_links":{"self":[{"href":"http:\/\/buki.io\/index.php?rest_route=\/wp\/v2\/posts\/30"}],"collection":[{"href":"http:\/\/buki.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/buki.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/buki.io\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/buki.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=30"}],"version-history":[{"count":4,"href":"http:\/\/buki.io\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":54,"href":"http:\/\/buki.io\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions\/54"}],"wp:attachment":[{"href":"http:\/\/buki.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/buki.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/buki.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}