抛出JVM问题的Python JDBC连接问题

时间:2017-06-20 11:56:11

标签: python jdbc jvm jpype jaydebeapi

我正在尝试使用Python jaydebeapi连接到Impala数据库。当我两次调用连接类时,我正面临着JVM问题。请在下面找到我的连接类和Sql_Query类。

Connection_Class:

import jaydebeapi
import jpype
import datetime
import ConfigParser
from fileinput import close

config = ConfigParser.RawConfigParser ( )
config.read ( 'ConfigFile.properties' )


def Impala_Connection(sql_query):
    conn_impala = None


    try:
        jars_location = config.get ( 'Jars_info' , 'Jars_Location' )
        args = "-Djava.class.path=%s" % jars_location
        jvm_path = jpype.getDefaultJVMPath ( )
        jpype.startJVM ( jvm_path , args )

    except IOError as err:

        print('An error occurred trying to read the file:{}".format(e)')
        sql_query.close ( )
    else:

        try:

            print "Start executing: " + sql_query + " at " + str (
            datetime.datetime.now ( ).strftime ( "%Y-%m-%d %H:%M" ) ) + "\n"
            url = config.get ( 'Jars_info' , 'Jdbc_Url' )
            Jdbc_Driver_Class = config.get ( 'Jars_info' , 'Jdbc_Driver_Class' )
            username = config.get ( 'Jars_info' , 'username' )
            password = config.get ( 'Jars_info' , 'password' )
            jdbc_jar_location = config.get ( 'Jars_info' , 'Jdbc_Jar_Location' )
            conn_impala = jaydebeapi.connect ( Jdbc_Driver_Class , url , {username , password} , jdbc_jar_location )
            curs = conn_impala.cursor ( )
            sql_execution = curs.execute ( sql_query )
            data = curs.fetchall ( sql_execution )
            curs.close()
            return (data)

        except Exception, err:
            print("Something went wrong with Impala Connection: {}".format(err))
        finally:
            close(conn_impala)
            jpype.shutdownJVM()

Sql_Query_Class:

from pyspark import SparkConf, SparkContext
from com.my.common_funcitons.Impala_Query_Executor import Impala_Connection
import sys

conf = SparkConf().setAppName("pyspark")
sc = SparkContext(conf=conf)


tbl_name = sys.argv[1]
refid = sys.argv[2]
metadata_Query="SELECT * from Metadata_Table TABLE_NAME='%s' and TEMP.unique_id=%s" %(tbl_name,refid)
metadata_info=Impala_Connection(metadata_Query)

if len(metadata_info) == 0:
    new_tbl_name = tbl_name+"_%"
    metadata_Query="SELECT * from Metadata_Table TABLE_NAME='%s' and TEMP.unique_id=%s"" (new_tbl_name,refid)
    metadata_info=Impala_Connection(metadata_Query)
    for row in metadata_info:
        metadata_no_of_columns=row[0]
        metadata_table_id=row[1]

    else:

    for row in metadata_info:
        metadata_no_of_columns=row[0]
        metadata_table_id=row[1]

我有两种类型的表名,如table和table_000,所以如果我得到空数据我需要添加table_%并要求相同。当我从同一个类调用Impala连接我得到JVM问题请找到错误堆栈如下:

File "/usr/lib64/python2.7/site-packages/jpype/_core.py", line 50, in startJVM
    _jpype.startup(jvm, tuple(args), True)
RuntimeError: Unable to start JVM at native/common/jp_env.cpp:78

我尝试在最终课程中添加JVMshutdown但仍面临问题。请给我一个解决方案。

2 个答案:

答案 0 :(得分:2)

使用jpype.isJVMStarted()函数我可以检查是否启动了jvm我解决了我的问题。

答案 1 :(得分:0)

当我尝试解决此问题时,我检查了在进行jaydebeapi连接之前是否正在与HDFS系统进行pyarrow连接。我意识到这个问题的顺序很重要,如果您以相反的方式进行连接(Jaydebeapi然后是pyarrow连接),它将起作用。

不知道这是否与您的问题有关,但这可以帮助减轻其他人的问题。