{"id":5612,"date":"2021-11-08T14:10:48","date_gmt":"2021-11-08T14:10:48","guid":{"rendered":"https:\/\/www.npntraining.com\/blog\/?p=5612"},"modified":"2021-12-31T07:13:31","modified_gmt":"2021-12-31T07:13:31","slug":"how-to-convert-rdd-to-dataframe","status":"publish","type":"post","link":"https:\/\/www.npntraining.com\/blog\/how-to-convert-rdd-to-dataframe\/","title":{"rendered":"How to convert RDD to DataFrame"},"content":{"rendered":"<p>In this blog post we will learn how to convert RDD to DataFrame with spark helper methods used in local development or testing.<\/p>\n<h1>Converting RDD to Data Frame<\/h1>\n<p>First let us create an RDD from collections,<\/p>\n<pre>val temperatureRecords = Seq(\n(India,Array(27.0,26.0,40.1)),\n(China,Array(27.0,26.0,40.1))\n)\nval temperatureRDD = spark.sparkContext.parallize(temperatureRDD);<\/pre>\n<p>There are several ways to convert RDD to DataFrame<\/p>\n<ol>\n<li>By using createDataFrame(RDD obj) from SparkSession object<\/li>\n<li>By using\u00a0createDataFrame(RDD obj) from SparkSession object and by specifying columns names<\/li>\n<li>By using createDataFrame(RDD obj, StructType type) by providing schema using StructType<\/li>\n<\/ol>\n<h2><strong>Method 01 -\u00a0<\/strong><\/h2>\n<p>We will use createDataFrame(Rdd rdd) method to convert RDD into DataFrame<\/p>\n<p>Creating RDD with collections and converting into DataFrame<\/p>\n<pre>val temperatureRecords = List((&quot;Afghanistan&quot;,&quot;8,996,351&quot;),(&quot;China&quot;,&quot;667,070.00&quot;),(&quot;India&quot;,&quot;449,480.61&quot;))\nval dfWithoutSchema = spark.createDataFrame(temperatureData)\ndfWithoutSchema.show;<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>+-----------+----------+\n|         _1|        _2|\n+-----------+----------+\n|Afghanistan| 8,996,351|\n|      China|667,070.00|\n|      India|449,480.61|\n+-----------+----------+<\/pre>\n<p>Creating RDD with external source and converting into DataFrame<\/p>\n<pre>val temperatureData = spark.sparkContext.textFile(&quot;d:\/spark-example\/population.csv&quot;);<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>+-----------+----------+\n|         _1|        _2|\n+-----------+----------+\n|Afghanistan| 8,996,351|\n|      China|667,070.00|\n|   India449|    480.61|\n+-----------+----------+<\/pre>\n<p>If you notice the above output the columns names are &quot;_1&quot; and &quot;<em>2&quot; since we didn't specify the column names. Lets see how to define column names in the next method.<\/p>\n<h2><strong>Method 02 -\u00a0<\/strong><\/h2>\n<p>toDF() provides a concise syntax for creating DataFrames\u00a0 by specifying column names and can be accessed after importing Spark implicits.<\/p>\n<pre>import sqlContext.implicits.<\/em><\/pre>\n<pre>val dataFrameWithSchema = spark.createDataFrame(temperatureRecords).toDF(&quot;Country&quot;,&quot;Population in billions&quot;);\ndataFrameWithSchema.show;<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>+-----------+----------------------+\n|    Country|Population in billions|\n+-----------+----------------------+\n|Afghanistan|             8,996,351|\n|      China|            667,070.00|\n|   India449|                480.61|\n+-----------+----------------------+<\/pre>\n<h2><strong>Method 03 -\u00a0<\/strong><\/h2>\n<p>In this method we will see how to provide schema by using StructType<\/p>\n<pre>val schema = new StructType()\n.add(StructField(&quot;id&quot;, StringType, true))\n.add(StructField(&quot;population&quot;, LongType, true))\n)\nva dataFrame = spark.createDataFrame(rowsRDD,schema)\ndataFrame.show;<\/pre>\n<p>\u00a0<a href=\"https:\/\/spark.apache.org\/docs\/latest\/api\/scala\/index.html#org.apache.spark.package\" target=\"_blank\" rel=\"noopener noreferrer\">Reference documentation<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post we will learn how to convert RDD to DataFrame with spark helper methods used in local development or testing. Converting RDD to Data Frame First let&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5613,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50,57],"tags":[],"class_list":["post-5612","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-engineering","category-data-science"],"_links":{"self":[{"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/posts\/5612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/comments?post=5612"}],"version-history":[{"count":39,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/posts\/5612\/revisions"}],"predecessor-version":[{"id":7825,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/posts\/5612\/revisions\/7825"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/media\/5613"}],"wp:attachment":[{"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/media?parent=5612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/categories?post=5612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/tags?post=5612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}