使用Spark 1.6.1。我在mariaDb中有一堆表,我希望转换为pySpark DataFrame对象。但是createExternalTable()正在抛出。例如:
In [292]: tn = sql.tableNames()[10]
In [293]: df = sql.createExternalTable(tn)
/home/charles/spark-1.6.1/python/lib/py4j-0.9-src.zip/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
306 raise Py4JJavaError(
307 "An error occurred while calling {0}{1}{2}.\n".
--> 308 format(target_id, ".", name), value)
309 else:
310 raise Py4JError(
Py4JJavaError: An error occurred while calling o18.createExternalTable.
: java.lang.RuntimeException: Tables created with SQLContext must be TEMPORARY. Use a HiveContext instead.
at scala.sys.package$.error(package.scala:27)
at org.apache.spark.sql.execution.SparkStrategies$DDLStrategy$.apply(SparkStrategies.scala:379)
at org.apache.spark.sql.catalyst.planning.QueryPlanner$$anonfun$1.apply(QueryPlanner.scala:58)
at org.apache.spark.sql.catalyst.planning.QueryPlanner$$anonfun$1.apply(QueryPlanner.scala:58)
at scala.collection.Iterator$$anon$13.hasNext(Iterator.scala:371)
at org.apache.spark.sql.catalyst.planning.QueryPlanner.plan(QueryPlanner.scala:59)
at org.apache.spark.sql.execution.QueryExecution.sparkPlan$lzycompute(QueryExecution.scala:47)
at org.apache.spark.sql.execution.QueryExecution.sparkPlan(QueryExecution.scala:45)
at org.apache.spark.sql.execution.QueryExecution.executedPlan$lzycompute(QueryExecution.scala:52)
at org.apache.spark.sql.execution.QueryExecution.executedPlan(QueryExecution.scala:52)
at org.apache.spark.sql.execution.QueryExecution.toRdd$lzycompute(QueryExecution.scala:55)
at org.apache.spark.sql.execution.QueryExecution.toRdd(QueryExecution.scala:55)
at org.apache.spark.sql.SQLContext.createExternalTable(SQLContext.scala:695)
at org.apache.spark.sql.SQLContext.createExternalTable(SQLContext.scala:668)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:231)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:381)
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:209)
at java.lang.Thread.run(Thread.java:745)
如果我指定source ='jdbc',也会发生同样的事情。
该表存在:
In [297]: sql.sql("SELECT * from {} LIMIT 5".format(tn)).show()
+--------------------+-----+-----+-----+----+------+------+------+----------------------+----+----+----+-----+
| Date| Open| High| Low|Last|Change|Settle|Volume|Prev_Day_Open_Interest|prod|exch|year|month|
+--------------------+-----+-----+-----+----+------+------+------+----------------------+----+----+----+-----+
|1999-10-29 00:00:...|245.0|245.0|245.0|null| null| 245.0| 1.0| 1.0| C| CME|2001| H|
|1999-11-01 00:00:...|245.0|245.0|245.0|null| null| 245.0| 0.0| 1.0| C| CME|2001| H|
|1999-11-02 00:00:...|245.0|245.0|245.0|null| null| 245.0| 0.0| 1.0| C| CME|2001| H|
|1999-11-03 00:00:...|245.0|245.5|245.0|null| null| 245.5| 5.0| 6.0| C| CME|2001| H|
|1999-11-04 00:00:...|245.5|245.5|245.5|null| null| 245.5| 0.0| 6.0| C| CME|2001| H|
+--------------------+-----+-----+-----+----+------+------+------+----------------------+----+----+----+-----+
根据错误,这应该适用于HIVE数据。我没有使用HIVEContext,而是使用SQLContext。根据{{3}},这支持ver> = 1.3。
有没有办法从SqlTable中提取DataFrame?
答案 0 :(得分:1)
给出描述你想要的是createExternalTable
用于管理Hive表而不是简单的表:
df = sqlContext.table(tn)
或分配sql
来电的结果:
df = sqlContext.sql("SELECT * from {}".format(tn))