使用pyspark建立与钻取的连接

时间:2017-08-11 13:28:22

标签: apache-spark pyspark pyspark-sql apache-drill mapr

我正在尝试使用钻取来连接pyspark shell,从MapR DB将数据提取到数据框中。

以下是我在pyspark shell中的操作:

 `dataframe_mysql = sqlContext.read.format("jdbc").option("url", "jdbc:drill:zk=localhost:5181/drill/demo_mapr_com-drillbits;schema=dfs;").option("driver","org.apache.drill.jdbc.Driver").option("dbtable","select * from dfs.`/DDDE/jsondb/ruleengine/testtransactions").option("user","root").option("password","mapr").load()`

不幸的是,我收到以下错误。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/mapr/spark/spark-1.6.3-bin-hadoop2.6/python/pyspark/sql/readwriter.py", line 139, in load
    return self._df(self._jreader.load())
  File "/opt/mapr/spark/spark-1.6.3-bin-hadoop2.6/python/lib/py4j-0.9-src.zip/py4j/java_gateway.py", line 813, in __call__
  File "/opt/mapr/spark/spark-1.6.3-bin-hadoop2.6/python/pyspark/sql/utils.py", line 45, in deco
    return f(*a, **kw)
  File "/opt/mapr/spark/spark-1.6.3-bin-hadoop2.6/python/lib/py4j-0.9-src.zip/py4j/protocol.py", line 308, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o88.load.
: java.lang.ClassNotFoundException: org.apache.drill.jdbc.Driver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at org.apache.spark.sql.execution.datasources.jdbc.DriverRegistry$.register(DriverRegistry.scala:38)
        at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anonfun$createConnectionFactory$1.apply(JdbcUtils.scala:45)
        at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anonfun$createConnectionFactory$1.apply(JdbcUtils.scala:45)
        at scala.Option.foreach(Option.scala:236)
        at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.createConnectionFactory(JdbcUtils.scala:45)
        at org.apache.spark.sql.execution.datasources.jdbc.JDBCRDD$.resolveTable(JDBCRDD.scala:120)
        at org.apache.spark.sql.execution.datasources.jdbc.JDBCRelation.<init>(JDBCRelation.scala:91)
        at org.apache.spark.sql.execution.datasources.jdbc.DefaultSource.createRelation(DefaultSource.scala:57)
        at org.apache.spark.sql.execution.datasources.ResolvedDataSource$.apply(ResolvedDataSource.scala:158)
        at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:119)
        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

知道我哪里错了吗?

编辑:在sqlline中,我可以通过以下方式检索数据框:

!connect jdbc:drill:zk=localhost:31010/drill/demo_mapr_com-drillbits;schema=dfs;
!connect jdbc:drill:zk=localhost:5181/drill/demo_mapr_com-drillbits;schema=dfs;
select * from dfs.`/DDDE/jsondb/ruleengine/testtransactions`;

2 个答案:

答案 0 :(得分:3)

Drill JDBC Driver JAR文件必须存在于客户端计算机上,以便您可以为要使用的应用程序或第三方工具配置驱动程序。你可以按照以下方式获得驱动程序:

<script> function regValidation() { document.getElementById('td100').innerHTML = ""; event.preventDefault ? event.preventDefault() : event.returnValue = false; var error = ""; var email1 = document.getElementById('em100').value; var email2 = document.getElementById('em101').value; var pass1 = document.getElementById('pw100').value; var pass2 = document.getElementById('pw101').value; if (!document.getElementById('em100').checkValidity()) error += "</br>Emails are not valid."; if (email1 !== email2) error += "</br>Emails do not match."; if (pass1 !== pass2) error += "</br>Passwords do not match."; if (pass1.length < @minPass || pass1.length > @maxPass) error += "</br>Password must be @minPass - @maxPass characters."; if (error !== "") { document.getElementById('td100').innerHTML = error } else { return true && regSuccess(); } } function regSuccess() { alert("Thank you for signing up!"); document.getElementById('fm101').submit(); return; } </script> JAR文件从以下Drill安装目录复制到您的工作目录,然后按照以下步骤启动脚本:

drill-jdbc-all

如果您使用./bin/spark-submit --jars drill-jdbc-all-<version>.jar your_spark_script.py ,则应执行以下操作:

pyspark

如果在钻取目录安装中找不到JAR:

pyspark --jars drill-jdbc-all-<version>.jar

答案 1 :(得分:0)

使用Drill ODBC驱动程序jar文件的另一种方法是使用Drill RESTful API,如下所述:https://drill.apache.org/docs/rest-api-introduction/#query