您的位置:首页 > 运维架构

hadoop和hive的实践应用(三)——hive的基本应用

2011-05-26 11:37 323 查看
前面我已经搭好了hadoop和hive环境,并且在hive中创建了表page,将数据load了进去。现在我想从这个表中统计每个url的流量,并放到其他关系中数据库中或者展现在页面上,怎么办?

去官网是看一下,不难发现可以用java ,python,php都可以实现,下面就用python 简单的写了写



from hive_service import ThriftHive
from hive_service.ttypes import HiveServerException
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol



urlcount={}
def getFlowbyHive():
try:
transport = TSocket.TSocket('219.*.*.200',10000)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = ThriftHive.Client(protocol)
transport.open()
client.execute("select URL, count(*) from page GROUP BY URL")
while (1):
row = client.fetchOne()
sp = row.split('/t')
if (len(sp)<2):
continue
if (row == None):
break
urlcount[sp[0]]=sp[1]
print sp[0],sp[1]
transport.close()

except Thrift.TException, tx:
print '%s' % (tx.message)



需要在219.*.*.200服务器上启动hive服务,用于python,java等程序连接hive,bin/hive --service hiveserver 10001不跟端口号默认10000支持nohup后台启动执行程序,完了urlcount中现在应经存上了url:count的键值对,入库或展现由你来处理了。

HWI是Hive Web Interface的简称,是hive cli的一个web替换方案。0.7默认是自带hwi的,conf/hive-default.xml文件都是默认值,在后台启动hwi服务

nohup bin/hive --service hwi > /dev/null 2> /dev/null &

浏览器中输入:http://10.20.151.7:9999/hwi/ 就可以访问,可以查看数据仓库,执行语句了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: