Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.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
Scala 使用DataFrame在配置单元中添加新列_Scala_Apache Spark_Hive - Fatal编程技术网

Scala 使用DataFrame在配置单元中添加新列

Scala 使用DataFrame在配置单元中添加新列,scala,apache-spark,hive,Scala,Apache Spark,Hive,我有一个临时表(每日截断和加载),其中我使用加载语句从一个csv文件加载数据,将数据以文本文件的形式存储在表中。现在,在这个表中,我需要创建一个可以捕获当前日期的列。为此,我从该表中创建一个Df,并使用withcolumn方法添加新列,然后再次使用“覆盖”模式将此Df保存在同一个表中。但当我将数据签入表中时,无法获取任何数据。请任何人建议我如何实现此功能。我尝试了以下代码 CREATE TABLE test (id String, Name STRING,college STRING)

我有一个临时表(每日截断和加载),其中我使用加载语句从一个csv文件加载数据,将数据以文本文件的形式存储在表中。现在,在这个表中,我需要创建一个可以捕获当前日期的列。为此,我从该表中创建一个Df,并使用withcolumn方法添加新列,然后再次使用“覆盖”模式将此Df保存在同一个表中。但当我将数据签入表中时,无法获取任何数据。请任何人建议我如何实现此功能。我尝试了以下代码

    CREATE TABLE test (id String, Name STRING,college STRING) ROW FORMAT DELIMITED   FIELDS TERMINATED BY ',' STORED AS TEXTFile;
    load Data inpath 'path' into table dbname.test ;
    val s1=spark.sql("select * from dbname.test")
    val s2=s1.withColumn("Admission_date", date_add(current_date(),0));
    s2.write.mode("overwrite").saveAsTable("dbname.test")

这里的问题是您正在
读取和覆盖同一个表

  • 但在spark中,我们需要做一些变通
步骤:

//1.read hive table
val s1=spark.sql("select * from dbname.test")
val s2=s1.withColumn("Admission_date", date_add(current_date(),0));

//2.write into temporary table tmp
s2.write.mode("overwrite").saveAsTable("dbname.tmp")

//3.read from temporary table and overwrite final table
val s3=spark.sql("select * from dbname.tmp")
s2.write.mode("overwrite").saveAsTable("dbname.test")

//4.drop the temporary table
spark.sql("drop table dbname.tmp")

当我使用s2.write.mode(“overwrite”).saveAsTable(“dbname.test”)将数据从临时表加载到我的表时,它还将我的表属性从文本更改为拼花。所以第一次加载数据后,当我使用load语句truncate后再次将数据加载到同一个表中时,它显示“file.csv”不是拼花文件。在尾部[80,65,82,49]处应为幻数,但在前面找到了[65,65,10,10]:存储为INPUTFORMAT org.apache.hadoop.mapred.TextInputFormat'after:存储为INPUTFORMAT'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat'