{"id":1974,"date":"2018-09-29T23:18:40","date_gmt":"2018-09-29T21:18:40","guid":{"rendered":"http:\/\/van-maanen.com\/?p=1974"},"modified":"2018-09-29T23:18:40","modified_gmt":"2018-09-29T21:18:40","slug":"processing-csv-files-in-spark","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=1974","title":{"rendered":"Processing CSV files in Spark"},"content":{"rendered":"<p>It is possible to process CSV files in Spark as if these files are tables. That looks very promising: reading a file, translating the file into something that can be seen as a table and subsequently applying SQL to this. We need some additional jars however. First, we need to download 2 jars: http:\/\/mvnrepository.com\/artifact\/com.databricks\/spark-csv_2.10 and http:\/\/mvnrepository.com\/artifact\/org.apache.commons\/commons-csv\/1.2. I stored these files in SPARK_HOME\\lib. In that directory, I saw other jars as well. I then started spark from SPARK_HOME with .\/bin\/spark-shell &#8211;jars lib\/spark-csv_2.10-1.5.0.jar,lib\/commons-csv-1.2.jar. This then includes the jars that were just downloaded.<br \/>\nThe inclusion of these jars provide a so-called SQL context (sqlContext).<br \/>\nI could then issue two lines of code:<\/p>\n<ul>\n<li>val df = sqlContext.read.format(&#8220;com.databricks.spark.csv&#8221;).option(&#8220;inferScheme&#8221;,&#8221;true&#8221;).option(&#8220;header&#8221;,&#8221;true&#8221;).load(&#8220;\/user\/tom\/baby_names.csv&#8221;);<\/li>\n<li>df.registerTempTable(&#8220;names&#8221;)<\/li>\n<\/ul>\n<p>In this situation a file (\/user\/tom\/baby_names.csv) can be approached as a table (names). One may then apply SQL to this:<\/p>\n<ul>\n<li>val Som = sqlContext.sql(&#8220;select sum(Int(Count)) from names&#8221;)<\/li>\n<\/ul>\n<p>Som is a dataframe. One may display the content with Som.collect.foreach(println)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is possible to process CSV files in Spark as if these files are tables. That looks very promising: reading a file, translating the file into something that can be seen as a table and subsequently applying SQL to this. We need some additional jars however. First, we need to download 2 jars: http:\/\/mvnrepository.com\/artifact\/com.databricks\/spark-csv_2.10 and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1974","post","type-post","status-publish","format-standard","hentry","category-nice-to-know"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/1974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1974"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/1974\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1974"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}