根据Rowkey从HBase中查询数据

时间:2022-04-22 03:43:50
    /**
* @Title: queryData
* @Description: 从HBase查询出数据
* @author xxxx
* @param tableName
* 表名
* @param rowkey
* rowkey
* @return 返回用户信息的list
* @throws IOException
* @throws UnsupportedEncodingException
*/
public static ArrayList<xxxxUserInfo> queryData(String tableName,
String rowkey) throws IOException, UnsupportedEncodingException
{
ArrayList<xxxxUserInfo> list = new ArrayList<xxxxUserInfo>(); Configuration config = SignHBase.getConfiguration();
HTable table = new HTable(config, tableName); Get get = new Get(rowkey.getBytes()); // 根据主键查询
Result r = table.get(get);
KeyValue[] kv = r.raw();
xxxxUserInfo xxxxUserInfo = new xxxxUserInfo();
for (int i = 0; i < kv.length; i++)
{
// 循环每一列
String qualifier = new String(kv[i].getQualifier());
if (qualifier.equals("n"))
{
String val = new String(kv[i].getValue());
xxxxUserInfo.setName(val);
}
else if (qualifier.equals("b"))
{
String val = new String(kv[i].getValue());
xxxxUserInfo.setBirthday(val);
}
else if (qualifier.equals("a"))
{
String val = new String(kv[i].getValue());
xxxxUserInfo.setAddress(val);
}
else if (qualifier.equals("z"))
{
String val = new String(kv[i].getValue());
xxxxUserInfo.setZip(val);
}
else if (qualifier.equals("f"))
{
String val = new String(kv[i].getValue());
xxxxUserInfo.setFax(val);
}
else if (qualifier.equals("cid"))
{
String val = new String(kv[i].getValue());
xxxxUserInfo.setCtfid(val);
}
else if (qualifier.equals("m"))
{
String val = new String(kv[i].getValue());
xxxxUserInfo.setMobile(val);
}
else if (qualifier.equals("t"))
{
String val = new String(kv[i].getValue());
xxxxUserInfo.setTel(val);
}
else if (qualifier.equals("e"))
{
String val = new String(kv[i].getValue());
xxxxUserInfo.setEmail(val);
}
else if (qualifier.equals("g"))
{
String val = new String(kv[i].getValue());
xxxxUserInfo.setGender(val);
}
else if (qualifier.equals("n"))
{
String val = new String(kv[i].getValue());
xxxxUserInfo.setNationality(val);
}
else if (qualifier.equals("ct"))
{
String val = new String(kv[i].getValue());
xxxxUserInfo.setCtftp(val);
}
}// end of 遍历每一列
list.add(xxxxUserInfo);// 将查询到的结果写入List中
return list;
}