- 如果有集群先将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()
小弟不才,大神可以多多指点,没有批评就没有成长。。。。。。。。。
自己原创作品,转载请携带连接