Skip to content

[BigTable] Insert new record with the existing row_key multiple times? #2677

Closed

Description

@dmitry-saritasa

Is it possible to append additional rows with the existing 'rowkey' in Bigtable?

it seems when I call

from operator import itemgetter
import random

def get_row_key(record, template):
    attrs = itemgetter(*template)
    vals = attrs(record)
    return '#'.join((str(v) for v in vals)

# main
record = dict(
    user=random.randint(10, 1000),
    viewability=30,
    developer=1,
    advertiser=2,
    campaign=3,
    game=4,
    spot=5,
    asset=6,
    sex='m',
    age=50,
    browser='chrome',
    browser_ver='55',
    os='android',
    os_ver='5.0',
    mobile=True,
    desktop=False,
    country='us',
    ip_addr='127.0.0.1',
    created=1478218018,
    tick_size=5,
    session='aaaa'
    is_generic=False
)

# pattern used in itemgetter
rowkey = ('asset', 'created', 'advertiser')
row = table.row(get_row_key(record, pattern))
for column, val in record.items():
    row.set_cell('stat', column, val)
row.commit()

so if I run code against the same record 3 times (which will produce the same rowkey for the row) I only get a single row. It seems 1st row is inserted, and then 2nd and 3rd mutate the same row. How to make it append? (OR row_key HAS TO BE UNIQUE?)

thanks,
Dmitry

Activity

  1. dhermes commented on Nov 4, 2016

    @dhermes
    Contributor

    @dmitry-saritasa Doing this will give you 3 cells in the same row/family/column with different timestamps.

    >>> client = bigtable.Client(admin=True)
    >>> instance = client.instance(something, 'us-central1-c')
    >>> table = instance.table('google-cloud-python-test-table')
    >>> table.list_column_families()
    {u'famfam': <google.cloud.bigtable.column_family.ColumnFamily object at 0x7f27987c1b10>}
    >>> cf = 'famfam'
    >>> row_key = 'foo#bar#baz'
    >>> row = table.row(row_key)
    >>>
    >>> col = b'hi'
    >>> for _ in range(3):
    ...     row.set_cell(cf, col, b'bye')
    ...     row.commit()
    ...
    >>>
    >>> row_data = table.read_row(row_key)
    >>> for cell in row_data.cells[cf][col]:
    ...     print((cell.value, cell.timestamp))
    ...
    ('bye', datetime.datetime(2016, 11, 4, 2, 43, 13, 566000, tzinfo=<UTC>))
    ('bye', datetime.datetime(2016, 11, 4, 2, 43, 13, 509000, tzinfo=<UTC>))
    ('bye', datetime.datetime(2016, 11, 4, 2, 43, 13, 445000, tzinfo=<UTC>))
  2. dhermes commented on Nov 4, 2016

    @dhermes
    Contributor

    I'm happy to re-open if I've misunderstood the question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: bigtableIssues related to the Bigtable API.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions