Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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
Apache spark 使用DataFrame.schema.fields.update时出错_Apache Spark_Apache Spark Sql - Fatal编程技术网

Apache spark 使用DataFrame.schema.fields.update时出错

Apache spark 使用DataFrame.schema.fields.update时出错,apache-spark,apache-spark-sql,Apache Spark,Apache Spark Sql,我想在我的数据框中强制转换两列。这是我的密码: val会话=SparkSession 建设者 .master(“本地”) .appName(“UDTransform”).getOrCreate() var df:DataFrame=session.createDataFrame(Seq((1,“Spark”,111),(2,“Storm”,112),(3,“Hadoop”,113),(4,“Kafka”,114),(5,“Flume”,115),(6,“Hbase”,116))) .toDF(“

我想在我的数据框中强制转换两列。这是我的密码:

val会话=SparkSession
建设者
.master(“本地”)
.appName(“UDTransform”).getOrCreate()
var df:DataFrame=session.createDataFrame(Seq((1,“Spark”,111),(2,“Storm”,112),(3,“Hadoop”,113),(4,“Kafka”,114),(5,“Flume”,115),(6,“Hbase”,116)))
.toDF(“CID”、“名称”、“标准”)
df.printSchema()
df.schema.fields.update(0,StructField(“CID”,StringType))
df.schema.fields.update(2,StructField(“STD”,StringType))
df.printSchema()
df.show()
我从控制台获取以下日志:

   root
 |-- CID: integer (nullable = false)
 |-- Name: string (nullable = true)
 |-- STD: integer (nullable = false)

root
 |-- CID: string (nullable = true)
 |-- Name: string (nullable = true)
 |-- STD: string (nullable = true)

17/06/28 12:44:32 ERROR CodeGenerator: failed to compile: org.codehaus.commons.compiler.CompileException: File 'generated.java', Line 36, Column 31: A method named "toString" is not declared in any enclosing class nor any supertype, nor through a static import
我想知道的是为什么会发生这种错误,以及如何解决它?
非常感谢

无法更新数据帧的架构,因为数据帧是不可变的, 但是您可以更新dataframe的模式并分配给新的dataframe

这是你能做的

val newDF = df.withColumn("CID", col("CID").cast("string"))
.withColumn("STD", col("STD").cast("string"))

newDF.printSchema()
newDF的模式是

    root
     |-- CID: string (nullable = true)
     |-- Name: string (nullable = true)
     |-- STD: string (nullable = true)
您的代码:

df.schema.fields.update(0, StructField("CID", StringType))
df.schema.fields.update(2, StructField("STD", StringType))
df.printSchema()
df.show()
在代码中

df.schema.fields
返回
StructFields
数组

Array[StructFields]
然后,如果您尝试更新为

df.schema.fields.update(0, StructField("CID", StringType))
这将更新第0个位置的
数组[StructField]
的值,I这不是您想要的


DataFrame.schema.fields.update
不更新DataFrame架构,而是更新由
DataFrame.schema.fields


希望这有帮助

是的,它可以工作。但我仍然想知道为什么会发生此错误。以及如何正确使用API DataFrame.schema.fields.update??DataFrame.schema.fields.update不更新DataFrame架构,而是返回一个数组并更新数组。