ali表格存储之更新,删除,创建

ali表格存储更新数据表:

目前我所了解到的,如果想要更新某一条数据,首先要获取到它的所有主键进行标识才可以进行更新,为了使用方便,我特意为更新自己写了一个函数:

ots_client = OTSClient(OTS_END_POINT, AL_AccessKeyId, AL_AccessKeySecret, OTS_INS)

def update_obt(self,uid, oem_name, **extra_fields):
    primary_key = [('uid', uid), ('oem_name', oem_name)]#主键
    update_of_attribute_columns = {
        'PUT': list((k, v) for k, v in extra_fields.items() if v or v in (0, 0.0)),
    }#extra_fields 即你想要更新的字段
    try:
        row = Row(primary_key, update_of_attribute_columns)
        condition = Condition(RowExistenceExpectation.EXPECT_EXIST) #设置验证条件,只有当满足该主键内容的数据存在时才进行修改,如果没有这个验证条件,当主键内容数据表中找不到时,他将会新建一条新的数据。
        consumed, return_row = ots_client.update_row(table_name=self.table_name,
                                                           row=row,
                                                           condition=condition,
                                                           return_type=ReturnType.RT_PK#返回内容,目前只支持返回none和主键
)

    except OTSClientError  as  e:
        return False, e.get_error_message()
    except OTSServiceError as e:
        return False, e.get_error_message()
    except Exception as e:
        return False, e
    return True, return_row

ali表格存储删除数据:

ots_client = OTSClient(OTS_END_POINT, AL_AccessKeyId, AL_AccessKeySecret, OTS_INS)

def delete_obt(self, merchant_id, user_id):
    primary_key = [('merchant_id', merchant_id), ('user_id', user_id)]
    row = Row(primary_key)
    try:
        consumed, return_row = ots_client .delete_row(self.table_name, row, None)
        if consumed.write > 0:
            return True, 'Delete succeed, consume %s write cu.' % consumed.write
        else:
            return False, u'没有找到该用户!'
    except OTSClientError as e:
        return False,
        "update row failed, http_status:%d, error_message:%s" % (e.get_http_status(), e.get_error_message())
    except OTSServiceError as e:
        return False, "update row failed, http_status:%d, error_code:%s, error_message:%s, request_id:%s" % (e.get_http_status(), e.get_error_code(), e.get_error_message(), e.get_request_id())
    except Exception as e:
        return False, u'参数异常%s'%e

ali表格存储增加数据:

ots_client = OTSClient(OTS_END_POINT, AL_AccessKeyId, AL_AccessKeySecret, OTS_INS)

def create_obt(self, uid, oem_name, **extra_fields):
    primary_key = [('uid', uid), ('oem_name', oem_name)]
    now_mktimt = int(time.mktime(timezone.now().timetuple()))
    attribute_columns = [
        ('create_time', now_mktimt),
        ('write_time', now_mktimt),
        ('logo_img', extra_fields.get('logo_img', '')),
        ('service_tel', extra_fields.get('service_tel', '')),
        ('tagline', extra_fields.get('tagline', '')),
        ('note', extra_fields.get('note', '')),
        ('status', extra_fields.get('status', 1))
    ]
    try:
        row = Row(primary_key, attribute_columns)
        condition = Condition(RowExistenceExpectation.EXPECT_NOT_EXIST)
        consumed, return_row = ots_client .put_row(table_name=self.table_name,
                                                        row=row,
                                                        condition=condition,
                                                        return_type=ReturnType.RT_PK
                                                        )
    except OTSClientError  as  e:
        return False, e.get_error_message()
    except OTSServiceError as e:
        return False, e.get_error_message()
    except Exception as e:
        return False, e
        # print('Put succeed, consume %s write cu.' % consumed.write)
    return True, oem_name

你可能感兴趣的:(ali服务)