大数据学习-python通过Pyhive连接hive数据库
生活随笔
收集整理的這篇文章主要介紹了
大数据学习-python通过Pyhive连接hive数据库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.hbase和hive結合
(1)hbase建表添加數據
#test是表名,name是列族 #hbase可以一個列族里邊多個字段 create 'test','name'#添加數據 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上創建外部表,映射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");測試,兩個平臺數據是否相通。且數據同步更新。
2.hive連接和并用pandas讀取數據
(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)啟動hive
hive --service metastore & hiveserver2 &(3)讀取數據
from pyhive import hive import pandas as pd conn = hive.Connection(host = IP地址, port = 10000, username = 'hive') #host主機ip,port:端口號,username:用戶名,database:使用的數據庫名稱cursor = conn.cursor() cursor.execute('show databases')# 打印結果 for result in cursor.fetchall():print(result) 或者pandas讀取 sql = 'select * from default.employees'df = pd.read_sql(sql,conn)?
總結
以上是生活随笔為你收集整理的大数据学习-python通过Pyhive连接hive数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言抽签系统_抽签小程序(C语言随机数
- 下一篇: cesium 页面截图_Cesium开发