【Python】使用pyhive提取数据时同时显示日志

To simultaneously display logs and extract data using PyHive, you need to establish a connection with your Hive server and then fetch the data and logs concurrently. Here is a detailed step-by-step guide for achieving this task using Python and the PyHive library:

  1. First, install the required Python libraries. You can do this using pip:
pip install pyhive
pip install thrift
pip install sasl
pip install thrift-sasl
  1. Import the required libraries in your Python script:
from pyhive import hive
from TCLIService.ttypes import TOperationState
  1. Define the connection parameters and establish the connection to your Hive server using hive.Connection class:
conn = hive.Connection(
    host='your_host',
    port=your_port,
    username='your_username',
    password='your_password',
    auth='CUSTOM',  # Use 'LDAP' or 'KERBEROS' if applicable
    database='your_database'
)
  1. Now, create a function to simultaneously fetch logs and data:
def execute_hive_query_and_fetch_logs(conn, query):
    cursor = conn.cursor()
    cursor.execute(query, async_=True)

    status = cursor.poll().operationState

    while status in (TOperationState.INITIALIZED_STATE, TOperationState.RUNNING_STATE):
        logs = cursor.fetch_logs()

        for message in logs:
            print(message)  # Print log messages

        status = cursor.poll().operationState

    result = cursor.fetchall()
    cursor.close()
    return result
  1. Use the above function to execute your Hive query and fetch logs and data concurrently:
query = "SELECT * FROM your_table;"
result = execute_hive_query_and_fetch_logs(conn, query)

print("Result:")
for row in result:
    print(row)


Replace 'your_host', 'your_port', 'your_username', 'your_database', and 'your_table' with appropriate values as per your setup.

This code will fetch logs and data simultaneously while executing the Hive query.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值