How to create an empty DataFrame? Why "ValueError: RDD is empty"?
How to create an empty DataFrame? Why "ValueError: RDD is empty"?
我正在尝试在 Spark (Pyspark) 中创建一个空数据框。
我使用的方法与此处讨论的方法类似 enter link description here,但它不起作用。
这是我的代码
df = sqlContext.createDataFrame(sc.emptyRDD(), schema)
这是错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/Me/Desktop/spark-1.5.1-bin-hadoop2.6/python/pyspark/sql/context.py", line 404, in createDataFrame
rdd, schema = self._createFromRDD(data, schema, samplingRatio)
File "/Users/Me/Desktop/spark-1.5.1-bin-hadoop2.6/python/pyspark/sql/context.py", line 285, in _createFromRDD
struct = self._inferSchema(rdd, samplingRatio)
File "/Users/Me/Desktop/spark-1.5.1-bin-hadoop2.6/python/pyspark/sql/context.py", line 229, in _inferSchema
first = rdd.first()
File "/Users/Me/Desktop/spark-1.5.1-bin-hadoop2.6/python/pyspark/rdd.py", line 1320, in first
raise ValueError("RDD is empty")
ValueError: RDD is empty
在撰写此答案时,您似乎需要某种模式
from pyspark.sql.types import *
field = [StructField("field1", StringType(), True)]
schema = StructType(field)
sc = spark.sparkContext
sqlContext.createDataFrame(sc.emptyRDD(), schema)
您可以像这样加载一个空文件(parquet
、json
等)来完成此操作:
df = sqlContext.read.json("my_empty_file.json")
然后当您尝试检查架构时,您会看到:
>>> df.printSchema()
root
在 Scala/Java 中不传递路径也应该有效,在 Python 中它会抛出异常。此外,如果您切换到 Scala/Python,您可以使用 this method 创建一个。
扩展 Joe Widen 的 ,您实际上可以创建没有字段的架构,如下所示:
schema = StructType([])
因此,当您使用它作为架构创建 DataFrame 时,您最终会得到一个 DataFrame[]
。
>>> empty = sqlContext.createDataFrame(sc.emptyRDD(), schema)
DataFrame[]
>>> empty.schema
StructType(List())
在 Scala 中,如果您选择使用 sqlContext.emptyDataFrame
并检查架构,它将 return StructType()
.
scala> val empty = sqlContext.emptyDataFrame
empty: org.apache.spark.sql.DataFrame = []
scala> empty.schema
res2: org.apache.spark.sql.types.StructType = StructType()
这将适用于 spark 2.0.0 或更高版本
from pyspark.sql import SQLContext
sc = spark.sparkContext
schema = StructType([StructField('col1', StringType(), False),StructField('col2', IntegerType(), True)])
sqlContext.createDataFrame(sc.emptyRDD(), schema)
你可以像这样使用:
pivot_table = sparkSession.createDataFrame([("99","99")], ["col1","col2"])
spark.range(0).drop("id")
这将创建一个包含 "id" 列且没有行的 DataFrame,然后删除 "id" 列,留下一个真正空的 DataFrame。
您可以在 pyspark 中使用以下语法创建一个空数据框:
df = spark.createDataFrame([], ["col1", "col2", ...])
其中 []
表示 col1
和 col2
的空值。然后您可以注册为 sql 查询的临时视图:
**df2.createOrReplaceTempView("artist")**
这是创建具有推断模式的空 spark df 的迂回但简单的方法
# Initialize a spark df using one row of data with the desired schema
init_sdf = spark.createDataFrame([('a_string', 0, 0)], ['name', 'index', 'seq_#'])
# remove the row. Leaves the schema
empty_sdf = init_sdf.where(col('name') == 'not_match')
empty_sdf.printSchema()
# Output
root
|-- name: string (nullable = true)
|-- index: long (nullable = true)
|-- seq_#: long (nullable = true)
Seq.empty[String].toDF()
这将创建一个空的 df。有助于测试目的和所有。 (Scala-Spark)
如果您想要一个基于现有数据框的空数据框,只需将行数限制为 0。
在 PySpark 中:
emptyDf = existingDf.limit(0)
import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType,StructField, StringType
spark = SparkSession.builder.appName('SparkPractice').getOrCreate()
schema = StructType([
StructField('firstname', StringType(), True),
StructField('middlename', StringType(), True),
StructField('lastname', StringType(), True)
])
df = spark.createDataFrame(spark.sparkContext.emptyRDD(),schema)
df.printSchema()
在 Spark 3.1.2 中,spark.sparkContext.emptyRDD()
函数会抛出错误。使用 schema
,传递一个空列表将起作用:
df = spark.createDataFrame([], schema)
我正在尝试在 Spark (Pyspark) 中创建一个空数据框。
我使用的方法与此处讨论的方法类似 enter link description here,但它不起作用。
这是我的代码
df = sqlContext.createDataFrame(sc.emptyRDD(), schema)
这是错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/Me/Desktop/spark-1.5.1-bin-hadoop2.6/python/pyspark/sql/context.py", line 404, in createDataFrame
rdd, schema = self._createFromRDD(data, schema, samplingRatio)
File "/Users/Me/Desktop/spark-1.5.1-bin-hadoop2.6/python/pyspark/sql/context.py", line 285, in _createFromRDD
struct = self._inferSchema(rdd, samplingRatio)
File "/Users/Me/Desktop/spark-1.5.1-bin-hadoop2.6/python/pyspark/sql/context.py", line 229, in _inferSchema
first = rdd.first()
File "/Users/Me/Desktop/spark-1.5.1-bin-hadoop2.6/python/pyspark/rdd.py", line 1320, in first
raise ValueError("RDD is empty")
ValueError: RDD is empty
在撰写此答案时,您似乎需要某种模式
from pyspark.sql.types import *
field = [StructField("field1", StringType(), True)]
schema = StructType(field)
sc = spark.sparkContext
sqlContext.createDataFrame(sc.emptyRDD(), schema)
您可以像这样加载一个空文件(parquet
、json
等)来完成此操作:
df = sqlContext.read.json("my_empty_file.json")
然后当您尝试检查架构时,您会看到:
>>> df.printSchema()
root
在 Scala/Java 中不传递路径也应该有效,在 Python 中它会抛出异常。此外,如果您切换到 Scala/Python,您可以使用 this method 创建一个。
扩展 Joe Widen 的
schema = StructType([])
因此,当您使用它作为架构创建 DataFrame 时,您最终会得到一个 DataFrame[]
。
>>> empty = sqlContext.createDataFrame(sc.emptyRDD(), schema)
DataFrame[]
>>> empty.schema
StructType(List())
在 Scala 中,如果您选择使用 sqlContext.emptyDataFrame
并检查架构,它将 return StructType()
.
scala> val empty = sqlContext.emptyDataFrame
empty: org.apache.spark.sql.DataFrame = []
scala> empty.schema
res2: org.apache.spark.sql.types.StructType = StructType()
这将适用于 spark 2.0.0 或更高版本
from pyspark.sql import SQLContext
sc = spark.sparkContext
schema = StructType([StructField('col1', StringType(), False),StructField('col2', IntegerType(), True)])
sqlContext.createDataFrame(sc.emptyRDD(), schema)
你可以像这样使用:
pivot_table = sparkSession.createDataFrame([("99","99")], ["col1","col2"])
spark.range(0).drop("id")
这将创建一个包含 "id" 列且没有行的 DataFrame,然后删除 "id" 列,留下一个真正空的 DataFrame。
您可以在 pyspark 中使用以下语法创建一个空数据框:
df = spark.createDataFrame([], ["col1", "col2", ...])
其中 []
表示 col1
和 col2
的空值。然后您可以注册为 sql 查询的临时视图:
**df2.createOrReplaceTempView("artist")**
这是创建具有推断模式的空 spark df 的迂回但简单的方法
# Initialize a spark df using one row of data with the desired schema
init_sdf = spark.createDataFrame([('a_string', 0, 0)], ['name', 'index', 'seq_#'])
# remove the row. Leaves the schema
empty_sdf = init_sdf.where(col('name') == 'not_match')
empty_sdf.printSchema()
# Output
root
|-- name: string (nullable = true)
|-- index: long (nullable = true)
|-- seq_#: long (nullable = true)
Seq.empty[String].toDF()
这将创建一个空的 df。有助于测试目的和所有。 (Scala-Spark)
如果您想要一个基于现有数据框的空数据框,只需将行数限制为 0。 在 PySpark 中:
emptyDf = existingDf.limit(0)
import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType,StructField, StringType
spark = SparkSession.builder.appName('SparkPractice').getOrCreate()
schema = StructType([
StructField('firstname', StringType(), True),
StructField('middlename', StringType(), True),
StructField('lastname', StringType(), True)
])
df = spark.createDataFrame(spark.sparkContext.emptyRDD(),schema)
df.printSchema()
在 Spark 3.1.2 中,spark.sparkContext.emptyRDD()
函数会抛出错误。使用 schema
,传递一个空列表将起作用:
df = spark.createDataFrame([], schema)