Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python odbc游标:在执行查询后保持持久状态_Python_Cursor_Odbc_Hive - Fatal编程技术网

Python odbc游标:在执行查询后保持持久状态

Python odbc游标:在执行查询后保持持久状态,python,cursor,odbc,hive,Python,Cursor,Odbc,Hive,假设表1位于数据库中 import pyodbc connection = pyodbc.connect(dsn='hive', autocommit=True) cursor = connection.cursor() cursor.execute("USE database_1") cursor.execute("SELECT * FROM table_1") 这将产生一个TableNotFound错误,因为当光标执行下一个查询时,我们已经将正在使用的数据库重置回默认值。有没有办法保持状

假设表1位于数据库中

import pyodbc
connection =  pyodbc.connect(dsn='hive', autocommit=True)
cursor = connection.cursor()
cursor.execute("USE database_1")
cursor.execute("SELECT * FROM table_1")

这将产生一个TableNotFound错误,因为当光标执行下一个查询时,我们已经将正在使用的数据库重置回默认值。有没有办法保持状态一致/将多个查询捆绑到execute语句中以避免这种情况?我特别感兴趣的是能够设置映射器/还原器的数量,并且能够在执行下一个查询时保持这种状态。我知道另一种方法是使用Python使shell连接到配置单元并执行hql文件,但我不希望这样做。

我建议您尝试以下几项:

  • 我认为在大多数情况下,如果不是所有情况,可以使用连接字符串指定要使用的数据库

  • 我看到“execute”命令返回光标本身,尽管我会尝试:

  • cursor.execute(“使用数据库_1”).execute(“从表_1中选择*)

    (以防记录错误)

  • 这实际上可能会起作用:
  • cursor.execute(“使用数据库_1”)

    cursor.commit()

    cursor.execute(“从表1中选择*)


    如果这有效,请更新。

    从我所知道的
    pyodbc
    文档来看,似乎没有对配置单元的特定支持。如果您愿意使用不同的库,请特别支持与HiveServer2(我认为是Hive0.11或更高版本)的连接。它可以与pip一起安装(
    pip install pyhs2
    ),但至少在我的Mint Linux 17机器上,我还必须先安装
    libpython dev
    libsasl2 dev

    我在Hive中模拟了您的场景的一个微不足道的近似值(
    table_1
    inside
    database_1
    但不是
    default
    ):

    因此,这是一个利用
    pyhs2
    连接到Hive的基本脚本:

    # Python 2.7
    import pyhs2
    from pyhs2.error import Pyhs2Exception
    
    hql = "SELECT * FROM table_1"
    with pyhs2.connect(
      host='localhost', port=10000, authMechanism="PLAIN", user="root",
      database="default"  # Of course it's possible just to specify database_1 here
    ) as db:
      with db.cursor() as cursor:
    
        try:
          print "Trying default database"
          cursor.execute(hql)
          for row in cursor.fetch(): print row
        except Pyhs2Exception as error:
          print(str(error))
    
        print "Switching databases to database_1"
        cursor.execute("use database_1")
        cursor.execute(hql)
        for row in cursor.fetch(): print row
    
    这是结果输出:

    Trying default database
    "Error while compiling statement: FAILED: SemanticException [Error 10001]: Line 1:14 Table not found 'table_1'"
    Switching databases to database_1
    ['this']
    ['is']
    ['some']
    ['sample']
    ['data']
    
    正如我在代码的注释行中所指出的,完全可以直接启动与
    数据库1
    的连接,而不是
    默认值
    ,但我想尝试模仿您在问题中发布的代码所做的操作(并演示在连接启动后切换数据库的能力)


    无论如何,如果你愿意接受一个非pyodbc的解决方案,希望能引起大家的思考。

    我了解到你可以设置ODBC连接字符串中的减缩器数量,例如

    string = 'dsn=hive/driver/path;mapred.reduce.tasks=100;....'
    connection = pyodbc.connect(string, autocommit=True)
    

    这允许您在连接中使用所需的特定设置;这并不能解决切换数据库的问题,但它解决了将设置引入Hive的其他情况,这是我的主要问题。

    不确定第一条语句的含义,但第二条两个选项不起作用。
    string = 'dsn=hive/driver/path;mapred.reduce.tasks=100;....'
    connection = pyodbc.connect(string, autocommit=True)