【问题标题】:pyspark 1.3.0 save Data Frame into HIVE tablepyspark 1.3.0 将数据框保存到 HIVE 表中
【发布时间】:2017-06-05 09:15:01
【问题描述】:

我正在使用 spark 1.3.0(在 python 中)

我有 DF:

DF.show(3)

ID             Date       Hour     TimeInCluster Cluster Xcluster Ycluster

25342438156 2012-11-30 15:00:00 26            T       130270   165620

25342438156 2012-11-30 16:00:00 86            D       136850   177070

25342438156 2012-11-30 17:00:00 35            D       136850   177070

我正在尝试将该 DF 保存到不存在的配置单元表中

我该怎么做?

谢谢

我将代码更改为:

sqlContext = HiveContext(sc)

FinalDf.write().mode(SaveMode.Overwrite).saveAsTable("myDB.sixuserstablediary")

但我得到了那个错误

py4j.protocol.Py4JJavaError: An error occurred while calling o280.apply.
: org.apache.spark.sql.AnalysisException: Cannot resolve column name "write" among (IMSI, Date, Hour, TimeInCluster, Cluster, Xcluster, Ycluster);
        at org.apache.spark.sql.DataFrame$$anonfun$resolve$1.apply(DataFrame.scala:162)
        at org.apache.spark.sql.DataFrame$$anonfun$resolve$1.apply(DataFrame.scala:162)
        at scala.Option.getOrElse(Option.scala:120)
        at org.apache.spark.sql.DataFrame.resolve(DataFrame.scala:161)
        at org.apache.spark.sql.DataFrame.col(DataFrame.scala:436)
        at org.apache.spark.sql.DataFrame.apply(DataFrame.scala:426)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:231)
        at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:379)
        at py4j.Gateway.invoke(Gateway.java:259)
        at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:133)
        at py4j.commands.CallCommand.execute(CallCommand.java:79)
        at py4j.GatewayConnection.run(GatewayConnection.java:207)
        at java.lang.Thread.run(Thread.java:745)

【问题讨论】:

  • 请参考以下链接:- stackoverflow.com/questions/30664008/…
  • 我试过但我的代码出错:FinalDf.registerTempTable("mytempTable") sqlContext.sql("create table Sixuserstablediary as select * from mytempTable")
  • 错误:sqlContext.sql("create table Sixuserstablediary as select * from mytempTable");文件“/opt/cloudera/parcels/CDH-5.4.7-1.cdh5.4.7.p0.3/lib/spark/python/pyspark/sql/context.py”,第 528 行,在 sql 中返回 DataFrame(self. _ssql_ctx.sql(sqlQuery), self) 文件“/opt/cloudera/parcels/CDH-5.4.7-1.cdh5.4.7.p0.3/lib/spark/python/lib/py4j-0.8.2.1-src. zip/py4j/java_gateway.py”,第 538 行,在 call 文件中“/opt/cloudera/parcels/CDH-5.4.7-1.cdh5.4.7.p0.3/lib/spark/ python/lib/py4j-0.8.2.1-src.zip/py4j/protocol.py",第 300 行,在 get_return_value

标签: python hadoop pyspark


【解决方案1】:

你需要使用 Spark HiveContext

导入 Spark HiveContex

from pyspark.sql import HiveContext
sqlContext = HiveContext(sc)

从数据框创建一个临时表,然后通过从临时表中选择数据插入到 hive 表中。

// Register the dataframe
   df.registerTempTable("tbl_tmp")

   sqlContext.sql("create table default.tbl_hive_data as select * from tbl_tmp")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-01
    • 2020-06-15
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    相关资源
    最近更新 更多