예제 #1
0
    def put_entity(self, table_name, row_key, column_names, cell_values):
        st = time.time()
        elist = [ERROR_HB]
        if (not row_key) or (not table_name):
            elist[0] += "Null row_key or table_name"
            return elist
        client = None
        try:
            if len(column_names) != len(cell_values):
                ttypes.IOError(
                    "Number of values does not match the number of columns")
            client = self.__initConnection()
            if self.__table_exist(table_name, client) == 0:
                columnlist = []
                for ii in column_names:
                    col = ttypes.ColumnDescriptor()
                    col.name = ii + ":"
                    col.maxVersions = 3
                    columnlist.append(col)

                client.createTable(table_name, columnlist)

            for ii in range(0, len(column_names)):
                m = ttypes.Mutation()
                m.column = column_names[ii] + ":"
                m.value = cell_values[ii]
                mutations = []
                mutations.append(m)
                client.mutateRow(table_name, row_key, mutations)
        except ttypes.IOError, io:
            if io:
                elist[0] += str(io)  # append the error
            else:
                elist[0] += "IO Error"
예제 #2
0
  def batch_delete(self, table_name, row_keys, column_names=[]):
    """ Remove a batch of rows.
     
    Args:
      table_name: Table to delete rows from
      row_keys: A list of keys to remove
      column_names: A list of column names
    Returns:
      Nothing
    Raises: 
      TypeError: Raised when given bad types of for args.
    """

    if not isinstance(table_name, str): raise TypeError("Expected str")
    if not isinstance(row_keys, list): raise TypeError("Expected list")


    all_mutations = []
    for row in row_keys:
      batch_mutation = ttypes.BatchMutation()
      mutations = []
      for col in column_names:
        m = ttypes.Mutation(isDelete=True)
        m.column = col + ":"
        mutations.append(m)
      batch_mutation.mutations = mutations
      batch_mutation.row = row
      all_mutations.append(batch_mutation) 
    client = self.__init_connection()
    client.mutateRows(table_name, all_mutations)
    self.__release_lock()
예제 #3
0
  def batch_put_entity(self, table_name, row_keys, column_names, cell_values):
    """Allows callers to store multiple rows with a single call.
   
    Args: 
      table_name: The table to mutate
      row_keys: A list of keys to store on
      column_names: A list of columns to mutate
      cell_values: A dict of key/value pairs
    Returns:
      Nothing 
    Raises:
      TypeError: Raised when given bad types of for args.
    """

    if not isinstance(table_name, str): raise TypeError("Expected str")
    if not isinstance(column_names, list): raise TypeError("Expected list")
    if not isinstance(row_keys, list): raise TypeError("Expected list")
    if not isinstance(cell_values, dict): raise TypeError("Expected dict")


    all_mutations = []
    for row in row_keys:
      batch_mutation = ttypes.BatchMutation()
      mutations = []
      for col in column_names:
        m = ttypes.Mutation()
        m.column = col + ":"
        m.value = cell_values[row][col]
        mutations.append(m)
      batch_mutation.mutations = mutations
      batch_mutation.row = row
      all_mutations.append(batch_mutation) 

    client = self.__init_connection()
    client.mutateRows(table_name, all_mutations)
    self.__release_lock()