public class HBaseDML {标签:info,对表,String,Bytes,API,toString,result,toBytes,HBase From: https://www.cnblogs.com/csj-717/p/16725372.html
//静态属性
public static Connection conn = HBaseConnection2.conn;
//添加数据
public void putCell(Contract contract) {
try {
Table table = conn.getTable(TableName.valueOf("test1", "Contract"));
Put put = new Put(Bytes.toBytes(contract.getRowkey()));
put.addColumn(
Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes(contract.getName()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("rowkey"), Bytes.toBytes(contract.getAddress()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes(contract.getAddress()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("place"), Bytes.toBytes(contract.getPlace()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("email"), Bytes.toBytes(contract.getEmail()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("faren"), Bytes.toBytes(contract.getFaren()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("contacts"), Bytes.toBytes(contract.getContacts()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("phone"), Bytes.toBytes(contract.getPhone()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("shuxing"), Bytes.toBytes(contract.getShuxing()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("introduce"), Bytes.toBytes(contract.getIntroduce()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("needName"), Bytes.toBytes(contract.getNeedName()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("needIntroduce"), Bytes.toBytes(contract.getNeedIntroduce()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(contract.getMoney()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("plan"), Bytes.toBytes(contract.getPlan()))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("khl"), Bytes.toBytes(contract.getKhl()));
table.put(put);
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public List<Contract> scanRows() {
List<Contract> contractList = new ArrayList<Contract>();
try {
Table table = conn.getTable(TableName.valueOf("test1", "Contract"));
//创建scan
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
while (iterator.hasNext()){
Result result = iterator.next();
String rowkey = Bytes.toString(result.getRow());
Cell cname = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("name"));
Cell caddress = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("address"));
Cell cplace = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("place"));
Cell cemail = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("email"));
Cell cfaren = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("faren"));
Cell ccontacts = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("contacts"));
Cell cphone = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("phone"));
Cell cshuxing = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("shuxing"));
Cell cintroduce = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("introduce"));
Cell cneedName = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("needName"));
Cell cneedIntroduce = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("needIntroduce"));
Cell cmoney = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("money"));
Cell cplan = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("plan"));
Cell ckhl = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("khl"));
String name = Bytes.toString(CellUtil.cloneValue(cname));
String address = Bytes.toString(CellUtil.cloneValue(caddress));
String place = Bytes.toString(CellUtil.cloneValue(cplace));
String email = Bytes.toString(CellUtil.cloneValue(cemail));
String faren = Bytes.toString(CellUtil.cloneValue(cfaren));
String contacts = Bytes.toString(CellUtil.cloneValue(ccontacts));
String phone = Bytes.toString(CellUtil.cloneValue(cphone));
String shuxing = Bytes.toString(CellUtil.cloneValue(cshuxing));
String introduce = Bytes.toString(CellUtil.cloneValue(cintroduce));
String needName = Bytes.toString(CellUtil.cloneValue(cneedName));
String needIntroduce = Bytes.toString(CellUtil.cloneValue(cneedIntroduce));
String money = Bytes.toString(CellUtil.cloneValue(cmoney));
String plan = Bytes.toString(CellUtil.cloneValue(cplan));
String khl = Bytes.toString(CellUtil.cloneValue(ckhl));
Contract contract = new Contract(rowkey, name, address, place, email, faren, contacts, phone, shuxing, introduce, needName, needIntroduce, money, plan, khl);
contractList.add(contract);
}
table.close();
} catch (IOException e) {
e.printStackTrace();
}
return contractList;
}
public List<Contract> filterByOName(Contract contract) {
List<Contract> contractList = new ArrayList<Contract>();
try {
Table table = conn.getTable(TableName.valueOf("test1", "Contract"));
Scan scan = new Scan();
//添加过滤
FilterList filterList = new FilterList();
//创建过滤器
SingleColumnValueFilter valueFilter = new SingleColumnValueFilter(
//列族名
Bytes.toBytes("info"),
//列名
Bytes.toBytes("name"),
//比较关系
CompareOperator.EQUAL,
//值
Bytes.toBytes(contract.getName())
);
filterList.addFilter(valueFilter);
scan.setFilter(filterList);
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
while (iterator.hasNext()){
Result result = iterator.next();
String rowkey = Bytes.toString(result.getRow());
Cell cname = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("name"));
Cell caddress = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("address"));
Cell cplace = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("place"));
Cell cemail = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("email"));
Cell cfaren = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("faren"));
Cell ccontacts = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("contacts"));
Cell cphone = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("phone"));
Cell cshuxing = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("shuxing"));
Cell cintroduce = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("introduce"));
Cell cneedName = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("needName"));
Cell cneedIntroduce = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("needIntroduce"));
Cell cmoney = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("money"));
Cell cplan = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("plan"));
Cell ckhl = result.getColumnLatestCell(Bytes.toBytes("info"),Bytes.toBytes("khl"));
String name = Bytes.toString(CellUtil.cloneValue(cname));
String address = Bytes.toString(CellUtil.cloneValue(caddress));
String place = Bytes.toString(CellUtil.cloneValue(cplace));
String email = Bytes.toString(CellUtil.cloneValue(cemail));
String faren = Bytes.toString(CellUtil.cloneValue(cfaren));
String contacts = Bytes.toString(CellUtil.cloneValue(ccontacts));
String phone = Bytes.toString(CellUtil.cloneValue(cphone));
String shuxing = Bytes.toString(CellUtil.cloneValue(cshuxing));
String introduce = Bytes.toString(CellUtil.cloneValue(cintroduce));
String needName = Bytes.toString(CellUtil.cloneValue(cneedName));
String needIntroduce = Bytes.toString(CellUtil.cloneValue(cneedIntroduce));
String money = Bytes.toString(CellUtil.cloneValue(cmoney));
String plan = Bytes.toString(CellUtil.cloneValue(cplan));
String khl = Bytes.toString(CellUtil.cloneValue(ckhl));
Contract contract1 = new Contract(rowkey, name, address, place, email, faren, contacts, phone, shuxing, introduce, needName, needIntroduce, money, plan, khl);
contractList.add(contract1);
}
table.close();
} catch (IOException e) {
e.printStackTrace();
}
return contractList;
}
}