我想使用 impala 计算 table 中的总记录数并将其存储到文件中。但不知道该怎么做

I want to count total records in a table using impala and store it into a file .But not sure how to do it

我在一个文件中有一个 table 的列表:tables.txt
内容是:

db.tab1
db.tab2

等等 现在我将它传递给一个函数并计算每个 table.

中的记录数
def rec_count(table_name):
    impala_cmd_cnt = "impala-shell -i %s -q  'select count(*) from %s'"%(impala_node,table_name)
    impala_cmd_res = os.system(impala_cmd_cnt)

impala_cmd_res 包含执行代码,即 0 表示成功,非零表示失败。
但我想将 table 的计数存储在一个变量中。该怎么做?

所以我找到了答案。我能够使用以下命令获得上述命令的输出 subprocess module.

show_create = "impala-shell -i  %s -q 'show create table %s'" % (impala_node, table_name)
        impala_show_cre_res = subprocess.Popen(show_create, stdout=subprocess.PIPE, shell=True)
        create_out = impala_show_cre_res.communicate()

create_out 包含输出。所以我使用正则表达式从输出中删除了需要的内容。