如何在Pyspark中连接到Presto JDBC



我想使用pyspark中的JDBC连接到Presto服务器。我遵循了用爪哇写的教程。我正在尝试在我的python3代码中执行同样的操作,但会遇到错误:

: java.sql.SQLException: No suitable driver

我试图执行以下代码:

jdbcDF = spark.read 
    .format("jdbc") 
    .option("url", "jdbc:presto://my_machine_ip:8080/hive/default") 
    .option("user", "airflow") 
    .option("dbtable", "may30_1") 
    .load()

应该指出的是,我在EMR上使用Spark,因此,spark已经提供给我。

上述教程中的代码是:

final String JDBC_DRIVER = "com.facebook.presto.jdbc.PrestoDriver";
    final String DB_URL = "jdbc:presto://localhost:9000/catalogName/schemaName";
    //  Database credentials
    final String USER = "username";
    final String PASS = "password";
    Connection conn = null;
    Statement stmt = null;
    try {
      //Register JDBC driver
      Class.forName(JDBC_DRIVER);

请注意以上代码中的JDBC_DRIVER,我无法推断出Python3 I.E。中的相应分配。

我也没有在任何配置中添加任何依赖性。

我希望能成功连接到我的Presto。现在,我遇到以下错误,完成stacktrace:

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
  File "/usr/lib/spark/python/pyspark/sql/readwriter.py", line 172, in load
    return self._df(self._jreader.load())
  File "/usr/lib/spark/python/lib/py4j-0.10.7-src.zip/py4j/java_gateway.py", line 1257, in __call__
  File "/usr/lib/spark/python/pyspark/sql/utils.py", line 63, in deco
    return f(*a, **kw)
  File "/usr/lib/spark/python/lib/py4j-0.10.7-src.zip/py4j/protocol.py", line 328, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o90.load.
: java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(DriverManager.java:315)
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$6.apply(JDBCOptions.scala:105)
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$6.apply(JDBCOptions.scala:105)
    at scala.Option.getOrElse(Option.scala:121)
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:104)
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:35)
    at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:32)
    at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:318)
    at org.apache.spark.sql.DataFrameReader.loadV1Source(DataFrameReader.scala:223)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:211)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:167)
    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:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)

您需要执行以下步骤将Presto连接到Pyspark。

  1. 从官方网站下载Presto JDBC驱动程序罐,并将其放入Spark Master节点。

  2. 使用以下参数启动pyspark shell:

bin/pyspark --driver-class-path com.facebook.presto.jdbc.PrestoDriver --jars /path/to/presto/jdbc/driver/jar/file
  1. 尝试使用Spark
  2. 连接到Presto
jdbcDF = spark.read 
    .format("jdbc") 
    .option("driver", "com.facebook.presto.jdbc.PrestoDriver")   <-- presto driver class
    .option("url", "jdbc:presto://<machine_ip>:8080/hive/default") 
    .option("user", "airflow") 
    .option("dbtable", "may30_1") 
    .load()

相关内容

  • 没有找到相关文章