用python把hbase数据库中的数据读取出来保存到txt文件并存入到hive中

如何通过python从txt文件把数据导入到hive中

通过sql语句,以’\t’为划分符号进行划分

代码如下:

import happybase
import numpy as np
import pandas as pd
from pyhive import hive
conn = hive.connect(host='192.168.43.148', port=10000, auth='NOSASL', username='沙粒',database='movie')
cursor = conn.cursor()
d_sql='drop table word'
c_sqr="create table if not exists movie.word(id int,name string,age int,tel string) ROW FORMAT DELIMITED FIELDS TERMINATER BY '\t' STORED AS TEXTFILE"
connection = happybase.Connection('192.168.43.148',9090) 
table = connection.table('final_movie2')
k=[]
d=[]
word_count=[]
for key, data in table.scan():
    temp=key.decode('utf-8')
    t=temp.split(",")
    if " "in t[1]:
        continue
    k.append(t)
    temp2=list(data.values())
    t2=int(temp2[0].decode('utf-8'))
    d.append(t2)
    word_count.append([t[0],t[1],t2])

# f=open("word.txt",encoding="utf-8",mode="w")
# for w in word_count:
#     for m in w:
#         f.write(str(m)+'\t')
#     f.write("\n")
# f.close()

#print(word_count)
s_sql='load data local inpath "word.txt" into table final_word'
#cursor.execute(d_sql)

cursor.execute(s_sql)
cursor.execute('select * from final_word')
result = cursor.fetchall()
print(result)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值