如何使用JDBC将Impala表直接加载到Spark?

时间:2016-09-08 20:58:47

标签: jdbc apache-spark pyspark kerberos impala

我正在尝试使用Python编写一个spark作业,它将打开与Impala的jdbc连接,并将Impala直接从Impala加载到Dataframe中。这个问题非常接近但在scala:Calling JDBC to impala/hive from within a spark job and creating a table

我该怎么做?其他数据源有很多例子,例如MySQL,PostgreSQL等,但我还没有看到Impala + Python + Kerberos的数据源。一个例子会有很大的帮助。谢谢!

使用来自网络的信息尝试了这一点,但它没有用。

SPARK笔记本

#!/bin/bash
export PYSPARK_PYTHON=/home/anave/anaconda2/bin/python
export HADOOP_CONF_DIR=/etc/hive/conf
export PYSPARK_DRIVER_PYTHON=/home/anave/anaconda2/bin/ipython
export PYSPARK_DRIVER_PYTHON_OPTS='notebook --ip=* --no-browser'

# use Java8
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH

# JDBC Drivers for Impala
export CLASSPATH=/home/anave/impala_jdbc_2.5.30.1049/Cloudera_ImpalaJDBC41_2.5.30/*.jar:$CLASSPATH
export JDBC_PATH=/home/anave/impala_jdbc_2.5.30.1049/Cloudera_ImpalaJDBC41_2.5.30

# --jars $SRCDIR/spark-csv-assembly-1.4.0-SNAPSHOT.jar \
# --conf spark.sql.parquet.binaryAsString=true \
# --conf spark.sql.hive.convertMetastoreParquet=false

pyspark --master yarn-client \
        --driver-memory 4G \
        --executor-memory 2G \
        # --num-executors 10 \
        --jars /home/anave/spark-csv_2.11-1.4.0.jar $JDBC_PATH/*.jar
        --driver-class-path $JDBC_PATH/*.jar

Python代码

properties = {
    "driver": "com.cloudera.impala.jdbc41.Driver",
    "AuthMech": "1",
#     "KrbRealm": "EXAMPLE.COM",
#     "KrbHostFQDN": "impala.example.com",
    "KrbServiceName": "impala"
}

# imp_env is the hostname of the db, works with other impala queries ran inside python
url = "jdbc:impala:imp_env;auth=noSasl"

db_df = sqlContext.read.jdbc(url=url, table='summary', properties=properties)

我收到此错误消息(Full Error Log):
Py4JJavaError:调用o42.jdbc时发生错误。 :java.lang.ClassNotFoundException:com.cloudera.impala.jdbc41.Driver

4 个答案:

答案 0 :(得分:2)

您可以使用

--jars $(echo /dir/of/jars/*.jar | tr ' ' ',') 

而不是

--jars /home/anave/spark-csv_2.11-1.4.0.jar $JDBC_PATH/*.jar

或其他方法请参阅我的answer

答案 1 :(得分:1)

在群集库中设置 jar 后,在 Azure Databricks 笔记本中执行此操作。通常遵循上一篇文章,除了 d 是驱动程序配置的大写。效果很好。

properties = {
"Driver": "com.cloudera.impala.jdbc41.Driver"
}


db_df = spark.read.jdbc(url= 'jdbc:impala://hostname.domain.net:21050/dbname;AuthMech=3;UID=xxxx;PWD=xxxx', table ='product', properties = properties)

db_df.show()

答案 2 :(得分:0)

这对我有用:

spark-shell --driver-class-path ImpalaJDBC41.jar --jars ImpalaJDBC41.jar 

val jdbcURL = s"jdbc:impala://192.168.56.101:21050;AuthMech=0"

val connectionProperties = new java.util.Properties()

val hbaseDF = sqlContext.read.jdbc(jdbcURL, "impala_table", connectionProperties)

答案 3 :(得分:0)

第一种方法是在impala_jdbc_connection.py脚本(如spark-submit --driver-class-path /opt/cloudera/parcels/CDH-6.2.0-1.cdh6.2.0.p0.967373/jars/ImpalaJDBC41.jar --jars /opt/cloudera/parcels/CDH-6.2.0-1.cdh6.2.0.p0.967373/jars/ImpalaJDBC41.jar --class com.cloudera.impala.jdbc41.Driver impala_jdbc_connection.py

下使用spark-submit

impala_jdbc_connection.py

properties = {
"drivers": "com.cloudera.impala.jdbc41.Driver"
}

#initalize the spark session
spark = (
        SparkSession.builder
        .config("spark.jars.packages", "jar-packages-list")
        .config("spark.sql.warehouse.dir","hdfs://dwh-hdp-node01.dev.ergo.liferunoffinsuranceplatform.com:8020/user/hive/warehouse")
        .enableHiveSupport()
        .getOrCreate() 
        )

db_df = spark.read.jdbc(url= 'jdbc:impala://host_ip_address:21050/database_name', table ='table_name', properties = properties)

db_df.show()

第二种方法不是从impala直接导入spark,而是将结果转换为spark数据框

pip install impyla来源:https://github.com/cloudera/impyla

连接到impala并从impala数据库获取结果并将结果转换为spark数据框

from impala.dbapi import connect

conn = connect(host = 'IP_ADDRESS_OF_HOST', port=21050)

cursor = conn.cursor()

cursor.execute('select * from database.table')

res= cursor.fetchall() # convert res to spark dataframe

for data in res:
        print(data)