python连接Hive数据库

import pandas as pd
from impala.dbapi import connect

class Hive:
    def __init__(self, host, port=1050):
        self.conn = connect(host, port)
        self.cursor = self.conn.cursor()

    def close(self):
        self.cursor.close()
        self.conn.close()

    def dataframe(self, sql):
        return pd.read_sql(sql, self.conn)

class DataQuery:
    def __init__(self):
        self.hive = Hive('120.0.12.23')

class EvaluationModel:

    def __init__(self):
        self.dataquery = DataQuery()
        self.hive = Hive('120.0.12.23')
        
    def get_fnid(self,left,right,top,bottom):
        sql = 'select fnid from sts.fishnet where grow_4326>="{}" and grow_4326 <="{}" and gcol_4326>="{}" and gcol_4326<="{}"'.format(left,right,top,bottom)
        fnid_info = self.hive.dataframe(sql)
        # print(fnid_info)
        fnid  = []                          #得到fnid每一个列表,写进文件
        fnid.append(custid)
        for row in fnid_info.values:
            fnid_list.append(row[0])     #得到fnid的总列表
            fnid.append(row[0])
        with open('fnid_jihe_1.csv', 'a+') as f:
            for line in fnid:
                f.write(line + ',')
            f.write('\n')

 if __name__ == '__main__':
     EM = EvaluationModel()
     EM.get_fnid()

你可能感兴趣的:(python,python,hive,数据库)