|
21 | 21 | from google.cloud._helpers import _datetime_from_microseconds |
22 | 22 | from google.cloud._helpers import _to_bytes |
23 | 23 |
|
| 24 | +_MISSING_COLUMN_FAMILY = ( |
| 25 | + 'Column family {} is not among the cells stored in this row.') |
| 26 | +_MISSING_COLUMN = ( |
| 27 | + 'Column {} is not among the cells stored in this row in the ' |
| 28 | + 'column family {}.') |
| 29 | +_MISSING_INDEX = ( |
| 30 | + 'Index {!r} is not valid for the cells stored in this row for column {} ' |
| 31 | + 'in the column family {}. There are {} such cells.') |
| 32 | + |
24 | 33 |
|
25 | 34 | class Cell(object): |
26 | 35 | """Representation of a Google Cloud Bigtable Cell. |
@@ -175,6 +184,103 @@ def row_key(self): |
175 | 184 | """ |
176 | 185 | return self._row_key |
177 | 186 |
|
| 187 | + def find_cells(self, column_family_id, column): |
| 188 | + """Get a time series of cells stored on this instance. |
| 189 | +
|
| 190 | + Args: |
| 191 | + column_family_id (str): The ID of the column family. Must be of the |
| 192 | + form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``. |
| 193 | + column (bytes): The column within the column family where the cells |
| 194 | + are located. |
| 195 | +
|
| 196 | + Returns: |
| 197 | + List[~google.cloud.bigtable.row_data.Cell]: The cells stored in the |
| 198 | + specified column. |
| 199 | +
|
| 200 | + Raises: |
| 201 | + KeyError: If ``column_family_id`` is not among the cells stored |
| 202 | + in this row. |
| 203 | + KeyError: If ``column`` is not among the cells stored in this row |
| 204 | + for the given ``column_family_id``. |
| 205 | + """ |
| 206 | + try: |
| 207 | + column_family = self._cells[column_family_id] |
| 208 | + except KeyError: |
| 209 | + raise KeyError(_MISSING_COLUMN_FAMILY.format(column_family_id)) |
| 210 | + |
| 211 | + try: |
| 212 | + cells = column_family[column] |
| 213 | + except KeyError: |
| 214 | + raise KeyError(_MISSING_COLUMN.format(column, column_family_id)) |
| 215 | + |
| 216 | + return cells |
| 217 | + |
| 218 | + def cell_value(self, column_family_id, column, index=0): |
| 219 | + """Get a single cell value stored on this instance. |
| 220 | +
|
| 221 | + Args: |
| 222 | + column_family_id (str): The ID of the column family. Must be of the |
| 223 | + form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``. |
| 224 | + column (bytes): The column within the column family where the cell |
| 225 | + is located. |
| 226 | + index (Optional[int]): The offset within the series of values. If |
| 227 | + not specified, will return the first cell. |
| 228 | +
|
| 229 | + Returns: |
| 230 | + ~google.cloud.bigtable.row_data.Cell value: The cell value stored |
| 231 | + in the specified column and specified index. |
| 232 | +
|
| 233 | + Raises: |
| 234 | + KeyError: If ``column_family_id`` is not among the cells stored |
| 235 | + in this row. |
| 236 | + KeyError: If ``column`` is not among the cells stored in this row |
| 237 | + for the given ``column_family_id``. |
| 238 | + IndexError: If ``index`` cannot be found within the cells stored |
| 239 | + in this row for the given ``column_family_id``, ``column`` |
| 240 | + pair. |
| 241 | + """ |
| 242 | + cells = self.find_cells(column_family_id, column) |
| 243 | + |
| 244 | + try: |
| 245 | + cell = cells[index] |
| 246 | + except (TypeError, IndexError): |
| 247 | + num_cells = len(cells) |
| 248 | + msg = _MISSING_INDEX.format( |
| 249 | + index, column, column_family_id, num_cells) |
| 250 | + raise IndexError(msg) |
| 251 | + |
| 252 | + return cell.value |
| 253 | + |
| 254 | + def cell_values(self, column_family_id, column, max_count=None): |
| 255 | + """Get a time series of cells stored on this instance. |
| 256 | +
|
| 257 | + Args: |
| 258 | + column_family_id (str): The ID of the column family. Must be of the |
| 259 | + form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``. |
| 260 | + column (bytes): The column within the column family where the cells |
| 261 | + are located. |
| 262 | + max_count (int): The maximum number of cells to use. |
| 263 | +
|
| 264 | + Returns: |
| 265 | + A generator which provides: cell.value, cell.timestamp_micros |
| 266 | + for each cell in the list of cells |
| 267 | +
|
| 268 | + Raises: |
| 269 | + KeyError: If ``column_family_id`` is not among the cells stored |
| 270 | + in this row. |
| 271 | + KeyError: If ``column`` is not among the cells stored in this row |
| 272 | + for the given ``column_family_id``. |
| 273 | + """ |
| 274 | + cells = self.find_cells(column_family_id, column) |
| 275 | + if max_count is None: |
| 276 | + max_count = len(cells) |
| 277 | + |
| 278 | + for index, cell in enumerate(cells): |
| 279 | + if index == max_count: |
| 280 | + break |
| 281 | + |
| 282 | + yield cell.value, cell.timestamp_micros |
| 283 | + |
178 | 284 |
|
179 | 285 | class InvalidReadRowsResponse(RuntimeError): |
180 | 286 | """Exception raised to to invalid response data from back-end.""" |
|
0 commit comments