Example #1
0
File: ots.py Project: hatlonely/ops
def update_row(client, table, row, change=False, view=True):
    if not change and view:
        res = get_row(client, table, row["PrimaryKeys"])
        to_delete_keys = []
        for col in res["Columns"]:
            if col not in row["Columns"]:
                to_delete_keys.append(col)
        for key in to_delete_keys:
            del (res["Columns"][key])
        diff.color_diff(res, row)
        return False
    if not change:
        return False
    # 如果行不存在,插入行
    # 如果行存在,修改或者新增 row 中的行,row 中不存在的列不变
    meta = client.describe_table(table)
    keys = [v[0] for v in meta.table_meta.schema_of_primary_key]
    pks = []
    for key in keys:
        pks.append((key, row["PrimaryKeys"][key]))
    cols = []
    for col in row["Columns"]:
        cols.append((col, row["Columns"][col]))
    _, _ = client.update_row(
        table, tablestore.Row(pks, {"PUT": cols}),
        tablestore.Condition(tablestore.RowExistenceExpectation.IGNORE))
    return True
Example #2
0
 def update(self, index, column, where=None, logical_operator=None,
            table_name=None, condition=None, return_type=None):
     """
     :param list or tuple index: Primary key. Eg: [('ActID', 'abc')]
     :param list or tuple column: Attribute column. Eg: [('PassiveParty', 'def', time.time() * 1000)]
     :param list or tuple where: Filter. Eg: [('StartTime', ComparatorType('>='), 0)]
     :param LogicalOperator logical_operator: LogicalOperator('AND')
     :param str table_name: Table name
     :param OTSRowCondition condition: Condition of existence of row
     :param OTSReturnType return_type: API return content
     :return:
     """
     column_filter = None
     if table_name is None:
         table_name = self.table_name
     if logical_operator is None:
         logical_operator = LogicalOperator('AND')
     if where is not None:
         if not isinstance(where[0][1], ComparatorType):
             raise InvalidParam(doc=ComparatorType)
         column_filter = ts.CompositeColumnCondition(logical_operator.OTS)
         tuple(map(lambda c: column_filter.add_sub_condition(
             ts.SingleColumnCondition(c[0], c[2], c[1].OTS, *c[3:])), where))
     if condition is None:
         condition = default.ROW_CONDITION
     elif not isinstance(condition, OTSRowCondition):
         raise InvalidParam(doc=OTSRowCondition)
     if return_type is not None and not isinstance(return_type, OTSReturnType):
         raise InvalidParam(doc=OTSReturnType)
     row = self.format_update_row(index, column)
     condition = ts.Condition(condition, column_filter)
     _, return_row = self.client.update_row(table_name, row, condition, return_type)
     return StoreData(getattr(return_row, 'primary_key', None))
Example #3
0
def insert_row(row):
    condition = tablestore.Condition(tablestore.RowExistenceExpectation.IGNORE)

    for i in range(len(row)):
        try:
            if (i == 0):
                consumed, return_row = ots_client.put_row(
                    table_name, row[i], condition)
            else:
                consumed, return_row = ots_client.update_row(
                    table_name, row[i], condition)
        except tablestore.OTSClientError as e:
            print(e.get_error_message())
            return (-1)
        except tablestore.OTSServiceError as e:
            print(e.get_error_message())
            return (-1)
    return (0)
Example #4
0
 def insert(self, index, column=None, table_name=None, condition=None, return_type=None):
     """
     :param list or tuple index: Primary key. Eg: [('ActID', 'abc')]
     :param list or tuple column: Attribute column. Eg: [('StartTime', 0)]
     :param str table_name: Table name
     :param OTSRowCondition condition: Condition of existence of row
     :param OTSReturnType return_type: API return content
     :return:
     """
     if table_name is None:
         table_name = self.table_name
     if condition is None:
         condition = default.ROW_CONDITION
     elif not isinstance(condition, OTSRowCondition):
         raise InvalidParam(doc=OTSRowCondition)
     if return_type is not None and not isinstance(return_type, OTSReturnType):
         raise InvalidParam(doc=OTSReturnType)
     row = ts.Row(index, column)
     condition = ts.Condition(condition)
     _, return_row = self.client.put_row(table_name, row, condition, return_type)
     return StoreData(getattr(return_row, 'primary_key', None))
Example #5
0
    def update_row(self, primary_key, update_data):
        # primary_key 是主键,如[('study_id','10.1038/s41467-019-10756-2'), ('cell_id','human_control-AAACCTGAGCTGAAAT'),('user_id',3)]
        # updtae_data 是待更新的列的list,[(col1,val1),(col2,val2)]
        try:
            consumed, return_row, next_token = self._Ali_client.get_row(
                self._tablename, primary_key, columns_to_get=['donor_gender'])
            if return_row == None:
                print("Error! This row doesn't exist in the table.")
            else:
                # convert update data to blockes
                row = []
                for i in range(
                        len(update_data) // 1024 + 1
                ):  # the maxium number of attribute columns in each writing operation is 1024
                    row.append(
                        tablestore.Row(
                            primary_key, {
                                'PUT':
                                update_data[i *
                                            1024:min(i * 1024 +
                                                     1024, len(update_data))]
                            }))

                # try to update data
                condition = tablestore.Condition(
                    tablestore.RowExistenceExpectation.IGNORE)
                for i in range(len(row)):
                    try:
                        consumed, return_row = self._Ali_client.update_row(
                            self._tablename, row[i], condition)
                    except tablestore.OTSClientError as e:
                        print(e.get_error_message())
                    except tablestore.OTSServiceError as e:
                        print(e.get_error_message())

        except tablestore.OTSClientError as e:
            print(e.get_error_message())
        except tablestore.OTSServiceError as e:
            print(e.get_error_message())
Example #6
0
 def put(self, data):
     c, rt = self.__client.put_row(self.__model['table_name'],
                                   toTSRow(self.__model, data),
                                   tablestore.Condition('IGNORE'),
                                   return_type=tablestore.ReturnType.RT_PK)
     return c, toPyDict(self.__model, rt)
Example #7
0
 def delete(self, data):
     row = toTSRow(self.__model, data)
     newrow = tablestore.Row(row.primary_key)
     return self.__client.delete_row(self.__model['table_name'], newrow,
                                     tablestore.Condition('IGNORE'))