Mazout
Mazout

Reputation: 104

Atomic way of inserting a row if it does not exist in bigtable?

We would like to insert a row if it does not exist in Bigtable. Our idea is to use the CheckAndMutateRow API with an onNoMatch filter. We are using the nodejs SDK. The idea is to do the following (it seems to works, but we don't know about the atomicity of the operation):

  const row = table.row('phone#4c410523#20190501');
  const filter = [];

  const config = {
    onNoMatch: [
      {
        method: 'insert',
        data: {
          stats_summary: {
            os_name: 'android',
            timestamp,
          },
        },
      },
    ],
  };

  await row.filter(filter, config);

Upvotes: 3

Views: 1167

Answers (2)

Bora
Bora

Reputation: 131

MutateRow does an upsert. So if you give it a rowkey, column name and timestamp it will create a new cell if it doesn't exist, or overwrite it otherwise. You can achieve this behavior with a "simple write".

Conditional writes are use e.g. if you want to check the value before you overwrite it. Let's say you want to set column A to X only if column B is Y or overwrite column A only if column A's current value is Z.

Upvotes: 0

Mabel A.
Mabel A.

Reputation: 2025

CheckAndMutateRow is atomic. Based on the API definition:

Mutations are applied atomically and in order, meaning that earlier mutations can be masked / negated by later ones. Cells already present in the row are left unchanged unless explicitly changed by a mutation.
After committing the accumulated mutations, resets the local mutations.

Upvotes: 1

Related Questions