happybase demo

def save(projectCode,featureId, featureValue, featureTime,updateCycle,scriptName,placeholderDim1,placeholderDim2,placeholderDim3,placeholderDim4):
    hostname = '172.16.24.75'
    hbaseTableName = 'feature_repo_debug'
    updateCycleStr='hourly,daily,weekly,monthly,quarterly,yearly'
    updateCycleList = []
    updateCycleArray = updateCycleStr.split(",")
    for item in updateCycleArray:
        updateCycleList.append(item)
    conn = happybase.Connection(host=hostname,port=9090,protocol='compact',transport='framed')
    table = conn.table(hbaseTableName)
    #字段非空判断
    if projectCode is  None:
        raise Exception('参数1,项目编码 字段不能为空!')
    if featureId is  None:
        raise Exception('参数2,指标id 字段不能为空!')
    if featureValue is  None:
        featureValue = None
    if featureTime is  None:
        raise Exception('参数4,指标时间维度值 字段不能为空!')
    if updateCycle is  None:
        raise Exception('参数5,更新周期 字段不能为空!')
    elif updateCycle not in updateCycleList:
        raise Exception('参数5,更新周期 的值必须在'+ updateCycle + '中!')
    if scriptName is  None:
        raise Exception('参数6,脚本名称 不能为空!')
    timestamps = time.localtime()
    createdAt = time.strftime("%Y-%m-%d %H:%M:%S", timestamps)
    row_key = str(uuid.uuid4())
#     conn = happybase.Connection(hostname)
#     table = conn.table(hbaseTableName)
#     conn.close()
    return table.put(row_key, {'cf1:project_code':projectCode, 'cf1:feature_id':featureId,'cf1:feature_value':featureValue,'cf1:feature_time':featureTime,'cf1:update_cycle':updateCycle,'cf1:script_name': scriptName,'cf1:placeholder_dim_1': placeholderDim1, 'cf1:placeholder_dim_2': placeholderDim2,'cf1:placeholder_dim_3':placeholderDim3,'cf1:placeholder_dim_4':placeholderDim4,'cf1:created_at':createdAt})

def batchsave(list1):
    hostname = '172.16.24.75'
    hbaseTableName = 'feature_repo_debug'
    updateCycleStr='hourly,daily,weekly,monthly,quarterly,yearly'
    updateCycleList = []
    updateCycleArray = updateCycleStr.split(",")
    for item in updateCycleArray:
        updateCycleList.append(item)
    conn = happybase.Connection(host=hostname,port=9090,protocol='compact',transport='framed')
    table = conn.table(hbaseTableName)
    with table.batch() as bat:
        for item in list1:
#             array = item.split(',')
            projectCode = item[0]
            featureId = item[1]
            featureValue = item[2]
            featureTime = item[3]
            updateCycle = item[4]
            scriptName = item[5]
            placeholderDim1 = ''
            placeholderDim2 = ''
            placeholderDim3 = ''
            placeholderDim4 = ''
            hostname = '172.16.24.75'
            hbaseTableName = 'feature_repo_debug'
            updateCycleStr='hourly,daily,weekly,monthly,quarterly,yearly'
            updateCycleList = []
            updateCycleArray = updateCycleStr.split(",")
            for item in updateCycleArray:
                updateCycleList.append(item)
            conn = happybase.Connection(host=hostname,port=9090,protocol='compact',transport='framed')
            table = conn.table(hbaseTableName)
            #字段非空判断
            if projectCode is  None:
                raise Exception('参数1,项目编码 字段不能为空!')
            if featureId is  None:
                raise Exception('参数2,指标id 字段不能为空!')
            if featureValue is  None:
                featureValue = None
            if featureTime is  None:
                raise Exception('参数4,指标时间维度值 字段不能为空!')
            if updateCycle is  None:
                raise Exception('参数5,更新周期 字段不能为空!')
            elif updateCycle not in updateCycleList:
                raise Exception('参数5,更新周期 的值必须在'+ updateCycle + '中!')
            if scriptName is  None:
                raise Exception('参数6,脚本名称 不能为空!')
            timestamps = time.localtime()
            createdAt = time.strftime("%Y-%m-%d %H:%M:%S", timestamps)
            row_key = str(uuid.uuid4())
            #     conn = happybase.Connection(hostname)
            #     table = conn.table(hbaseTableName)
            #     conn.close()
            table.put(row_key, {'cf1:project_code':projectCode, 'cf1:feature_id':featureId,'cf1:feature_value':featureValue,'cf1:feature_time':featureTime,'cf1:update_cycle':updateCycle,'cf1:script_name': scriptName,'cf1:placeholder_dim_1': placeholderDim1, 'cf1:placeholder_dim_2': placeholderDim2,'cf1:placeholder_dim_3':placeholderDim3,'cf1:placeholder_dim_4':placeholderDim4,'cf1:created_at':createdAt})
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值