如何在 PySpark 中檢查某個物件是 RDD 還是 DataFrame?


RDD 是彈性分散式資料集的縮寫,它是 PySpark 的基本抽象(不可變的物件集合)。RDD 是 PySpark 的主要構建塊。它們被分成較小的塊並分佈在叢集中的節點之間。它支援轉換和操作。

PySpark 中的 DataFrame

DataFrame 是 Python 中一個二維帶標籤的資料結構。它用於資料操作和資料分析。它接受不同的資料型別,例如整數、浮點數、字串等。列標籤是唯一的,而行則用唯一的索引值標記,這有助於訪問特定的行。

DataFrame 通常用於機器學習任務中,以操作和分析大型資料集。它們支援諸如過濾、排序、合併、分組和轉換資料等操作。

PySpark 提供了一個名為 isinstance() 的函式,該函式有助於檢查給定物件是 RDD 還是 DataFrame。

語法

以下是使用 isinstance() 函式的語法。

isinstance(data,rdd/dataframe)

其中,

  • Isinstance() 是用於查詢資料是 RDD 還是 DataFrame 的函式

  • data 是輸入資料

安裝 PySpark

首先,我們必須使用以下程式碼在 Python 環境中安裝 PySpark 庫。

pip install PySpark 

輸出

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting PySpark
  Downloading PySpark-3.3.2.tar.gz (281.4 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 281.4/281.4 MB 5.3 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... done
Collecting py4j==0.10.9.5
  Downloading py4j-0.10.9.5-py2.py3-none-any.whl (199 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 199.7/199.7 KB 28.1 MB/s eta 0:00:00
Building wheels for collected packages: PySpark
  Building wheel for PySpark (setup.py) ... done
  Created wheel for PySpark: filename=PySpark-3.3.2-py2.py3-none-any.whl size=281824028 sha256=184a9a6949d4be5a4746cd53cb28d40cf38a4771048f5f14445d8ee4ab14a07c
  Stored in directory: /root/.cache/pip/wheels/6c/e3/9b/0525ce8a69478916513509d43693511463c6468db0de237c86
Successfully built PySpark
Installing collected packages: py4j, PySpark
  Attempting uninstall: py4j
    Found existing installation: py4j 0.10.9.7
    Uninstalling py4j-0.10.9.7:
      Successfully uninstalled py4j-0.10.9.7
Successfully installed py4j-0.10.9.5 PySpark-3.3.2

在使用 isinstance() 函式之前,我們必須建立資料,即 DataFrame 或 RDD。

from PySpark.sql importmSparkSession
from PySpark.sql.types import *

spark = SparkSession.builder \
   .appName("substring check") \
   .getOrCreate()

schema = StructType([
   StructField("name", StringType(), True),
   StructField("age", IntegerType(), True),
   StructField("gender", StringType(), True),
   StructField("occupation", StringType(), True),
   StructField("salary", DoubleType(), True)
])

df = [("John", 25, "Male", "Developer", 5000.0),
        ("Jane", 30, "Female", "Manager", 7000.0),
        ("Bob", 35, "Male", "Director", 10000.0),
        ("Alice", 40, "Female", "CEO", 15000.0)]
data = spark.createDataFrame(df, schema)
df.show()

輸出

+-----+---+------+----------+-------+
| name|age|gender|occupation| salary|
+-----+---+------+----------+-------+
| John| 25|  Male| Developer| 5000.0|
| Jane| 30|Female|   Manager| 7000.0|
|  Bob| 35|  Male|  Director|10000.0|
|Alice| 40|Female|       CEO|15000.0|
+-----+---+------+----------+-------+

示例

在以下示例中,我們將資料與格式名稱 RDD 或 DataFrame 一起傳遞給 PySpark 的 isinstance() 函式。

from PySpark.sql import DataFrame
from PySpark.rdd import RDD
if isinstance(data, RDD):
   print("The given data is an RDD")
elif isinstance(data, DataFrame):
   print("The given data is a DataFrame")
else:
   print("The given data is neither an RDD nor a DataFrame")

輸出

The given data is a DataFrame

示例

在以下示例中,我們將列表資料結構傳遞給 isinstance() 函式。

from PySpark.sql import DataFrame
from PySpark.rdd import RDD
data = [22,1,14,5,12,5,7,2,24,2,21,11]
if isinstance(data, RDD):
   print("The given data is an RDD")
elif isinstance(data, DataFrame):
   print("The given data is a DataFrame")
else:
   print("The given data is neither an RDD nor a DataFrame")

輸出

The given data is neither an RDD nor a DataFrame

更新於: 2023年10月20日

875 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.