UpdateJSON.updateTableWithIndex()
UpdateJSON.updateTableWithIndex()
メソッドは、索引を使用して更新を実行します。
この読取り操作は、すべてのエラー・チェックを実行する必要がないため、前のメソッドよりもかなり容易に実装できます。索引で識別される表の行のみが返され、ネットワーク・トラフィックが減少し、コードで検査する行が少なくなるため、より効率的でもあります。ただし、必要な索引が存在しない場合、このメソッドは失敗します。
// Update the zip code found on all Boston home addresses
// to "02102-1000"
//
// Because we have an index available to us, we only have to look
// at those rows which have person.address.home.city = Boston.
// All other rows are skipped during the read operation.
private void updateTableWithIndex(KVStore kvstore) {
TableAPI tableH = kvstore.getTableAPI();
Table myTable = tableH.getTable("personContacts");
// Construct the IndexKey.
Index homeCityIdx = myTable.getIndex("idx_home_city");
IndexKey homeCityIdxKey = null;
// If NullPointerException is thrown by createIndexKey(),
// it means that the required index has not been created.
// Run the createIndex() method before running this method.
homeCityIdxKey = homeCityIdx.createIndexKey();
// Return only those entries with a home city of "Boston"
homeCityIdxKey.put("person.address.home.city", "Boston");
// Iterate over the returned table rows. Because we're
// using an index, we're guaranteed that
// person.address.home.city exists and equals Boston
// for every table row seen here.
TableIterator<Row> iter =
tableH.tableIterator(homeCityIdxKey, null, null);
try {
while (iter.hasNext()) {
Row row = iter.next();
updateZipCode(tableH, row, "home", "02102-1000");
}
} finally {
if (iter != null) {
iter.close();
}
}
System.out.println("Updated a table using an index.");
}