设为首页 加入收藏

TOP

关于python在爬虫scrapy框架,使用happybase方法链接hbase进行数据上传操作
2019-02-19 13:44:59 】 浏览:103
Tags:关于 python 爬虫 scrapy 框架 使用 happybase 方法 链接 hbase 进行 数据 上传 操作
  • 如果有集群先将hbase的集群启起来
  • python代码如下
import happybase
import MySQLdb
#pipelines.py文件里面类
覆盖原有类 Pachong_qcwyPipeline
class Pachong_qcwyPipeline(object):
	#初始化
    def __init__(self):
    	#同时导入mysql,mysql链接
        self.conn = MySQLdb.Connect(
            host='localhost',
            port=3306,
            user='root',
            password='123',
            db="xm",
            charset='utf8'
        )
        #mysql指针
        self.cursor = self.conn.cursor()
        #调用happybase.Connection输入服务器(虚拟安装Hbase服务器的ip,端口------终端查询ifconfig)
        self.connection = happybase.Connection(host='192.168.93.10', port=9090)
       #打开hbase
        self.connection.open()
        #打开创建好的数据库ai129下的表pc1
        self.table = self.connection.table('ai129:pc1')
        self.a=0
        #覆盖scrapy下的方法
    def process_item(self, item, spider):
        """职位,月薪,公司,经验,学历,公司地点,岗位描述,
                       公司规模,公司官网,公司主营业务"""
               控制导入mysql的数量50条        
        if self.a <= 50:
            self.a += 1
            self.insert(
                [item['zw'], item['yx'], item['gs'], item['jy'], item['xl'], item['dd'], item['ms'], item['gm'],
                 item['gw'],item['yw'], item['ddq'], item['gjc'], item['by1']])
            return item
        #之后导入hbase
        else:
            try:
            	#大批量导入 self.table.batch
                with self.table.batch(transaction=True) as table1:
                    print('进入HBASE')
                    #拼接rowkey-----ky
                    ky = item['ddq'] + ':' + item['by1'] + ':' + item['gjc'] + ':' + item['gs'] + ':' + item['yx'] + ':' +item['zw']
                    #值拼接
                    wy = 'item['dd']+':'+item['ms']+':'+item['gm']+'+'+item['gw']+':'+item['yw']
                    #lc1,lc2都是列簇
                    table1.put(ky, {'lc1:zw': item['zw'],'lc2:wy':wy,'lc1:gs': item['gs'],'lc1:yx': item['yx'],'lc1:jy': item['jy'],'lc1:xl': item['xl']})
            except:
                pass
    def insert(self, datas):
        sql = "INSERT INTO lagouwang(zw,yx,gs,jy,xl,dd,ms,gm,gw,yw,ddq,gjc,by1) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        self.cursor.execute(sql, datas)
        self.conn.commit()

小弟不才,大神可以多多指点,没有批评就没有成长。。。。。。。。。
自己原创作品,转载请携带连接

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇影响HBase insert性能的几个因素 下一篇Hbase和Hive整合,Hive读Hbase的..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目