google-cloud-bigtable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +10 -0
- data/LICENSE +201 -0
- data/README.md +65 -0
- data/lib/google/bigtable/admin/v2/bigtable_instance_admin_pb.rb +139 -0
- data/lib/google/bigtable/admin/v2/bigtable_instance_admin_services_pb.rb +85 -0
- data/lib/google/bigtable/admin/v2/bigtable_table_admin_pb.rb +137 -0
- data/lib/google/bigtable/admin/v2/bigtable_table_admin_services_pb.rb +117 -0
- data/lib/google/bigtable/admin/v2/common_pb.rb +24 -0
- data/lib/google/bigtable/admin/v2/instance_pb.rb +72 -0
- data/lib/google/bigtable/admin/v2/table_pb.rb +88 -0
- data/lib/google/bigtable/v2/bigtable_pb.rb +109 -0
- data/lib/google/bigtable/v2/bigtable_services_pb.rb +67 -0
- data/lib/google/bigtable/v2/data_pb.rb +155 -0
- data/lib/google/cloud/bigtable/admin/credentials.rb +26 -0
- data/lib/google/cloud/bigtable/admin/v2/bigtable_instance_admin_client.rb +1417 -0
- data/lib/google/cloud/bigtable/admin/v2/bigtable_instance_admin_client_config.json +123 -0
- data/lib/google/cloud/bigtable/admin/v2/bigtable_table_admin_client.rb +1079 -0
- data/lib/google/cloud/bigtable/admin/v2/bigtable_table_admin_client_config.json +109 -0
- data/lib/google/cloud/bigtable/admin/v2/credentials.rb +50 -0
- data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/bigtable_instance_admin.rb +279 -0
- data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/bigtable_table_admin.rb +353 -0
- data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/instance.rb +194 -0
- data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/table.rb +209 -0
- data/lib/google/cloud/bigtable/admin/v2/doc/google/iam/v1/iam_policy.rb +62 -0
- data/lib/google/cloud/bigtable/admin/v2/doc/google/iam/v1/policy.rb +127 -0
- data/lib/google/cloud/bigtable/admin/v2/doc/google/longrunning/operations.rb +92 -0
- data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/any.rb +124 -0
- data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/duration.rb +90 -0
- data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/empty.rb +28 -0
- data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/field_mask.rb +223 -0
- data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/timestamp.rb +106 -0
- data/lib/google/cloud/bigtable/admin/v2/doc/google/rpc/status.rb +83 -0
- data/lib/google/cloud/bigtable/admin/v2.rb +200 -0
- data/lib/google/cloud/bigtable/admin.rb +196 -0
- data/lib/google/cloud/bigtable/app_profile/job.rb +102 -0
- data/lib/google/cloud/bigtable/app_profile/list.rb +159 -0
- data/lib/google/cloud/bigtable/app_profile.rb +373 -0
- data/lib/google/cloud/bigtable/chunk_processor.rb +253 -0
- data/lib/google/cloud/bigtable/cluster/job.rb +92 -0
- data/lib/google/cloud/bigtable/cluster/list.rb +169 -0
- data/lib/google/cloud/bigtable/cluster.rb +264 -0
- data/lib/google/cloud/bigtable/column_family.rb +280 -0
- data/lib/google/cloud/bigtable/column_range.rb +186 -0
- data/lib/google/cloud/bigtable/convert.rb +75 -0
- data/lib/google/cloud/bigtable/credentials.rb +24 -0
- data/lib/google/cloud/bigtable/errors.rb +35 -0
- data/lib/google/cloud/bigtable/gc_rule.rb +215 -0
- data/lib/google/cloud/bigtable/instance/cluster_map.rb +70 -0
- data/lib/google/cloud/bigtable/instance/job.rb +97 -0
- data/lib/google/cloud/bigtable/instance/list.rb +159 -0
- data/lib/google/cloud/bigtable/instance.rb +921 -0
- data/lib/google/cloud/bigtable/longrunning_job.rb +105 -0
- data/lib/google/cloud/bigtable/mutation_entry.rb +244 -0
- data/lib/google/cloud/bigtable/mutation_operations.rb +338 -0
- data/lib/google/cloud/bigtable/policy.rb +163 -0
- data/lib/google/cloud/bigtable/project.rb +580 -0
- data/lib/google/cloud/bigtable/read_modify_write_rule.rb +129 -0
- data/lib/google/cloud/bigtable/read_operations.rb +345 -0
- data/lib/google/cloud/bigtable/row.rb +125 -0
- data/lib/google/cloud/bigtable/row_filter/chain_filter.rb +539 -0
- data/lib/google/cloud/bigtable/row_filter/condition_filter.rb +108 -0
- data/lib/google/cloud/bigtable/row_filter/interleave_filter.rb +570 -0
- data/lib/google/cloud/bigtable/row_filter/simple_filter.rb +273 -0
- data/lib/google/cloud/bigtable/row_filter.rb +593 -0
- data/lib/google/cloud/bigtable/row_range.rb +174 -0
- data/lib/google/cloud/bigtable/rows_mutator.rb +120 -0
- data/lib/google/cloud/bigtable/rows_reader.rb +196 -0
- data/lib/google/cloud/bigtable/sample_row_key.rb +82 -0
- data/lib/google/cloud/bigtable/service.rb +817 -0
- data/lib/google/cloud/bigtable/table/cluster_state.rb +93 -0
- data/lib/google/cloud/bigtable/table/column_family_map.rb +68 -0
- data/lib/google/cloud/bigtable/table/list.rb +147 -0
- data/lib/google/cloud/bigtable/table.rb +676 -0
- data/lib/google/cloud/bigtable/v2/bigtable_client.rb +579 -0
- data/lib/google/cloud/bigtable/v2/bigtable_client_config.json +65 -0
- data/lib/google/cloud/bigtable/v2/credentials.rb +45 -0
- data/lib/google/cloud/bigtable/v2/doc/google/bigtable/v2/bigtable.rb +286 -0
- data/lib/google/cloud/bigtable/v2/doc/google/bigtable/v2/data.rb +492 -0
- data/lib/google/cloud/bigtable/v2/doc/google/protobuf/any.rb +124 -0
- data/lib/google/cloud/bigtable/v2/doc/google/protobuf/wrappers.rb +89 -0
- data/lib/google/cloud/bigtable/v2/doc/google/rpc/status.rb +83 -0
- data/lib/google/cloud/bigtable/v2.rb +132 -0
- data/lib/google/cloud/bigtable/value_range.rb +175 -0
- data/lib/google/cloud/bigtable/version.rb +22 -0
- data/lib/google/cloud/bigtable.rb +223 -0
- data/lib/google-cloud-bigtable.rb +167 -0
- metadata +283 -0
@@ -0,0 +1,105 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2018 Google LLC
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Bigtable
|
21
|
+
# # LongrunningJob
|
22
|
+
#
|
23
|
+
# A resource representing the long-running, asynchronous processing operation.
|
24
|
+
# The job can be refreshed to retrieve the result object once the operation has been completed.
|
25
|
+
#
|
26
|
+
# @see https://cloud.google.com/bigtable/docs/reference/admin/rpc/google.longrunning#google.longrunning.Operation
|
27
|
+
# Long-running Operation
|
28
|
+
#
|
29
|
+
class LongrunningJob
|
30
|
+
# @private
|
31
|
+
# The Google::Gax::Operation gRPC object.
|
32
|
+
attr_accessor :grpc
|
33
|
+
|
34
|
+
# @private
|
35
|
+
# The gRPC Service object.
|
36
|
+
attr_accessor :service
|
37
|
+
|
38
|
+
# Get result object of the operation.
|
39
|
+
#
|
40
|
+
# @return [Object, nil]
|
41
|
+
# `nil` if the operation is not complete.
|
42
|
+
#
|
43
|
+
def results
|
44
|
+
return nil unless done?
|
45
|
+
return nil unless @grpc.grpc_op.result == :response
|
46
|
+
@grpc.results
|
47
|
+
end
|
48
|
+
|
49
|
+
# Checks if the processing of the instance operation is complete.
|
50
|
+
#
|
51
|
+
# @return [boolean] `true` when complete, `false` otherwise.
|
52
|
+
#
|
53
|
+
def done?
|
54
|
+
@grpc.done?
|
55
|
+
end
|
56
|
+
|
57
|
+
# Checks if the processing of the instance operation has errored.
|
58
|
+
#
|
59
|
+
# @return [boolean] `true` when errored, `false` otherwise.
|
60
|
+
#
|
61
|
+
def error?
|
62
|
+
@grpc.error?
|
63
|
+
end
|
64
|
+
|
65
|
+
##
|
66
|
+
# The status if the operation associated with this job produced an
|
67
|
+
# error.
|
68
|
+
#
|
69
|
+
# @return [Object, Google::Rpc::Status, nil] A status object with
|
70
|
+
# the status code and message, or `nil` if no error occurred.
|
71
|
+
#
|
72
|
+
def error
|
73
|
+
return nil unless error?
|
74
|
+
@grpc.error
|
75
|
+
end
|
76
|
+
|
77
|
+
# Reloads the job with current data from the long-running,
|
78
|
+
# asynchronous processing of an operation.
|
79
|
+
#
|
80
|
+
# @return [Google::Cloud::Bigtable::Instance::Job] The same job instance.
|
81
|
+
#
|
82
|
+
def reload!
|
83
|
+
@grpc.reload!
|
84
|
+
self
|
85
|
+
end
|
86
|
+
|
87
|
+
# Reloads the job until the operation is complete. The delay between
|
88
|
+
# reloads will incrementally increase.
|
89
|
+
#
|
90
|
+
def wait_until_done!
|
91
|
+
@grpc.wait_until_done!
|
92
|
+
end
|
93
|
+
|
94
|
+
# @private
|
95
|
+
# New BasicJob from a Google::Gax::Operation object.
|
96
|
+
def self.from_grpc grpc, service
|
97
|
+
new.tap do |job|
|
98
|
+
job.grpc = grpc
|
99
|
+
job.service = service
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,244 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2018 Google LLC
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Bigtable
|
21
|
+
# # MutationEntry
|
22
|
+
#
|
23
|
+
# MutationEntry is a chainable structure, which holds data for diffrent
|
24
|
+
# type of mutations.
|
25
|
+
# MutationEntry is used in following data operations
|
26
|
+
#
|
27
|
+
# * Mutate row. See {Google::Cloud::Bigtable::Table#mutate_row}
|
28
|
+
# * Mutate rows. See {Google::Cloud::Bigtable::Table#mutate_rows}
|
29
|
+
# * Check and mutate row using a predicate.
|
30
|
+
# see {Google::Cloud::Bigtable::Table#check_and_mutate_row}
|
31
|
+
#
|
32
|
+
# @example
|
33
|
+
# entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
|
34
|
+
# entry.set_cell(
|
35
|
+
# "cf1", "fiel01", "XYZ", timestamp: Time.now.to_i * 1000
|
36
|
+
# ).delete_cells(
|
37
|
+
# "cf2",
|
38
|
+
# "field02",
|
39
|
+
# timestamp_from: (Time.now.to_i - 1800) * 1000,
|
40
|
+
# timestamp_to: (Time.now.to_i * 1000)
|
41
|
+
# ).delete_from_family("cf3").delete_from_row
|
42
|
+
#
|
43
|
+
# @example Using table
|
44
|
+
# require "google/cloud/bigtable"
|
45
|
+
#
|
46
|
+
# bigtable = Google::Cloud::Bigtable.new
|
47
|
+
# table = bigtable.table("my-instance", "my-table")
|
48
|
+
#
|
49
|
+
# entry = table.new_mutation_entry("user-1")
|
50
|
+
# entry.set_cell(
|
51
|
+
# "cf1", "fiel01", "XYZ", timestamp: Time.now.to_i * 1000
|
52
|
+
# )
|
53
|
+
#
|
54
|
+
class MutationEntry
|
55
|
+
attr_accessor :row_key
|
56
|
+
|
57
|
+
# mutations gRPC list
|
58
|
+
# @return [Array<Google::Bigtable::V2::Mutation>]
|
59
|
+
attr_accessor :mutations
|
60
|
+
|
61
|
+
# Creates a mutation entry instance.
|
62
|
+
#
|
63
|
+
# @param row_key [String]
|
64
|
+
def initialize row_key = nil
|
65
|
+
@row_key = row_key
|
66
|
+
@mutations = []
|
67
|
+
@retryable = true
|
68
|
+
end
|
69
|
+
|
70
|
+
# Add SetCell mutation to list of mutations.
|
71
|
+
#
|
72
|
+
# A Mutation which sets the value of the specified cell.
|
73
|
+
#
|
74
|
+
# @param family [String] Table column family name.
|
75
|
+
# The name of the family into which new data should be written.
|
76
|
+
# Must match `[-_.a-zA-Z0-9]+`
|
77
|
+
# @param qualifier [String] Column qualifier name.
|
78
|
+
# The qualifier of the column into which new data should be written.
|
79
|
+
# Can be any byte string, including the empty string.
|
80
|
+
# @param value [String, Integer] Cell value data.
|
81
|
+
# The value to be written into the specified cell.
|
82
|
+
# @param timestamp [Time, Integer] Timestamp value.
|
83
|
+
# The timestamp of the cell into which new data should be written.
|
84
|
+
# Use -1 for current Bigtable server time.
|
85
|
+
# Otherwise, the client should set this value itself, noting that the
|
86
|
+
# default value is a timestamp of zero if the field is left unspecified.
|
87
|
+
# Values must match the granularity of the table (e.g. micros, millis).
|
88
|
+
# @return [MutationEntry] `self` object of entry for chaining.
|
89
|
+
#
|
90
|
+
# @example
|
91
|
+
# entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
|
92
|
+
# entry.set_cell("cf1", "field01", "XYZ")
|
93
|
+
#
|
94
|
+
# @example With timestamp
|
95
|
+
# entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
|
96
|
+
# entry.set_cell(
|
97
|
+
# "cf-1",
|
98
|
+
# "field-1",
|
99
|
+
# "XYZ"
|
100
|
+
# timestamp: Time.now.to_i * 1000 # Time stamp in millis seconds.
|
101
|
+
# )
|
102
|
+
#
|
103
|
+
def set_cell family, qualifier, value, timestamp: nil
|
104
|
+
# If value is integer then covert it to sign 64 bit int big-endian.
|
105
|
+
value = [value].pack("q>") if value.is_a?(Integer)
|
106
|
+
options = {
|
107
|
+
family_name: family,
|
108
|
+
column_qualifier: qualifier,
|
109
|
+
value: value
|
110
|
+
}
|
111
|
+
|
112
|
+
if timestamp
|
113
|
+
options[:timestamp_micros] = timestamp
|
114
|
+
@retryable = timestamp != -1
|
115
|
+
end
|
116
|
+
@mutations << Google::Bigtable::V2::Mutation.new(set_cell: options)
|
117
|
+
self
|
118
|
+
end
|
119
|
+
|
120
|
+
# Add DeleteFromColumn entry to list of mutations.
|
121
|
+
#
|
122
|
+
# A Mutation which deletes cells from the specified column, optionally
|
123
|
+
# restricting the deletions to a given timestamp range.
|
124
|
+
#
|
125
|
+
# @param family [String] Table column family name.
|
126
|
+
# The name of the family from which cells should be deleted.
|
127
|
+
# Must match `[-_.a-zA-Z0-9]+`
|
128
|
+
# @param qualifier [String] Column qualifier name.
|
129
|
+
# The qualifier of the column from which cells should be deleted.
|
130
|
+
# Can be any byte string, including the empty string.
|
131
|
+
# @param timestamp_from [Integer] Timestamp lower bound. Optional.
|
132
|
+
# The range of timestamps within which cells should be deleted.
|
133
|
+
# @param timestamp_to [Integer] Timestamp upper bound. Optional.
|
134
|
+
# The range of timestamps within which cells should be deleted.
|
135
|
+
# @return [MutationEntry] `self` object of entry for chaining.
|
136
|
+
#
|
137
|
+
# @example Without timestamp range
|
138
|
+
# entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
|
139
|
+
# entry.delete_cells("cf-1", "field-1")
|
140
|
+
#
|
141
|
+
# @example With timestamp range
|
142
|
+
# entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
|
143
|
+
# entry.delete_cells(
|
144
|
+
# "cf1",
|
145
|
+
# "field-1",
|
146
|
+
# timestamp_from: (Time.now.to_i - 1800) * 1000,
|
147
|
+
# timestamp_to: (Time.now.to_i * 1000)
|
148
|
+
# )
|
149
|
+
# @example With lower bound timestamp range
|
150
|
+
# entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
|
151
|
+
# entry.delete_cells(
|
152
|
+
# "cf1",
|
153
|
+
# "field-1",
|
154
|
+
# timestamp_from: (Time.now.to_i - 1800) * 1000
|
155
|
+
# )
|
156
|
+
#
|
157
|
+
def delete_cells \
|
158
|
+
family,
|
159
|
+
qualifier,
|
160
|
+
timestamp_from: nil,
|
161
|
+
timestamp_to: nil
|
162
|
+
grpc = Google::Bigtable::V2::Mutation::DeleteFromColumn.new(
|
163
|
+
family_name: family,
|
164
|
+
column_qualifier: qualifier
|
165
|
+
)
|
166
|
+
if timestamp_from || timestamp_to
|
167
|
+
time_range = Google::Bigtable::V2::TimestampRange.new
|
168
|
+
time_range.start_timestamp_micros = timestamp_from if timestamp_from
|
169
|
+
time_range.end_timestamp_micros = timestamp_to if timestamp_to
|
170
|
+
grpc.time_range = time_range
|
171
|
+
end
|
172
|
+
@mutations << Google::Bigtable::V2::Mutation.new(
|
173
|
+
delete_from_column: grpc
|
174
|
+
)
|
175
|
+
self
|
176
|
+
end
|
177
|
+
|
178
|
+
# Add DeleteFromFamily to list of mutations.
|
179
|
+
#
|
180
|
+
# A Mutation which deletes all cells from the specified column family.
|
181
|
+
#
|
182
|
+
# @param family [String] Table column family name.
|
183
|
+
# The name of the family from which cells should be deleted.
|
184
|
+
# Must match `[-_.a-zA-Z0-9]+`
|
185
|
+
# @return [MutationEntry] `self` object of entry for chaining.
|
186
|
+
#
|
187
|
+
# @example
|
188
|
+
# entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
|
189
|
+
# entry.delete_from_family("cf-1")
|
190
|
+
#
|
191
|
+
def delete_from_family family
|
192
|
+
@mutations << Google::Bigtable::V2::Mutation.new(
|
193
|
+
delete_from_family: { family_name: family }
|
194
|
+
)
|
195
|
+
self
|
196
|
+
end
|
197
|
+
|
198
|
+
# Add DeleteFromRow entry to list of mutations
|
199
|
+
#
|
200
|
+
# A Mutation which deletes all cells from the containing row.
|
201
|
+
#
|
202
|
+
# @return [MutationEntry] `self` object of entry for chaining.
|
203
|
+
#
|
204
|
+
# @example
|
205
|
+
# entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
|
206
|
+
# entry.delete_from_row
|
207
|
+
#
|
208
|
+
def delete_from_row
|
209
|
+
@mutations << Google::Bigtable::V2::Mutation.new(delete_from_row: {})
|
210
|
+
self
|
211
|
+
end
|
212
|
+
|
213
|
+
# Mutation entry is retryable or not based on set_cell value.
|
214
|
+
#
|
215
|
+
# @return [Boolean]
|
216
|
+
#
|
217
|
+
def retryable?
|
218
|
+
@retryable
|
219
|
+
end
|
220
|
+
|
221
|
+
# No of mutations
|
222
|
+
#
|
223
|
+
# @return [Integer]
|
224
|
+
|
225
|
+
def length
|
226
|
+
@mutations.length
|
227
|
+
end
|
228
|
+
|
229
|
+
# @private
|
230
|
+
#
|
231
|
+
# Convert mutation entry to gRPC protobuf object.
|
232
|
+
#
|
233
|
+
# @return [Google::Bigtable::V2::MutateRowsRequest::Entry]
|
234
|
+
#
|
235
|
+
def to_grpc
|
236
|
+
Google::Bigtable::V2::MutateRowsRequest::Entry.new(
|
237
|
+
row_key: @row_key,
|
238
|
+
mutations: @mutations
|
239
|
+
)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
@@ -0,0 +1,338 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2018 Google LLC
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# https:#www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
|
18
|
+
require "google/cloud/bigtable/mutation_entry"
|
19
|
+
require "google/cloud/bigtable/row"
|
20
|
+
require "google/cloud/bigtable/rows_mutator"
|
21
|
+
require "google/cloud/bigtable/read_modify_write_rule"
|
22
|
+
|
23
|
+
module Google
|
24
|
+
module Cloud
|
25
|
+
module Bigtable
|
26
|
+
# # MutationOperations
|
27
|
+
#
|
28
|
+
# Collection of mutations apis.
|
29
|
+
#
|
30
|
+
# * Mutate single row
|
31
|
+
# * Mutate multiple rows
|
32
|
+
# * Read modify and write row atomically on the server
|
33
|
+
# * Check and mutate row
|
34
|
+
#
|
35
|
+
module MutationOperations
|
36
|
+
# Mutate row.
|
37
|
+
#
|
38
|
+
# Mutates a row atomically. Cells already present in the row are left
|
39
|
+
# unchanged unless explicitly changed by +mutation+.
|
40
|
+
# Changes to be atomically applied to the specified row. Entries are applied
|
41
|
+
# in order, meaning that earlier mutations can be masked by later ones.
|
42
|
+
# Must contain at least one mutation entry and at most 100000.
|
43
|
+
#
|
44
|
+
# @param entry [Google::Cloud::Bigtable::MutationEntry]
|
45
|
+
# Mutation entry with row key and list of mutations.
|
46
|
+
# @return [Boolean]
|
47
|
+
# @example Single mutation on row.
|
48
|
+
# require "google/cloud"
|
49
|
+
#
|
50
|
+
# bigtable = Google::Cloud::Bigtable.new
|
51
|
+
#
|
52
|
+
# table = bigtable.table("my-instance", "my-table")
|
53
|
+
#
|
54
|
+
# entry = table.new_mutation_entry.new("user-1")
|
55
|
+
# entry.set_cell("cf1", "field1", "XYZ")
|
56
|
+
# table.mutate_row(entry)
|
57
|
+
#
|
58
|
+
# @example Multiple mutations on row.
|
59
|
+
# require "google/cloud"
|
60
|
+
#
|
61
|
+
# bigtable = Google::Cloud::Bigtable.new
|
62
|
+
#
|
63
|
+
# table = bigtable.table("my-instance", "my-table")
|
64
|
+
#
|
65
|
+
# entry = table.new_mutation_entry("user-1")
|
66
|
+
# entry.set_cell(
|
67
|
+
# "cf-1",
|
68
|
+
# "field-1",
|
69
|
+
# "XYZ"
|
70
|
+
# timestamp: Time.now.to_i * 1000 # Time stamp in milli seconds.
|
71
|
+
# ).delete_from_column("cf2", "field02")
|
72
|
+
#
|
73
|
+
# table.mutate_row(entry)
|
74
|
+
#
|
75
|
+
def mutate_row entry
|
76
|
+
client.mutate_row(
|
77
|
+
path,
|
78
|
+
entry.row_key,
|
79
|
+
entry.mutations,
|
80
|
+
app_profile_id: @app_profile_id
|
81
|
+
)
|
82
|
+
true
|
83
|
+
end
|
84
|
+
|
85
|
+
# Mutates multiple rows in a batch. Each individual row is mutated
|
86
|
+
# atomically as in MutateRow, but the entire batch is not executed
|
87
|
+
# atomically.
|
88
|
+
#
|
89
|
+
# @param entries [Array<Google::Cloud::Bigtable::MutationEntry>]
|
90
|
+
# The row keys and corresponding mutations to be applied in bulk.
|
91
|
+
# Each entry is applied as an atomic mutation, but the entries may be
|
92
|
+
# applied in arbitrary order (even between entries for the same row).
|
93
|
+
# At least one entry must be specified, and in total the entries can
|
94
|
+
# contain at most 100000 mutations.
|
95
|
+
# @return [Array<Google::Bigtable::V2::MutateRowsResponse::Entry>]
|
96
|
+
#
|
97
|
+
# @example
|
98
|
+
# require "google/cloud"
|
99
|
+
#
|
100
|
+
# bigtable = Google::Cloud::Bigtable.new
|
101
|
+
#
|
102
|
+
# table = bigtable.table("my-instance", "my-table")
|
103
|
+
#
|
104
|
+
# entries = []
|
105
|
+
# entries << table.new_mutation_entry("row-1").set_cell("cf1", "field1", "XYZ")
|
106
|
+
# entries << table.new_mutation_entry("row-2").set_cell("cf1", "field1", "ABC")
|
107
|
+
# table.mutate_row(entries)
|
108
|
+
#
|
109
|
+
def mutate_rows entries
|
110
|
+
RowsMutator.new(self, entries).apply_mutations
|
111
|
+
end
|
112
|
+
|
113
|
+
# Modifies a row atomically on the server. The method reads the latest
|
114
|
+
# existing timestamp and value from the specified columns and writes a new
|
115
|
+
# entry based on pre-defined read/modify/write rules. The new value for the
|
116
|
+
# timestamp is the greater of the existing timestamp or the current server
|
117
|
+
# time. The method returns the new contents of all modified cells.
|
118
|
+
#
|
119
|
+
# @param key [String]
|
120
|
+
# The key of the row to which the read/modify/write rules should be applied.
|
121
|
+
# @param rules [Google::Cloud::Bigtable::ReadModifyWriteRule, Array<Google::Cloud::Bigtable::ReadModifyWriteRule>]
|
122
|
+
# Rules specifying how the specified row's contents are to be transformed
|
123
|
+
# into writes. Entries are applied in order, meaning that earlier rules will
|
124
|
+
# affect the results of later ones.
|
125
|
+
# @return [Google::Cloud::Bigtable::Row]
|
126
|
+
# @example Apply multiple modification rules.
|
127
|
+
# require "google/cloud/bigtable"
|
128
|
+
#
|
129
|
+
# bigtable = Google::Cloud::Bigtable.new
|
130
|
+
# table = bigtable.table("my-instance", "my-table")
|
131
|
+
#
|
132
|
+
# rule_1 = table.new_read_modify_write_rule("cf", "field01")
|
133
|
+
# rule_1.append("append-xyz")
|
134
|
+
#
|
135
|
+
# rule_2 = table.new_read_modify_write_rule("cf", "field01")
|
136
|
+
# rule_2.increment(1)
|
137
|
+
#
|
138
|
+
# row = table.read_modify_write_row("user01", [rule_1, rule_2])
|
139
|
+
#
|
140
|
+
# puts row.cells
|
141
|
+
#
|
142
|
+
# @example Apply single modification rules.
|
143
|
+
# require "google/cloud/bigtable"
|
144
|
+
#
|
145
|
+
# bigtable = Google::Cloud::Bigtable.new
|
146
|
+
# table = bigtable.table("my-instance", "my-table")
|
147
|
+
#
|
148
|
+
# rule = table.new_read_modify_write_rule("cf", "field01").append("append-xyz")
|
149
|
+
#
|
150
|
+
# row = table.read_modify_write_row("user01", rule)
|
151
|
+
#
|
152
|
+
# puts row.cells
|
153
|
+
#
|
154
|
+
def read_modify_write_row key, rules
|
155
|
+
res_row = client.read_modify_write_row(
|
156
|
+
path,
|
157
|
+
key,
|
158
|
+
Array(rules).map(&:to_grpc),
|
159
|
+
app_profile_id: @app_profile_id
|
160
|
+
).row
|
161
|
+
row = Row.new(res_row.key)
|
162
|
+
|
163
|
+
res_row.families.each do |family|
|
164
|
+
family.columns.each do |column|
|
165
|
+
column.cells.each do |cell|
|
166
|
+
row_cell = Row::Cell.new(
|
167
|
+
family.name,
|
168
|
+
column.qualifier,
|
169
|
+
cell.timestamp_micros,
|
170
|
+
cell.value,
|
171
|
+
cell.labels
|
172
|
+
)
|
173
|
+
row.cells[family.name] << row_cell
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
row
|
178
|
+
end
|
179
|
+
|
180
|
+
# Mutates a row atomically based on the output of a predicate Reader filter.
|
181
|
+
#
|
182
|
+
# NOTE: Condition predicate filter is not supported.
|
183
|
+
#
|
184
|
+
# @param key [String] Row key.
|
185
|
+
# The key of the row to which the conditional mutation should be applied.
|
186
|
+
# @param predicate [SimpleFilter, ChainFilter, InterleaveFilter] Predicate filter.
|
187
|
+
# The filter to be applied to the contents of the specified row. Depending
|
188
|
+
# on whether or not any results are yielded, either +true_mutations+ or
|
189
|
+
# +false_mutations+ will be executed. If unset, checks that the row contains
|
190
|
+
# any values at all.
|
191
|
+
# @param on_match [Google::Cloud::Bigtable::MutationEntry] Mutation entry apply on predicate filter match.
|
192
|
+
# Changes to be atomically applied to the specified row if +predicate_filter+
|
193
|
+
# yields at least one cell when applied to +row_key+. Entries are applied in
|
194
|
+
# order, meaning that earlier mutations can be masked by later ones.
|
195
|
+
# Must contain at least one entry if +false_mutations+ is empty, and at most
|
196
|
+
# 100000.
|
197
|
+
# @param otherwise [Google::Cloud::Bigtable::MutationEntry] Mutation entry apply on predicate filter do not match.
|
198
|
+
# Changes to be atomically applied to the specified row if +predicate_filter+
|
199
|
+
# does not yield any cells when applied to +row_key+. Entries are applied in
|
200
|
+
# order, meaning that earlier mutations can be masked by later ones.
|
201
|
+
# Must contain at least one entry if +true_mutations+ is empty, and at most
|
202
|
+
# 100000.
|
203
|
+
# @return [Boolean]
|
204
|
+
# Predicate match or not status
|
205
|
+
# @example
|
206
|
+
# require "google/cloud/bigtable"
|
207
|
+
#
|
208
|
+
# bigtable = Google::Cloud::Bigtable.new
|
209
|
+
# table = bigtable.table("my-instance", "my-table")
|
210
|
+
#
|
211
|
+
# predicate_filter = Google::Cloud::Bigtable::RowFilter.key("user-10")
|
212
|
+
# on_match_mutations = Google::Cloud::Bigtable::MutationEntry.new
|
213
|
+
# on_match_mutations.set_cell(
|
214
|
+
# "cf-1",
|
215
|
+
# "field-1",
|
216
|
+
# "XYZ",
|
217
|
+
# timestamp: Time.now.to_i * 1000 # Time stamp in micro seconds.
|
218
|
+
# ).delete_from_column("cf2", "field02")
|
219
|
+
#
|
220
|
+
# otherwise_mutations = Google::Cloud::Bigtable::MutationEntry.new
|
221
|
+
# otherwise_mutations.delete_from_family("cf3")
|
222
|
+
#
|
223
|
+
# response = table.check_and_mutate_row(
|
224
|
+
# "user01",
|
225
|
+
# predicate_filter,
|
226
|
+
# on_match: on_match_mutations,
|
227
|
+
# otherwise: otherwise_mutations
|
228
|
+
# )
|
229
|
+
#
|
230
|
+
# if response
|
231
|
+
# puts "All predicates matched"
|
232
|
+
# end
|
233
|
+
#
|
234
|
+
def check_and_mutate_row \
|
235
|
+
key,
|
236
|
+
predicate,
|
237
|
+
on_match: nil,
|
238
|
+
otherwise: nil
|
239
|
+
true_mutations = on_match.mutations if on_match
|
240
|
+
false_mutations = otherwise.mutations if otherwise
|
241
|
+
response = client.check_and_mutate_row(
|
242
|
+
path,
|
243
|
+
key,
|
244
|
+
predicate_filter: predicate.to_grpc,
|
245
|
+
true_mutations: true_mutations,
|
246
|
+
false_mutations: false_mutations,
|
247
|
+
app_profile_id: @app_profile_id
|
248
|
+
)
|
249
|
+
response.predicate_matched
|
250
|
+
end
|
251
|
+
|
252
|
+
# Read sample row keys.
|
253
|
+
#
|
254
|
+
# Returns a sample of row keys in the table. The returned row keys will
|
255
|
+
# delimit contiguous sections of the table of approximately equal size,
|
256
|
+
# which can be used to break up the data for distributed tasks like
|
257
|
+
# mapreduces.
|
258
|
+
#
|
259
|
+
# @yieldreturn [Google::Cloud::Bigtable::SampleRowKey]
|
260
|
+
# @return [:yields: sample_row_key]
|
261
|
+
# Yield block for each processed SampleRowKey.
|
262
|
+
#
|
263
|
+
# @example
|
264
|
+
# require "google/cloud"
|
265
|
+
#
|
266
|
+
# bigtable = Google::Cloud::Bigtable.new
|
267
|
+
# table = bigtable.table("my-instance", "my-table")
|
268
|
+
#
|
269
|
+
# table.sample_row_keys.each do |sample_row_key|
|
270
|
+
# p sample_row_key.key # user00116
|
271
|
+
# p sample_row_key.offset # 805306368
|
272
|
+
# end
|
273
|
+
#
|
274
|
+
def sample_row_keys
|
275
|
+
return enum_for(:sample_row_keys) unless block_given?
|
276
|
+
|
277
|
+
response = client.sample_row_keys(
|
278
|
+
path,
|
279
|
+
app_profile_id: @app_profile_id
|
280
|
+
)
|
281
|
+
response.each do |grpc|
|
282
|
+
yield SampleRowKey.from_grpc(grpc)
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
# Create instance of mutation_entry
|
287
|
+
#
|
288
|
+
# @param row_key [String] Row key. Optional
|
289
|
+
# The key of the row to which the mutation should be applied.
|
290
|
+
# @return [Google::Cloud::Bigtable::MutationEntry]
|
291
|
+
#
|
292
|
+
# @example
|
293
|
+
# require "google/cloud/bigtable"
|
294
|
+
#
|
295
|
+
# bigtable = Google::Cloud::Bigtable.new
|
296
|
+
# table = bigtable.table("my-instance", "my-table")
|
297
|
+
#
|
298
|
+
# entry = table.new_mutation_entry("row-key-1")
|
299
|
+
#
|
300
|
+
# # Without row key
|
301
|
+
# entry = table.new_mutation_entry
|
302
|
+
#
|
303
|
+
def new_mutation_entry row_key = nil
|
304
|
+
Google::Cloud::Bigtable::MutationEntry.new(row_key)
|
305
|
+
end
|
306
|
+
|
307
|
+
# Create instance of ReadModifyWriteRule to append or increment value
|
308
|
+
# of the cell qualifier.
|
309
|
+
#
|
310
|
+
# @param family [String]
|
311
|
+
# The name of the family to which the read/modify/write should be applied.
|
312
|
+
# @param qualifier [String]
|
313
|
+
# The qualifier of the column to which the read/modify/write should be
|
314
|
+
# @return [Google::Cloud::Bigtable::ReadModifyWriteRule]
|
315
|
+
#
|
316
|
+
# @example Create rule to append to qualifier value.
|
317
|
+
# require "google/cloud/bigtable"
|
318
|
+
#
|
319
|
+
# bigtable = Google::Cloud::Bigtable.new
|
320
|
+
# table = bigtable.table("my-instance", "my-table")
|
321
|
+
# rule = table.new_read_modify_write_rule("cf", "qualifier-1")
|
322
|
+
# rule.append("append-xyz")
|
323
|
+
#
|
324
|
+
# @example Create rule to increment qualifier value.
|
325
|
+
# require "google/cloud/bigtable"
|
326
|
+
#
|
327
|
+
# bigtable = Google::Cloud::Bigtable.new
|
328
|
+
# table = bigtable.table("my-instance", "my-table")
|
329
|
+
# rule = table.new_read_modify_write_rule("cf", "qualifier-1")
|
330
|
+
# rule.increment(100)
|
331
|
+
#
|
332
|
+
def new_read_modify_write_rule family, qualifier
|
333
|
+
Google::Cloud::Bigtable::ReadModifyWriteRule.new(family, qualifier)
|
334
|
+
end
|
335
|
+
end
|
336
|
+
end
|
337
|
+
end
|
338
|
+
end
|