大数据学习-python通过Pyhive连接hive数据库
生活随笔
收集整理的這篇文章主要介紹了
大数据学习-python通过Pyhive连接hive数据库
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.hbase和hive結(jié)合
(1)hbase建表添加數(shù)據(jù)
#test是表名,name是列族 #hbase可以一個(gè)列族里邊多個(gè)字段 create 'test','name'#添加數(shù)據(jù) put 'test','1','name:t1','1' put 'test','1','name:t2','2'#查詢 scan 'test'#查詢 get 表名,row-key,列族 get 'test','1','name:t1'#刪除表 disable 'test' drop 'test'#查看表信息 desc 'test'(2)在hive上創(chuàng)建外部表,映射hbase
CREATE EXTERNAL TABLE test( key string,t1 int, t2 int) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\u0001' STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,name:t1,name:t2") TBLPROPERTIES ("hbase.table.name" = "test", "hbase.mapred.output.outputtable" = "test");測(cè)試,兩個(gè)平臺(tái)數(shù)據(jù)是否相通。且數(shù)據(jù)同步更新。
2.hive連接和并用pandas讀取數(shù)據(jù)
(1)配置hive-site.xml文件
<property><name>hive.server2.thrift.bind.host</name><value>192.168.99.250</value> </property> <property><name>hive.server2.thrift.port</name><value>10000</value> </property>(2)啟動(dòng)hive
hive --service metastore & hiveserver2 &(3)讀取數(shù)據(jù)
from pyhive import hive import pandas as pd conn = hive.Connection(host = IP地址, port = 10000, username = 'hive') #host主機(jī)ip,port:端口號(hào),username:用戶名,database:使用的數(shù)據(jù)庫名稱cursor = conn.cursor() cursor.execute('show databases')# 打印結(jié)果 for result in cursor.fetchall():print(result) 或者pandas讀取 sql = 'select * from default.employees'df = pd.read_sql(sql,conn)?
總結(jié)
以上是生活随笔為你收集整理的大数据学习-python通过Pyhive连接hive数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言抽签系统_抽签小程序(C语言随机数
- 下一篇: python os renames_Py