PySpark 存储级别
StorageLevel决定了RDD应如何存储。在Apache Spark中,StorageLevel决定了RDD是应存储在内存中还是应存储在磁盘上,或者两者都存储。它还决定是否对RDD进行序列化以及是否复制RDD分区。
下面的代码块中定义了一个StorageLevel的类−
class pyspark.StorageLevel(useDisk, useMemory, useOffHeap, deserialized, replication = 1)
现在,关于RDD的存储决策,有不同的存储级别,如下所示:
- DISK_ONLY = StorageLevel(True, False, False, False, 1)
-
DISK_ONLY_2 = StorageLevel(True, False, False, False, 2)
-
MEMORY_AND_DISK = StorageLevel(True, True, False, False, 1)
-
MEMORY_AND_DISK_2 = StorageLevel(True, True, False, False, 2)
-
MEMORY_AND_DISK_SER = StorageLevel(True, True, False, False, 1)
-
MEMORY_AND_DISK_SER_2 = StorageLevel(True, True, False, False, 2)
-
MEMORY_ONLY = StorageLevel(False, True, False, False, 1)
-
MEMORY_ONLY_2 = StorageLevel(False, True, False, False, 2)
-
MEMORY_ONLY_SER = StorageLevel(False, True, False, False, 1)
-
MEMORY_ONLY_SER_2 = StorageLevel(False, True, False, False, 2)
-
OFF_HEAP = StorageLevel(True, True, True, False, 1)
让我们考虑以下StorageLevel的例子,我们使用存储级别 MEMORY_AND_DISK_2 ,这意味着RDD的分区将有2个副本。
------------------------------------storagelevel.py-------------------------------------
from pyspark import SparkContext
import pyspark
sc = SparkContext (
"local",
"storagelevel app"
)
rdd1 = sc.parallelize([1,2])
rdd1.persist( pyspark.StorageLevel.MEMORY_AND_DISK_2 )
rdd1.getStorageLevel()
print(rdd1.getStorageLevel())
------------------------------------storagelevel.py-------------------------------------
命令 − 命令如下所示:
$SPARK_HOME/bin/spark-submit storagelevel.py
输出 - 上述命令的输出如下所示 –
Disk Memory Serialized 2x Replicated