Spark RDD、DataFrame和DataSet的區別

RDD

RDD(Resilient Distributed Datasets) ,彈性分佈式數據集, 是分佈式內存的一個抽象概念,RDD提供了一種高度受限的共享內存模型,即RDD是隻讀的記錄分區的集合,只能通過在其他RDD執行確定的轉換操作(如map、join和group by)而創建,然而這些限制使得實現容錯的開銷很低。對開發者而言,RDD可以看作是Spark的一個對象,它本身運行於內存中,如讀文件是一個RDD,對文件計算是一個RDD,結果集也是一個RDD ,不同的分片、 數據之間的依賴 、key-value類型的map數據都可以看做RDD。

RDD作爲數據結構,本質上是一個只讀的分區記錄集合。一個RDD可以包含多個分區,每個分區就是一個dataset片段。RDD可以相互依賴。如果RDD的每個分區最多隻能被一個Child RDD的一個分區使用,則稱之爲narrow dependency【窄依賴】;若多個Child RDD分區都可以依賴,則稱之爲wide dependency【寬依賴】。不同的操作依據其特性,可能會產生不同的依賴。例如map操作會產生narrow dependency,而join操作則產生wide dependency。

Datasets and DataFrames

官網對於Datasets and DataFrames的解釋是這樣的:

A Dataset is a distributed collection of data. Dataset is a new interface added in Spark 1.6 that provides the benefits of RDDs (strong typing, ability to use powerful lambda functions) with the benefits of Spark SQL’s optimized execution engine. A Dataset can be constructed from JVM objects and then manipulated using functional transformations (mapflatMapfilter, etc.). The Dataset API is available in Scala and Java. Python does not have the support for the Dataset API. But due to Python’s dynamic nature, many of the benefits of the Dataset API are already available (i.e. you can access the field of a row by name naturally row.columnName). The case for R is similar.

A DataFrame is a Dataset organized into named columns. It is conceptually equivalent to a table in a relational database or a data frame in R/Python, but with richer optimizations under the hood. DataFrames can be constructed from a wide array of sources such as: structured data files, tables in Hive, external databases, or existing RDDs. The DataFrame API is available in Scala, Java, Python, and R. In Scala and Java, a DataFrame is represented by a Dataset of Rows. In the Scala APIDataFrame is simply a type alias of Dataset[Row]. While, in Java API, users need to use Dataset<Row> to represent a DataFrame.

翻譯上面兩段:

DataSet是一種分佈式的數據集合。DataSet是在Spark 1.6中增加的一個新接口,它不但提供了RDD(強類型,強使用lambda函數)的優勢,而且具有Spark SQL被優化的執行引擎的優點。 DataSet可以從JVM對象構建數據集,然後使用功能轉換transformations(map,flatMap,filter等)進行操作。DataSet的API可用於Scala和Java。 Python不支持Dataset API。 但是由於Python的動態特性,Dataset API的許多優點已經可用(即您可以通過名稱自然地訪問行的字段row.columnName)。 R的情況類似。

DataFrame是一個組織成命名列的DataSet。 它在概念上等同於關係數據庫中的表或R / Python中的數據框架,但在引擎下更加優化。 DataFrames可以從各種各樣的源構建,例如:結構化數據文件,Hive中的表,外部數據庫或現有RDD。 DataFrame API在Scala,Java,Python和R中可用。在Scala和Java中,DataFrame由“數據集”行表示。 在Scala API中,DataFrame只是Dataset [Row]的一個類型別名。 而在Java API中,用戶需要使用Dataset <Row>來表示DataFrame。

從上面兩段可以看出,DataFrame相當於關係型數據庫中的表,DataSet相當於表的集合,就是一個分佈式數據集。

關於RDD如何升級爲DataSet,參考這篇文章https://www.iteblog.com/archives/1675.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章