Database Reference
In-Depth Information
System.out.println("querying for userId : " + id);
byte[] key = Bytes.toBytes(id);
long t1 = System.nanoTime();
Get get = new Get(key);
long t2 = System.nanoTime();
Result result = htable.get(get);
if (result.isEmpty()) {
// first check, this row may not exist
System.out.println(" row user=" + id + " : not found");
} else {
// You always access the data through binary key
byte[] family = Bytes.toBytes(familyName);
byte[] emailCol = Bytes.toBytes("email");
KeyValuekv = result.getColumnLatest(family, emailCol);
if (kv == null) {
// even if row exist, this column may not
exist
System.out.println(" column 'email' not found");
} else {
// found
byte[] value = kv.getValue();
String email = new String(value);
System.out.println(" email=" + email);
}
}
System.out.println(" query time : " + (t2 - t1) / 1000000.0 +
" ms\n");
}
}
}
The preceding code will run, and it will give you output similar to that shown in the
following code. Some users will be found and some not, and that is exactly what we
want to test:
querying for userId : 197
row user=197 : not found
query time : 0.018413 ms
querying for userId : 148
row user=148 : not found
query time : 0.003088 ms
querying for userId : 178
 
Search WWH ::




Custom Search