通过ICE接口对hbase进行文件操作

最近开发了几个对集群做文件操作的模块,都是基于python(2.7)。下面记录一下对通过ICE接口,写HBASE的模块,贴出部分代码。


1. 关于ICE

ICE是一个中间件,我们可以通过ICE,不考虑server和client分别是什么,进行跨集群的操作。ice已经有很多资料可供学习,以后也会自己总结一个关于ice的文档


2. 依赖包

ICE


3. 源码及解释

模块实现的基本内容就是,连接ice服务,按照一定的json格式,将要操作的文件信息及路径传给ice,定的接口json如下:


zip源:
{
    "taskId":"asdfsdafdsf2o3r",
    "files": ["aaa/bbb/a.txt", "aaa/ccc/b.txt"],
    "parent": "/usr/data/input/1/aaa.zip",
    "parentType": "zip"
}

目录源:
{
    "taskId":"asdfsdafdsf2o3r",
    "files": ["aaa/bbb/a.txt", "aaa/ccc/b.txt"],
    "parent": "/usr/data/input/1/",
    "parentType": "nfs"
}

入库结果:
{
    "taskId":"asdfsdafdsf2o3r",
    "parent": "/usr/data/input/1/aaa.zip",
    "result": [
        {"file":"aaa/bbb/a.txt","isSuccess":false,"reason":"putData to hbase failed","length":1234, "costTime":500},
        {"file":"aaa/ccc/b.txt","isSuccess":true,"reason":"","length":1234, "costTime":500}]
}

实现代码段:

def write_hbase(self):
        cfgs = self.configs.configs.app.hbasewriter
        for category in self.contents:
            try:
                self.log_info('begin to write_hbase resource {}', category)
                content_info = self.contents[category]

                msg = channel.Message(data=channel.MessageData())
                uuid_str = uuid.uuid4().hex
                content = Info(taskId=uuid_str, files=content_info.files, parent=content_info.parent,parentType=content_info.parentType)
                msg.content = content.encode()
                msg.data.text = ''
                msg.flag = 0

                result = channel.sync_call(cfgs.proxy, msg)
                result_content = Info().decode(result.content)

                if result_content.result:
                    for res in result_content.result:
                        print('result is {}'.format(res))
                        if trans_bool(res.isSuccess):
                            self.log_info('write_hbase sucess, file: {}, costTime: {}, length: {}'
                                          .format(res.file, res.costTime, res.length))
                        else:
                            self.log_error('write_hbase fail, file: {}, reason: {}'
                                          .format(res.file, res.reason))
                else:
                    self.log_error('get basic data from interface error:{}', result_content.error)
            except:
                self.log_exception('error happen in write_hbase')


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值