我正在使用hbaseTemplate执行put,并将autoFlush属性设置为true(默认值)并且它工作正常。然而,当我在代码中执行put和a get时,我没有得到我的对象:(
把功能
hbaseTemplate.execute(tableName, new TableCallback<Object>()
{
@Override
public Object doInTable(HTableInterface table) throws IOException
{
if (contact.getContactID() == null)
{
contact.setContactID(UUID.randomUUID().toString());
}
Put put = new Put(Bytes.toBytes(contact.getCompositeKey()));
if (contact.getFirstName() != null) {
put.add(columnFamilyName, firstName, Bytes.toBytes(contact.getFirstName()));
}
if (contact.getLastName() != null) {
put.add(columnFamilyName, lastName, Bytes.toBytes(contact.getLastName()));
}
if (contact.getContactID() != null) {
put.add(columnFamilyName, contactID, Bytes.toBytes(contact.getContactID()));
}
table.put(put);
return null;
}
});
获取功能
public UserContact getContactByContactId(String contactId)
{
Scan scan = new Scan();
String criteria = contactId;
Filter filter = new SingleColumnValueFilter(columnFamilyName, contactID, CompareFilter.CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes(criteria)));
scan.setFilter(filter);
UserContact contact = null;
contact = hbaseTemplate.find(tableName, scan, new ResultsExtractor<UserContact>() {
@Override
public UserContact extractData(ResultScanner resultScanner) throws Exception
{
Result result = resultScanner.next();
return loadContact(result);
}
});
然后在客户端,我所做的只是
String contactId = "somerandomid";
UserContact contact = new UserContact();
contact.setContactId(contactId);
repo.put(contact);
UserContact c = repo.getContactByContactId(contactId);
// here c is null :(
但是使用shell我可以看到联系人添加得很好......