关于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()

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

读书小孩

您的打赏将是我前行的动力源

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值