Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pyspark列值在创建DataFrame时自动移位_Dataframe_Apache Spark_Pyspark_Databricks - Fatal编程技术网

Pyspark列值在创建DataFrame时自动移位

Pyspark列值在创建DataFrame时自动移位,dataframe,apache-spark,pyspark,databricks,Dataframe,Apache Spark,Pyspark,Databricks,我正在尝试使用下面的嵌套模式手动创建pyspark数据帧- schema = StructType([ StructField('fields', ArrayType(StructType([ StructField('source', StringType()), StructField('sourceids', ArrayType(IntegerType()))]))), StructField('first_name',StringType(

我正在尝试使用下面的嵌套模式手动创建pyspark数据帧-

schema = StructType([
    StructField('fields', ArrayType(StructType([
        StructField('source', StringType()), 
        StructField('sourceids', ArrayType(IntegerType()))]))), 
  StructField('first_name',StringType()), 
  StructField('last_name',StringType()), 
  StructField('kare_id',StringType()),
  StructField('match_key',ArrayType(StringType()))
])
row = [Row(fields=[Row(
                    source='BCONNECTED', 
                    sourceids=[10,202,30]), 
                Row(
                    source='KP', 
                    sourceids=[20,30,40])],first_name='Christopher', last_name='Nolan', kare_id='kare1', match_key=['abc','abcd']), 
        Row(fields=[
                Row(
                    source='BCONNECTED', 
                    sourceids=[20,304,5,6]), 
                Row(
                    source='KP',  
                    sourceids=[40,50,60])],first_name='Michael', last_name='Caine', kare_id='kare2', match_key=['ncnc','cncnc'])]

content = spark.createDataFrame(sc.parallelize(row), schema=schema)
content.printSchema()
我正在使用以下代码使用此模式创建数据帧-

schema = StructType([
    StructField('fields', ArrayType(StructType([
        StructField('source', StringType()), 
        StructField('sourceids', ArrayType(IntegerType()))]))), 
  StructField('first_name',StringType()), 
  StructField('last_name',StringType()), 
  StructField('kare_id',StringType()),
  StructField('match_key',ArrayType(StringType()))
])
row = [Row(fields=[Row(
                    source='BCONNECTED', 
                    sourceids=[10,202,30]), 
                Row(
                    source='KP', 
                    sourceids=[20,30,40])],first_name='Christopher', last_name='Nolan', kare_id='kare1', match_key=['abc','abcd']), 
        Row(fields=[
                Row(
                    source='BCONNECTED', 
                    sourceids=[20,304,5,6]), 
                Row(
                    source='KP',  
                    sourceids=[40,50,60])],first_name='Michael', last_name='Caine', kare_id='kare2', match_key=['ncnc','cncnc'])]

content = spark.createDataFrame(sc.parallelize(row), schema=schema)
content.printSchema()
架构打印正确,但在执行content.show()时,我可以看到kare_id和last_name列的值已交换

+--------------------+-----------+---------+-------+-------------+
|              fields| first_name|last_name|kare_id|    match_key|
+--------------------+-----------+---------+-------+-------------+
|[[BCONNECTED, [10...|Christopher|    kare1|  Nolan|  [abc, abcd]|
|[[BCONNECTED, [20...|    Michael|    kare2|  Caine|[ncnc, cncnc]|
+--------------------+-----------+---------+-------+-------------+

首先,在创建数据时,您实际上定义了两次模式,当时您已经在RDD中使用了row对象,因此不需要使用createDataFrame函数,您可以执行以下操作:

sc.parallelize(row).toDF().show()
但是,如果您仍然想显式地提到模式,那么您需要保持模式和数据的顺序相同,并且您提到的模式根据您传递的数据是不正确的。正确的模式应该是:

schema = StructType([
  StructField('fields', ArrayType(StructType([StructField('source', StringType()),StructField('sourceids', ArrayType(IntegerType()))]))), 
  StructField('first_name',StringType()), 
  StructField('kare_id',StringType()),
  StructField('last_name',StringType()), 
  StructField('match_key',ArrayType(StringType()))
])

kare_id应该位于姓氏之前,因为这是传递数据的顺序

PySpark使用字典顺序对列名上的
对象进行排序。因此,数据中列的顺序将是
字段、名字、姓氏、匹配键

Spark然后将每个列名与导致不匹配的数据相关联。修复方法是将模式条目交换为
last\u name
kare\u id
,如下所示:

schema = StructType([
    StructField('fields', ArrayType(StructType([
    StructField('source', StringType()),
    StructField('sourceids', ArrayType(IntegerType()))]))),
    StructField('first_name', StringType()),
    StructField('kare_id', StringType()),
    StructField('last_name', StringType()),
    StructField('match_key', ArrayType(StringType()))
])
来自行上的PySpark文档:“行可用于通过使用命名参数创建行对象,字段将按名称排序。”


非常感谢您。干净整洁。非常感谢