Rajagopal M
Rajagopal M

Reputation: 23

Google Spanner - Java Mutation - Delete rows with subset of composite key values

I have a requirement where I need to delete rows based on the subset of composite keys via mutation.

I have a table

CREATE TABLE FOO
(KEY1 INT64, KEY2 INT64, KEY3 INT64, VALUE STRING(100))
PRIMARY KEY (KEY1, KEY2, KEY3)

However I need a mutation that should delete all rows matching KEY1 and KEY2 ignoring KEY3 value. I am trying to achieve with Mutation and I get this error

My Code:

Mutation.delete("FOO",
            KeySet.newBuilder().
                    addKey(Key.of(key1Value,
                            key2Value))
                    .build())

Error:

com.google.cloud.spanner.SpannerException: FAILED_PRECONDITION: io.grpc.StatusRuntimeException: FAILED_PRECONDITION: Wrong number of key parts for FOO. Expected: 3. Got: ["943598","213122"]

I have links for all composite keys but not a subset of them: Deleting row using composite key

Upvotes: 1

Views: 625

Answers (1)

Knut Olav Løite
Knut Olav Løite

Reputation: 3532

You can use KeySet.prefixRange(..) for this:

Mutation.delete("my-table", KeySet.prefixRange(Key.of(key1Value, key2Value)));

Upvotes: 2

Related Questions