Beispiel #1
0
    def test_mutate_rows(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = bigtable_pb2.MutateRowsResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[iter([expected_response])])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = bigtable_v2.BigtableClient()

        # Setup Request
        table_name = client.table_path("[PROJECT]", "[INSTANCE]", "[TABLE]")
        entries = []

        response = client.mutate_rows(table_name, entries)
        resources = list(response)
        assert len(resources) == 1
        assert expected_response == resources[0]

        assert len(channel.requests) == 1
        expected_request = bigtable_pb2.MutateRowsRequest(
            table_name=table_name, entries=entries
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Beispiel #2
0
def _mutate_rows_request(table_name, rows, app_profile_id=None):
    """Creates a request to mutate rows in a table.

    :type table_name: str
    :param table_name: The name of the table to write to.

    :type rows: list
    :param rows: List or other iterable of :class:`.DirectRow` instances.

    :type: app_profile_id: str
    :param app_profile_id: (Optional) The unique name of the AppProfile.

    :rtype: :class:`data_messages_v2_pb2.MutateRowsRequest`
    :returns: The ``MutateRowsRequest`` protobuf corresponding to the inputs.
    :raises: :exc:`~.table.TooManyMutationsError` if the number of mutations is
             greater than 100,000
    """
    request_pb = data_messages_v2_pb2.MutateRowsRequest(
        table_name=table_name, app_profile_id=app_profile_id)
    mutations_count = 0
    for row in rows:
        _check_row_table_name(table_name, row)
        _check_row_type(row)
        mutations = row._get_mutations()
        request_pb.entries.add(row_key=row.row_key, mutations=mutations)
        mutations_count += len(mutations)
    if mutations_count > _MAX_BULK_MUTATIONS:
        raise TooManyMutationsError("Maximum number of mutations is %s" %
                                    (_MAX_BULK_MUTATIONS, ))
    return request_pb
Beispiel #3
0
def _mutate_rows_request(table_name, rows):
    """Creates a request to mutate rows in a table.

    :type table_name: str
    :param table_name: The name of the table to write to.

    :type rows: list
    :param rows: List or other iterable of :class:`.DirectRow` instances.

    :rtype: :class:`data_messages_v2_pb2.MutateRowsRequest`
    :returns: The ``MutateRowsRequest`` protobuf corresponding to the inputs.
    :raises: :exc:`~.table.TooManyMutationsError` if the number of mutations is
             greater than 100,000
    """
    request_pb = data_messages_v2_pb2.MutateRowsRequest(table_name=table_name)
    mutations_count = 0
    for row in rows:
        _check_row_table_name(table_name, row)
        _check_row_type(row)
        entry = request_pb.entries.add()
        entry.row_key = row.row_key
        # NOTE: Since `_check_row_type` has verified `row` is a `DirectRow`,
        #  the mutations have no state.
        for mutation in row._get_mutations(None):
            mutations_count += 1
            entry.mutations.add().CopyFrom(mutation)
    if mutations_count > _MAX_BULK_MUTATIONS:
        raise TooManyMutationsError('Maximum number of mutations is %s' %
                                    (_MAX_BULK_MUTATIONS,))
    return request_pb
Beispiel #4
0
    def test_mutate_rows(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = bigtable_pb2.MutateRowsResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[iter([expected_response])])
        client = bigtable_v2.BigtableClient(channel=channel)

        # Setup Request
        table_name = client.table_path('[PROJECT]', '[INSTANCE]', '[TABLE]')
        entries = []

        response = client.mutate_rows(table_name, entries)
        resources = list(response)
        assert len(resources) == 1
        assert expected_response == resources[0]

        assert len(channel.requests) == 1
        expected_request = bigtable_pb2.MutateRowsRequest(
            table_name=table_name, entries=entries)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def mutate_rows(
        self,
        table_name,
        entries,
        app_profile_id=None,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Mutates multiple rows in a batch. Each individual row is mutated
        atomically as in MutateRow, but the entire batch is not executed
        atomically.

        Example:
            >>> from google.cloud import bigtable_v2
            >>>
            >>> client = bigtable_v2.BigtableClient()
            >>>
            >>> table_name = client.table_path('[PROJECT]', '[INSTANCE]', '[TABLE]')
            >>>
            >>> # TODO: Initialize `entries`:
            >>> entries = []
            >>>
            >>> for element in client.mutate_rows(table_name, entries):
            ...     # process element
            ...     pass

        Args:
            table_name (str): The unique name of the table to which the mutations should be applied.
            entries (list[Union[dict, ~google.cloud.bigtable_v2.types.Entry]]): The row keys and corresponding mutations to be applied in bulk.
                Each entry is applied as an atomic mutation, but the entries may be
                applied in arbitrary order (even between entries for the same row).
                At least one entry must be specified, and in total the entries can
                contain at most 100000 mutations.

                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.bigtable_v2.types.Entry`
            app_profile_id (str): This value specifies routing for replication. If not specified, the
                "default" application profile will be used.
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will
                be retried using a default configuration.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.
            metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
                that is provided to the method.

        Returns:
            Iterable[~google.cloud.bigtable_v2.types.MutateRowsResponse].

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        # Wrap the transport method to add retry and timeout logic.
        if "mutate_rows" not in self._inner_api_calls:
            self._inner_api_calls[
                "mutate_rows"] = google.api_core.gapic_v1.method.wrap_method(
                    self.transport.mutate_rows,
                    default_retry=self._method_configs["MutateRows"].retry,
                    default_timeout=self._method_configs["MutateRows"].timeout,
                    client_info=self._client_info,
                )

        request = bigtable_pb2.MutateRowsRequest(table_name=table_name,
                                                 entries=entries,
                                                 app_profile_id=app_profile_id)
        if metadata is None:
            metadata = []
        metadata = list(metadata)
        try:
            routing_header = [("table_name", table_name)]
        except AttributeError:
            pass
        else:
            routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
                routing_header)
            metadata.append(routing_metadata)

        return self._inner_api_calls["mutate_rows"](request,
                                                    retry=retry,
                                                    timeout=timeout,
                                                    metadata=metadata)
Beispiel #6
0
    def mutate_rows(self,
                    table_name,
                    entries,
                    app_profile_id=None,
                    retry=google.api_core.gapic_v1.method.DEFAULT,
                    timeout=google.api_core.gapic_v1.method.DEFAULT):
        """
        Mutates multiple rows in a batch. Each individual row is mutated
        atomically as in MutateRow, but the entire batch is not executed
        atomically.

        Example:
            >>> from google.cloud import bigtable_v2
            >>>
            >>> client = bigtable_v2.BigtableClient()
            >>>
            >>> table_name = client.table_path('[PROJECT]', '[INSTANCE]', '[TABLE]')
            >>> entries = []
            >>>
            >>> for element in client.mutate_rows(table_name, entries):
            ...     # process element
            ...     pass

        Args:
            table_name (str): The unique name of the table to which the mutations should be applied.
            entries (list[Union[dict, ~google.cloud.bigtable_v2.types.Entry]]): The row keys and corresponding mutations to be applied in bulk.
                Each entry is applied as an atomic mutation, but the entries may be
                applied in arbitrary order (even between entries for the same row).
                At least one entry must be specified, and in total the entries can
                contain at most 100000 mutations.
                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.bigtable_v2.types.Entry`
            app_profile_id (str): This is a private alpha release of Cloud Bigtable replication. This feature
                is not currently available to most Cloud Bigtable customers. This feature
                might be changed in backward-incompatible ways and is not recommended for
                production use. It is not subject to any SLA or deprecation policy.

                This value specifies routing for replication. If not specified, the
                \"default\" application profile will be used.
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.

        Returns:
            Iterable[~google.cloud.bigtable_v2.types.MutateRowsResponse].

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        request = bigtable_pb2.MutateRowsRequest(
            table_name=table_name,
            entries=entries,
            app_profile_id=app_profile_id,
        )
        return self._mutate_rows(request, retry=retry, timeout=timeout)
def _mutate_rows_request_pb(*args, **kw):
    from google.cloud.bigtable_v2.proto import (
        bigtable_pb2 as data_messages_v2_pb2)

    return data_messages_v2_pb2.MutateRowsRequest(*args, **kw)