pyspark 中的 Pandas 数据框到 hive [英] Pandas dataframe in pyspark to hive
问题描述
如何将 Pandas 数据帧发送到 hive 表?
我知道如果我有一个 spark 数据框,我可以使用
将它注册到一个临时表df.registerTempTable("table_name")sqlContext.sql("create table table_name2 as select * from table_name")
但是当我尝试使用 pandas dataFrame 来 registerTempTable 时,出现以下错误:
AttributeError: 'DataFrame' 对象没有属性 'registerTempTable'
有没有办法让我使用 pandas dataFrame 来注册临时表或将其转换为 spark dataFrame,然后使用它注册一个临时表,以便我可以将其发送回 hive.
我猜你是在尝试使用 pandas df
而不是 Spark 的 DF.
Pandas DataFrame 没有 registerTempTable
这样的方法.
您可以尝试从 pandas DF 创建 Spark DF.
更新:
我已经在 Cloudera 下对其进行了测试(安装了 Anaconda 包裹,其中包括熊猫模块).
确保您已将 PYSPARK_PYTHON
设置为所有 Spark 工作器上的 anaconda python 安装(或另一个包含 Pandas 模块的安装)(通常在:spark-conf/spark-env.sh
)
这是我的测试结果:
<预><代码>>>>将熊猫导入为 pd>>>将 numpy 导入为 np>>>df = pd.DataFrame(np.random.randint(0,100,size=(10, 3)), columns=list('ABC'))>>>sdf = sqlContext.createDataFrame(df)>>>sdf.show()+---+---+---+|A|乙|C|+---+---+---+|98|33|75||91|57|80||20|87|85||20|61|37||96|64|60||79|45|82||82|16|22||77|34|65||74|18|17||71|57|60|+---+---+---+>>>sdf.printSchema()根|-- A: long (nullable = true)|-- B: 长(可为空 = 真)|-- C: long (nullable = true)How to send a pandas dataframe to a hive table?
I know if I have a spark dataframe, I can register it to a temporary table using
df.registerTempTable("table_name")
sqlContext.sql("create table table_name2 as select * from table_name")
but when I try to use the pandas dataFrame to registerTempTable, I get the below error:
AttributeError: 'DataFrame' object has no attribute 'registerTempTable'
Is there a way for me to use a pandas dataFrame to register a temp table or convert it to a spark dataFrame and then use it register a temp table so that I can send it back to hive.
I guess you are trying to use pandas df
instead of Spark's DF.
Pandas DataFrame has no such method as registerTempTable
.
you may try to create Spark DF from pandas DF.
UPDATE:
I've tested it under Cloudera (with installed Anaconda parcel, which includes Pandas module).
Make sure that you have set PYSPARK_PYTHON
to your anaconda python installation (or another one containing Pandas module) on all your Spark workers (usually in: spark-conf/spark-env.sh
)
Here is result of my test:
>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame(np.random.randint(0,100,size=(10, 3)), columns=list('ABC'))
>>> sdf = sqlContext.createDataFrame(df)
>>> sdf.show()
+---+---+---+
| A| B| C|
+---+---+---+
| 98| 33| 75|
| 91| 57| 80|
| 20| 87| 85|
| 20| 61| 37|
| 96| 64| 60|
| 79| 45| 82|
| 82| 16| 22|
| 77| 34| 65|
| 74| 18| 17|
| 71| 57| 60|
+---+---+---+
>>> sdf.printSchema()
root
|-- A: long (nullable = true)
|-- B: long (nullable = true)
|-- C: long (nullable = true)
这篇关于pyspark 中的 Pandas 数据框到 hive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!