OBKV-Table supports global indexes, which behave the same as in SQL.
Scope of use
When a global index is specified for a partitioned table, its usage is limited by the partitioning methods supported by the client. Only the following partitioning methods are supported:
Partitioning method |
Java client |
|---|---|
key |
Supported |
hash |
Supported |
range |
Supported |
In addition, the client cannot parse or evaluate complex expressions and can perform partition calculation only based on columns. Therefore, a partitioning column cannot be an expression and must be one or more columns. For example:
Supported |
Not supported |
|---|---|
CREATE INDEX idx ON tb(C2) GLOBAL PARTITION BY HASH(C2);CREATE INDEX idx ON tb(C2) GLOBAL PARTITION BY RANGE(C2, C3); |
CREATE INDEX idx ON tb(C2) GLOBAL PARTITION BY HASH(C2+C3); |
Example of using global indexes with the Java client
The following is an example:
public void test_update() throws Exception {
test_update("test_global_hash_hash", 20);
test_update("test_global_key_key", 20);
test_update("test_global_range_range", 20);
}
public void test_update(String tableName, int recordCount) throws Exception {
try {
client.addRowKeyElement(tableName, new String[] { "C1" });
String[] properties_name = { "C2", "C3" };
// prepare data
for (int i = 0; i < recordCount; i++) {
int key = gen_key(i);
long affectRows = client.insert(tableName, new Object[] { key }, properties_name,
new Object[] { key + 1, ("hello " + key).getBytes() });
Assert.assertEquals(1, affectRows);
}
// update global index key
for (int i = 0; i < recordCount; i++) {
int key = gen_key(i);
long affectRows = client.update(tableName, new Object[] { key },
new String[] { "C2" }, new Object[] { key + 2 });
Assert.assertEquals(1, affectRows);
Map<String, Object> result = client.get(tableName, new Object[] { key },
properties_name);
Assert.assertEquals(key + 2, result.get("C2"));
Assert.assertEquals("hello " + key, result.get("C3"));
}
// update other key
for (int i = 0; i < recordCount; i++) {
int key = gen_key(i);
long affectRows = client.update(tableName, new Object[] { key },
new String[] { "C3" }, new Object[] { "hi " + key });
Assert.assertEquals(1, affectRows);
Map<String, Object> result = client.get(tableName, new Object[] { key },
properties_name);
Assert.assertEquals(key + 2, result.get("C2"));
Assert.assertEquals("hi " + key, result.get("C3"));
}
checkIndexTableRow(tableName, recordCount);
} finally {
// delete data
for (int i = 0; i < recordCount; i++) {
int key = gen_key(i);
long affectRows = client.delete(tableName, new Object[] { key });
Assert.assertEquals(1, affectRows);
}
checkIndexTableRow(tableName, 0);
}
}
For more examples of using global indexes with the Java client, see Global indexes in the Java client.