{"id":2023,"date":"2018-01-13T09:29:52","date_gmt":"2018-01-13T09:29:52","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=755"},"modified":"2018-01-13T09:29:52","modified_gmt":"2018-01-13T09:29:52","slug":"apache-spark-sql-datasets","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/","title":{"rendered":"Introduction to Apache Spark SQL Datasets"},"content":{"rendered":"<p>Spark datasets is a distributed collection of data. It is a new interface, provides benefits of RDDs with Spark SQL\u2019s optimized execution engine. In this blog, we will learn the concept of Spark SQL dataSets.<\/p>\n<p>We will also focus on why datasets is needful, and what is the significance of encoder in the datasets? Moreover, we will cover the features of the SQL DataSets in Apache Spark.<\/p>\n<p>Also, understand how to create a SQL datasets in this spark tutorial.<\/p>\n<h3>What is Spark SQL DataSet?<\/h3>\n<p>It is an interface, provides the advantages of RDDs with the comfort of Spark SQL\u2019s execution engine. It is a distributed collection of data. We can also create Spark datasets from JVM objects. By using functional transformations (map, flatMap, filter, etc.), it can be manipulated.<\/p>\n<p>Datasets API is only available in Scala and Java. R and Python do not have the support of the datasets API as Python is very dynamic in nature, it provides many of the benefits of the datasets API, such as we can access the field of a row by name naturally row.columnName.<\/p>\n<p>In addition, it is a strongly-typed immutable collection of objects, these are mapped to a relational schema. There is a new concept called an <em>encoder<\/em>, at the core of the datasets API. However, an encoder is responsible for converting between JVM objects and tabular representation.<\/p>\n<p>By using Spark\u2019s tungsten binary format, all the tabular representation is stored. It allows operations on serialized data and also improves memory utilization. Datasets API is quite familiar, it provides many of the same functional transformations such as map, flatMap, filter.<\/p>\n<p>The encoder is a primary concept in serialization and deserialization framework. In Spark, there are many built-in encoders, which are very advance. Encoders generate bytecode to interact with off-heap data. Without a de-serializing entire object, the encoder provides on-demand access to individual attributes.<\/p>\n<p>It is structured as well as lazy query expression which triggers on the action, datasets represent logical plan internally. This plan tells the computational query, which we need to produce the data. That plan is also a base catalyst query plan for the logical operator to form a logical query plan.<\/p>\n<p>Afterwards, when we analyze this and resolve we can form a physical query plan. Most importantly,\u00a0 datasets reduce the memory usage.<\/p>\n<h3>Why SQL DataSets in Spark?<\/h3>\n<p>As we know, there were some drawbacks with RDD as well as Spark dataframes, to overcome those limitations datasets introduced. As in dataFrame, data cannot be manipulated without knowing its structure.<\/p>\n<p>Since there was no clause for compile-time type safety. \u00a0While In RDD there was no automatic optimization. Thus, whenever required we need to do it manually, to overcome this issues we needed dataset.<\/p>\n<p>Datasets\u00a0inherit all the features of RDD and dataframe, such as:<\/p>\n<ul>\n<li>RDD\u2019s convenience.<\/li>\n<li>Dataframe\u2019s performance optimization.<\/li>\n<li>Scala\u2019s static type-safety.<\/li>\n<\/ul>\n<h3>Features of SQL DataSets<b><br \/>\n<\/b><\/h3>\n<p>There are so many features of dataset, such as-<\/p>\n<div id=\"attachment_73117\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/11\/Features-of-DataSets-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-73117\" class=\"wp-image-73117 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/11\/Features-of-DataSets-01.jpg\" alt=\"SQL Datasets - features of datasets\" width=\"1200\" height=\"628\" \/><\/a><p id=\"caption-attachment-73117\" class=\"wp-caption-text\">Apache Spark SQL Datasets &#8211; Features Of Datasets<\/p><\/div>\n<h4>1. Inter-convertible<\/h4>\n<p>It is possible to convert the type-safe dataset to an \u201cuntyped\u201d dataframe. There are 3 methods, which datasets holder provides for a conversion from <i>Seq[T]<\/i> or <i>RDD[T]<\/i> types to <i>Dataset[T]:<\/i><\/p>\n<ul>\n<li><i>toDS(): Dataset[T]<\/i><\/li>\n<li>toDF(): dataframe<\/li>\n<li>toDF(colNames: String*): dataframe<\/li>\n<\/ul>\n<h4>2. Persistent Storage<\/h4>\n<p>As we know that datasets in Spark\u00a0are both serializable and queryable. Therefore, it is possible to save it to persistent storage.<\/p>\n<h4>3. Analysis at compile time<\/h4>\n<p>We can check syntax and analysis at compile time by using datasets. This is also one of the limitations of RDD or data frame. Hence, It is not possible using dataframe, RDDs or regular SQL queries.<\/p>\n<h4>4. Optimized Query<\/h4>\n<p>By using catalyst query optimizer and tungsten, it provides an optimized query. Tungsten improves the execution by optimizing the Spark job.<\/p>\n<h4>5. Single API for Java and Scala<\/h4>\n<p>There is a single interface for Java and Scala, in datasets. Using single interface also reduces the burden of libraries. Since libraries have no longer to deal with two different type of inputs. Due to this unification, we can use Scala interface, code examples from both languages.<\/p>\n<h4>6. Faster Computation<\/h4>\n<p>We can implement dataset faster than the RDD implementation, that results in good performance of the system.<\/p>\n<h4>7. Less Memory Consumption<\/h4>\n<p>It creates a more optimal layout while caching, hence dataset reduces the memory consumption.<\/p>\n<h3>How to create SQL DataSets in Spark<\/h3>\n<p>There are multiple ways through which we can create a dataset. like:<\/p>\n<h4>1. SparkSession<\/h4>\n<p>To Spark SQL, spark session is the entry point. While developing SQL applications using datasets, it is the first object we have to create. In Spark <b>2.0, <\/b>spark session has merged SQL context and Hivecontext in one object.<\/p>\n<p>To create an instance of Spark session, we use the SparkSession.builder method.<\/p>\n<p><b>SparkSession.builder<\/b><\/p>\n<p>By using stop method, we can stop the current SparkSession<\/p>\n<p><b>spark.stop<\/b><\/p>\n<h4>2. QueryExecution<\/h4>\n<p>By using QueryExecution, we represent structured query execution pipeline of the datasets. We need to use QueryExecution attribute to access QueryExecution of a dataset. While we execute, a logical plan in Spark session we get QueryExecution.<\/p>\n<p><b>executePlan(plan: LogicalPlan): QueryExecution<\/b><\/p>\n<p>To produce a QueryExecution in the current SparkSession, execute plan executes the input logical plan.<\/p>\n<h4>3. Encoder<\/h4>\n<p>Encoders translate between Spark\u2019s internal binary format and JVM objects. Moreover, by using encoder, we can serialize the object. It serializes objects for processing or transmitting over the network encoders.<\/p>\n<h3><strong>Conclusion<\/strong><\/h3>\n<p>As a result, we have seen that datasets are strongly typed data structure in Spark. Basically, datasets represent structured queries. In the datasets, we get the functionality of both RDD as well as dataframes. By using datasets, we can generate the optimized query.<\/p>\n<p>As we discussed above, that datasets lessen the memory consumption, that increases the performance of the system. Also provides a single API for both Java and Scala. Hence, datasets boost up the efficiency of working of the system.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spark datasets is a distributed collection of data. It is a new interface, provides benefits of RDDs with Spark SQL\u2019s optimized execution engine. In this blog, we will learn the concept of Spark SQL&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":73289,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[614],"tags":[881,882,883,884,885,886,655,887,888],"class_list":["post-2023","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apache-spark","tag-dataset","tag-datasets-getting-started-with-apache-spark","tag-datasets-in-spark","tag-introducing-apache-spark-datasets","tag-introduction-to-apache-spark-sql-datasets","tag-spark-dataset-tutorial-introduction-to-apache-spark-dataset","tag-spark-datasets","tag-spark-sql-datasets","tag-sql-datasets"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Introduction to Apache Spark SQL Datasets - TechVidvan<\/title>\n<meta name=\"description\" content=\"Apache spark SQL datasets- Introduction, Needs of SQL datasets, Features of datasets in spark: interconvertible, persistent storage, optimized query, Creation of SQL datasets in spark,\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Introduction to Apache Spark SQL Datasets - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Apache spark SQL datasets- Introduction, Needs of SQL datasets, Features of datasets in spark: interconvertible, persistent storage, optimized query, Creation of SQL datasets in spark,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-13T09:29:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Spark-SQL-Datasets-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Introduction to Apache Spark SQL Datasets - TechVidvan","description":"Apache spark SQL datasets- Introduction, Needs of SQL datasets, Features of datasets in spark: interconvertible, persistent storage, optimized query, Creation of SQL datasets in spark,","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/","og_locale":"en_US","og_type":"article","og_title":"Introduction to Apache Spark SQL Datasets - TechVidvan","og_description":"Apache spark SQL datasets- Introduction, Needs of SQL datasets, Features of datasets in spark: interconvertible, persistent storage, optimized query, Creation of SQL datasets in spark,","og_url":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2018-01-13T09:29:52+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Spark-SQL-Datasets-01.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Introduction to Apache Spark SQL Datasets","datePublished":"2018-01-13T09:29:52+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/"},"wordCount":983,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Spark-SQL-Datasets-01.jpg","keywords":["Dataset","Datasets - Getting Started with Apache Spark","datasets in spark","Introducing Apache Spark Datasets","Introduction to Apache Spark SQL Datasets","Spark Dataset Tutorial - Introduction to Apache Spark Dataset","Spark Datasets","spark sql datasets","sql datasets"],"articleSection":["Spark Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/","url":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/","name":"Introduction to Apache Spark SQL Datasets - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Spark-SQL-Datasets-01.jpg","datePublished":"2018-01-13T09:29:52+00:00","description":"Apache spark SQL datasets- Introduction, Needs of SQL datasets, Features of datasets in spark: interconvertible, persistent storage, optimized query, Creation of SQL datasets in spark,","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Spark-SQL-Datasets-01.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Spark-SQL-Datasets-01.jpg","width":1200,"height":628,"caption":"Apache Spark SQL Datasets"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/apache-spark-sql-datasets\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Introduction to Apache Spark SQL Datasets"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/2023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=2023"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/2023\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/73289"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=2023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=2023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=2023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}