Ejemplo n.º 1
0
 def add_attribute(self, key, value):
     check_none(key, "Attribute name is None.")
     if value is None and key in self.attributes:
         self.attributes.pop(key)
     else:
         self.attributes[to_bytes(key)] = to_bytes(value)
     return self
Ejemplo n.º 2
0
 def add_configuration(self, key, value):
     check_none(key, "Configuration key is None.")
     if value is None and key in self.configuration:
         self.configuration.pop(key)
     else:
         self.configuration[to_bytes(key)] = to_bytes(value)
     return self
Ejemplo n.º 3
0
def _column_format(family, qualifier):
    # type: (str, Union[None, str, List[str]]) -> Union[None, List[TColumn]]
    """
    Util method to get columns from given column family and qualifier.
    If the family is None, this method will return None.
    Args:
        family: name of column family.
        qualifier: name of column qualifier, it can be a str, None or a list of strs.

    Returns: a list of combined columns.

    """
    if family is None:
        return None
    if not isinstance(family, str) and not isinstance(family, bytes):
        raise ValueError(
            "A family name must be a str object, but got {}".format(
                type(family)))
    family_bytes = to_bytes(family)
    if qualifier is None:
        return [TColumn(family=family_bytes)]
    if isinstance(qualifier, str):
        return [TColumn(family=family_bytes, qualifier=to_bytes(qualifier))]
    if isinstance(qualifier, list) or isinstance(qualifier, tuple):
        cols = []
        for cq in qualifier:
            if isinstance(cq, str) or cq is None:
                cols.append(
                    TColumn(family=family_bytes, qualifier=to_bytes(cq)))
            else:
                raise ValueError(
                    "Qualifier should be None, str or a list (tuple) of str")
        return cols
Ejemplo n.º 4
0
 def __init__(
         self,
         row=None,  # type: Union[str]
         family=None,  # type: Union[None, str]
         qualifier=None,  # type: Union[None, str, List[str]]
         value=None,  # type: Union[str, List[str]]
 ):
     super(Put, self).__init__(row, family, qualifier, value)
     check_none(self.row, "Row cannot be none for Put operation.")
     check_none(self.value, "Value cannot be none for Put operation.")
     column_values = []
     columns = _column_format(family, qualifier)
     if isinstance(value, str):
         for col in columns:
             column_values.append(
                 TColumnValue(family=to_bytes(col.family),
                              qualifier=to_bytes(col.qualifier),
                              value=self.value))
     elif isinstance(value, list) or isinstance(value, tuple):
         if len(columns) != len(value):
             raise ValueError(
                 "The number of columns mismatches the number of value list."
             )
         for i, col in enumerate(columns):
             column_values.append(
                 TColumnValue(family=col.family,
                              qualifier=col.qualifier,
                              value=to_bytes(value[i])))
     self.core = TPut(row=self.row, columnValues=column_values)
Ejemplo n.º 5
0
 def add_attributes(self, key, value):
     check_none(key, "None key found.")
     if value is None:
         if key in self.attributes:
             self.attributes.pop(key)
     else:
         self.attributes[to_bytes(key)] = to_bytes(value)
     return self
Ejemplo n.º 6
0
def str_to_tablename(name):
    check_none(name, "")
    type_check(name, str)
    names = name.split(DELIMITER)
    if len(names) == 1:
        return TTableName(ns=to_bytes('default'), qualifier=to_bytes(names[0]))
    elif len(names) == 2:
        return TTableName(ns=to_bytes(names[0]), qualifier=to_bytes(names[1]))
    else:
        raise RuntimeError("Get table name with wrong format.")
Ejemplo n.º 7
0
 def __init__(
         self,
         row,  # type: Union[None, str]
         family,  # type: Union[None, str]
         qualifier,  # type: Union[None, str]
         value,  # type: Union[None, str]
 ):
     self.row = to_bytes(row)
     self.family = to_bytes(family)
     self.qualifier = to_bytes(qualifier)
     self.value = to_bytes(value)
Ejemplo n.º 8
0
 def __init__(
         self,
         start_row=None,  # type: Union[None, str]
         family=None,  # type: Union[None, str]
         qualifier=None,  # type: Union[None, str, List[str]]
         stop_row=None,  # type: Union[None, str]
         num_rows=5000,  # type: int
         max_versions=None,  # type: Union[None, int]
         reversed=False,  # type: Union[bool]
         filter_string=None,  # type:str
         filter_bytes=None,  # type:bytes
 ):
     super(Scan, self).__init__(start_row, family, qualifier, None)
     self.reversed = reversed
     self.stop_row = to_bytes(stop_row)
     self.num_rows = num_rows
     self.core = TScan(startRow=self.row,
                       stopRow=self.stop_row,
                       columns=_column_format(self.family, self.qualifier),
                       maxVersions=max_versions,
                       reversed=self.reversed,
                       filterString=to_bytes(filter_string),
                       filterBytes=filter_bytes)
Ejemplo n.º 9
0
 def __init__(
         self,
         row=None,  # type: Union[str]
         family=None,  # type: Union[None, str]
         qualifier=None,  # type: Union[None, str, List[str]]
         value=None,  # type: Union[None, str]
         max_versions=None,  # type: Union[None, int]
         filter_string=None,  # type: str
         filter_bytes=None,  # type: bytes
 ):
     super(Get, self).__init__(row, family, qualifier, value)
     check_none(self.row, "Row cannot be none for Get operation.")
     self.maxVersions = max_versions
     self.core = TGet(row=self.row,
                      columns=_column_format(family, qualifier),
                      timestamp=None,
                      timeRange=None,
                      maxVersions=self.maxVersions,
                      filterString=to_bytes(filter_string),
                      filterBytes=filter_bytes)
Ejemplo n.º 10
0
 def build(self):
     return TColumnFamilyDescriptor(
         name=to_bytes(self.name),
         attributes=self.attributes,
         configuration=self.configuration,
         blockSize=self.blockSize,
         bloomnFilterType=self.bloomnFilterType,
         compressionType=self.compressionType,
         dfsReplication=self.dfsReplication,
         dataBlockEncoding=self.dataBlockEncoding,
         keepDeletedCells=self.keepDeletedCells,
         maxVersions=self.maxVersions,
         minVersions=self.minVersions,
         scope=self.scope,
         timeToLive=self.timeToLive,
         blockCacheEnabled=self.blockCacheEnabled,
         cacheBloomsOnWrite=self.cacheBloomsOnWrite,
         cacheDataOnWrite=self.cacheDataOnWrite,
         cacheIndexesOnWrite=self.cacheIndexesOnWrite,
         compressTags=self.compressTags,
         evictBlocksOnClose=self.evictBlocksOnClose,
         inMemory=self.inMemory)
Ejemplo n.º 11
0
 def __init__(self, table_name, client):
     self._name = to_bytes(table_name)
     self._client = client
     self.conf = client.conf
     self.executor = Executor(self.conf.retry_times, self.conf.retry_timeout, master=self._client)