Exemplo n.º 1
0
    def _update_schema(self, columns, method=None, fail_silently=True):
        """
        Updates current table schema. If not successful, returns appropriate HTTP error status code. If
        successful, returns HTTP 200 status or boolean False if table does not exist.

        :param list columns: List of columns (plain strings).
        :param str method: HTTP method (GET, POST, PUT, DELETE).
        :return int: HTTP response status code.
        """
        if self.check_if_exists_on_schema_operations:
            if not self.exists(fail_silently=fail_silently):
                return False

        if method is None:
            method = POST

        url, data = self._get_data_for_table_create_or_update(columns)

        response = HttpRequest(connection=self.connection,
                               url=url,
                               data=data,
                               method=method,
                               fail_silently=fail_silently).get_response()

        return response.status_code
Exemplo n.º 2
0
    def _put(self,
             row,
             columns,
             timestamp=None,
             encode_content=True,
             fail_silently=True):
        """
        Cell store (single or multiple). If not successful, returns appropriate HTTP error status code. If
        successful, returns HTTP 200 status.

        ..note: Inserting of multiple rows could be achieved with batch.

        :param str row:
        :param dict columns:
        :param bool encode_content: Better be True, because otherwise values may not be submitted correctly.
        :param bool fail_silently:
        :return int:
        """
        if not self.exists(fail_silently=fail_silently):
            return None

        url = self._build_put_url(row, columns)

        data = self._build_table_data(row, columns, timestamp=timestamp, encode_content=encode_content, \
                                      with_row_declaration=True)

        response = HttpRequest(connection=self.connection,
                               url=url,
                               data=data,
                               decode_content=False,
                               method=PUT,
                               fail_silently=fail_silently).get_response()

        return response.status_code
Exemplo n.º 3
0
    def __init__(self, table, url, batch_size=None, start_row=None, end_row=None, start_time=None, end_time=None, \
                 data={}, extra_headers={}, method=None):
        self.table = table
        self.url = url
        self.batch_size = batch_size
        self.start_row = start_row
        self.end_row = end_row
        self.start_time = start_time
        self.end_time = end_time
        self.id = url.split('/')[-1]
        url = '{table_name}/scanner/{scanner_id}'.format(
            table_name=self.table.name, scanner_id=self.id)

        def encode_data(data):
            encoded = {}
            for key, value in data.items():
                encoded.update(
                    {base64.b64encode(key): base64.b64encode(value)})
            return encoded

        self.response = HttpRequest(
            connection=self.table.connection,
            url=url,
            data=encode_data(data),
            #extra_headers = extra_headers,
            #method = method
        ).get_response()
Exemplo n.º 4
0
    def _post(self,
              row,
              columns,
              timestamp=None,
              encode_content=True,
              fail_silently=True):
        """
        Update (POST) operation.

        :param str:
        :param dict columns:
        :param timestamp: Not yet used.
        :param bool encode_content: Better be True, because otherwise values may not be submitted correctly.
        :param bool fail_silently:
        :return int:
        """
        if not self.exists(fail_silently=fail_silently):
            return None

        url = self._build_put_url(row, columns)

        data = self._build_table_data(row, columns, timestamp=timestamp, encode_content=encode_content, \
                                      with_row_declaration=True)

        response = HttpRequest(connection=self.connection,
                               url=url,
                               data=data,
                               decode_content=False,
                               method=POST,
                               fail_silently=fail_silently).get_response()

        return response.status_code
Exemplo n.º 5
0
    def _delete(self,
                row,
                column=None,
                qualifier=None,
                timestamp=None,
                fail_silently=True):
        """
        Deletes the table row or selected columns row given. If not successful, returns appropriate HTTP error
        status code. If successful, returns HTTP 200 status.

        :param str row: Row id to delete.
        :param str column: Column to delete. If given, only that specific column is deleted. If left blank or
            set to None, entire row is deleted.
        :param str qualifier: Column qualifier. If given, only that specific column qualifier is deleted. If left
            blank or set to None, entire column family is deleted.
        :param bool fail_silently:
        :return int: HTTP status code.
        """
        url = self._build_delete_url(row=row,
                                     column=column,
                                     qualifier=qualifier)

        response = HttpRequest(connection=self.connection,
                               url=url,
                               method=DELETE,
                               fail_silently=fail_silently).get_response()
        return response.status_code
Exemplo n.º 6
0
    def _scanner(self, batch_size=None, start_row=None, end_row=None, start_time=None, end_time=None, \
                 filter_string=None, data='', fail_silently=True):
        """
        Creates a scanner instance.

        :param int batch_size:
        :param str start_row:
        :param str end_row:
        :param start_time:
        :param end_time:
        :param str filter_string:
        :return starbase.client.Scanner: Creates and returns a class::`starbase.client.Scanner` instance.
        """
        url = '{0}/scanner'.format(self.name)

        if filter_string is not None:
            data = {"filter": filter_string}

        response = HttpRequest(connection=self.connection,
                               url=url,
                               data=data,
                               method=PUT,
                               fail_silently=fail_silently).get_response()
        scanner_url = response.raw.headers.get('location')

        return Scanner(table=self, url=scanner_url)
Exemplo n.º 7
0
    def create(self, *columns, **kwargs):
        """
        Creates a table schema. If not successful, returns appropriate HTTP error status code. If successful,
        returns HTTP 201 status.

        :param list *columns: List of columns (plain strins).
        :return int: HTTP response status code (201 on success). Returns boolean False on failure.

        :example:
        >>> from starbase import Connection
        >>> connection = Connection()
        >>> table = connection.table('table1')
        >>> table.create('column1', 'column2')
        """
        # If table exists, return False
        fail_silently = kwargs.get('fail_silently', True)
        if self.exists():
            if fail_silently:
                return False
            else:
                raise AlreadyExists('Table already exists')

        url, data = self._get_data_for_table_create_or_update(columns)

        response = HttpRequest(connection=self.connection,
                               url=url,
                               data=data,
                               method=PUT,
                               fail_silently=fail_silently).get_response()
        return response.status_code
Exemplo n.º 8
0
    def version(self):
        """
        Software version. Returns the software version.

        :return dict: Dictionary with info on software versions (OS, Server, JVM, etc).
        """
        response = HttpRequest(connection=self, url='version').get_response()
        return response.content
Exemplo n.º 9
0
    def cluster_status(self):
        """
        Storage cluster satus. Returns detailed status on the HBase cluster backing the Stargate instance.

        :return dict: Dictionary with information on dead nodes, live nodes, average load, regions, etc.
        """
        response = HttpRequest(connection=self, url='status/cluster').get_response()
        return response.content
Exemplo n.º 10
0
    def _get(self, row, columns=None, timestamp=None, decode_content=True, number_of_versions=None, raw=False, \
            perfect_dict=None):
        """
        Retrieves one or more cells from a full row, or one or more specified columns in the row, with optional
        filtering via timestamp, and an optional restriction on the maximum number of versions to return.

        The `raw` argument is dominant. If given, the raw response it returned. Otherwise, a nice response is
        returned that does make sense. If `perfect_dict` set to True, then we return a nice dict, instead of a
        horrible one.

        In result JSON, the value of the `$` field (key) is the cell data.

        :param str row:
        :param list|set|tuple|dict columns:
        :param timestamp: Not yet used.
        :param bool decode_content: If set to True, content is decoded using ``stargate.json_decoder.json_decode``.
        :param int number_of_versions: If provided, multiple versions of the given record are returned.
        :param bool perfect_dict:
        :param bool raw:
        :return dict:
        """
        if not self.exists():
            return None

        if perfect_dict is None:
            perfect_dict = self.connection.perfect_dict

        # If just one column given as string, make a list of it.
        if isinstance(columns, string_types):
            columns = [columns]

        # Base URL
        url = "{table_name}/{row}/".format(table_name=self.name, row=row)

        url += self._build_url_parts(columns)

        # If should be versioned, adding additional URL parts.
        if number_of_versions is not None:
            assert isinstance(number_of_versions, int)
            url += '?v={0}'.format(str(number_of_versions))
        response = HttpRequest(connection=self.connection,
                               url=url,
                               decode_content=decode_content).get_response()

        response_content = response.content

        if raw:
            return response_content

        if response_content:
            try:
                res = Table._extract_usable_data(response_content,
                                                 perfect_dict=perfect_dict,
                                                 with_row_id=False)
                if isinstance(res, (list, tuple)) and 1 == len(res):
                    return res[0]
            except:
                pass
Exemplo n.º 11
0
    def cluster_version(self):
        """
        Storage cluster version. Returns version information regarding the HBase cluster backing the Stargate
        instance.

        :return str: HBase version.
        """
        response = HttpRequest(connection=self, url='version/cluster').get_response()
        return response.content
Exemplo n.º 12
0
    def regions(self):
        """
        Table metadata. Retrieves table region metadata.

        :return dict:
        """
        url = "{table_name}/regions".format(table_name=self.name)
        response = HttpRequest(connection=self.connection,
                               url=url).get_response()
        return response.content
Exemplo n.º 13
0
 def delete(self):
     """
     Delete scanner.
     """
     url = '{table_name}/scanner/{scanner_id}'.format(
         table_name=self.table.name, scanner_id=self.id)
     response = HttpRequest(connection=self.table.connection,
                            url=url,
                            method=DELETE).get_response()
     return response.status_code
Exemplo n.º 14
0
    def version(self, fail_silently=True):
        """
        Software version. Returns the software version.

        :param bool fail_silently:
        :return dict: Dictionary with info on software versions (OS, Server, JVM, etc).
        """
        response = HttpRequest(connection=self,
                               url='version',
                               fail_silently=fail_silently).get_response()
        return response.content
Exemplo n.º 15
0
    def cluster_version(self, fail_silently=True):
        """
        Storage cluster version. Returns version information regarding the HBase cluster backing the Stargate
        instance.

        :param bool fail_silently:
        :return str: HBase version.
        """
        response = HttpRequest(connection=self,
                               url='version/cluster',
                               fail_silently=fail_silently).get_response()
        return response.content
Exemplo n.º 16
0
    def tables(self, raw=False):
        """
        Table list. Retrieves the list of available tables.

        :param bool raw: If set to True raw result (JSON) is returned.
        :return list: Just a list of plain strings of table names, no Table instances.
        """
        response = HttpRequest(connection=self).get_response()
        if not raw:
            try:
                return [table['name'] for table in response.content['table']]
            except:
                return []
        return response.content
Exemplo n.º 17
0
    def schema(self):
        """
        Table schema. Retrieves table schema.

        :return dict: Dictionary with schema info (detailed information on column families).

        :example:
        >>> from starbase import Connection
        >>> connection = Connection()
        >>> table = connection.table('table1')
        >>> table.schema()
        """
        url = "{table_name}/schema".format(table_name=self.name)
        response = HttpRequest(connection=self.connection,
                               url=url).get_response()
        return response.content
Exemplo n.º 18
0
    def commit(self, finalize=False):
        """
        Sends all queued items to Stargate.

        :param bool finalize: If set to True, the batch is finalized, settings are cleared up and response is
            returned.

        :return dict: If `finalize` set to True, returns the returned value of method
            meth::`starbase.client.batch.Batch.finalize`.
        """
        response = HttpRequest(
            connection = self.table.connection,
            url = self._url,
            data = {"Row" : self._stack},
            decode_content = False,
            method = self._method,
            ).get_response()
        self._response.append(response.status_code)
        self._stack = []

        if finalize:
            return self.finalize()
Exemplo n.º 19
0
    def drop(self):
        """
        Drops current table. If not successful, returns appropriate HTTP error status code. If successful,
        returns HTTP 200 status.

        :return int: HTTP response status code (200 on success).

        :example:
        In the example below we check if table named `table1` exists and if so - drop it.

        >>> from starbase import Connection
        >>> connection = Connection()
        >>> table = connection.table('table1')
        >>> if table.exists():
        >>>     table.drop()
        """
        response = HttpRequest(connection=self.connection,
                               url='{0}/schema'.format(self.name),
                               method=DELETE).get_response()

        # If response.status_code == 200 it means table was successfully dropped/deleted.
        return response.status_code