我将用户名和密码作为 ColumnFamily 的列,以用户名作为键。
使用比较器创建列族 TestUserInfo = UTF8Type with column_metadata = [{column_name: username, validation_class:UTF8Type}, {column_name: password, validation_class:UTF8Type} ];
当密钥存在于数据库中时,以下代码可以正常工作。而当密钥不存在时抛出me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:Key may not be empty) 。
SliceQuery<String, String, String> sliceQuery = HFactory.createSliceQuery(cassandraDBObject.getKeyspace(), StringSerializer.get(), StringSerializer.get(), StringSerializer.get());
sliceQuery.setColumnFamily("TestUserInfo");
sliceQuery.setKey(userName);
sliceQuery.setColumnNames("username", "password");
String userNameFromDB = null;
String passwordFromDB = null;
try {
[Error here -->] QueryResult<ColumnSlice<String, String>> queryResult = sliceQuery.execute();
ColumnSlice<String, String> resultCol = queryResult.get();
System.out.println(resultCol);
userNameFromDB = resultCol.getColumnByName("username").getValue();
passwordFromDB = resultCol.getColumnByName("password").getValue();
} catch (Exception e) {
e.printStackTrace();
}
你能告诉我哪里出错了吗?或者这是预期的行为?如果是这样,那么我如何检查空结果?假设我正在检查来自 db 的登录信息。那么如果用户名(键)不在数据库中,那么我需要得到一个空结果对吗?
谢谢!