首页 > 其他分享 >HBase对表增查操作 API

HBase对表增查操作 API

时间:2022-09-24 12:33:06浏览次数:49  
标签:info 对表 String Bytes API toString result toBytes HBase

public class HBaseDML {
//静态属性
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;
}

}

标签:info,对表,String,Bytes,API,toString,result,toBytes,HBase
From: https://www.cnblogs.com/csj-717/p/16725372.html

相关文章

  • 连接HBase
    单线连接HBasepublicclassHBaseConnection{publicstaticvoidmain(String[]args)throwsIOException{Configurationconf=newConfigura......
  • 如何在Windows上一键部署PaddleOCR的WebAPI服务
    PaddleOCR旨在打造一套丰富、领先、且实用的OCR工具库,助力开发者训练出更好的模型,并应用落地。官方​开源项目地址:PaddlePaddle/PaddleOCR:AwesomemultilingualOCRtoo......
  • SAP Commerce Cloud OCC API UnknownResourceError 错误
    错误消息:{"errors":[{"message":"Thereisnoresourceforpath/occ/v2/powertools/xxx/","type":"UnknownResourceError"......
  • API接口
    一、前后端开发模式#以前开发项目是前端写好静态文件然后后端再用模板语法套到这个静态文件中之后衍生出了全栈开发就是前端后端都一个人写#然就现在逐渐开始前......
  • API接口与drf规范
    前后端开发模式1.前后端混合前端写好静态html页面,后端使用模板语法进行渲染,然后在在对接,遇到问题返回给前端进行修改,这要求后端人员会一些HTML、JS等前端语言,这种模式让......
  • 前后端开发模式、API接口、接口测试工具postman、restful规范、序列化和反序列化、dja
    目录前后端开发模式一、两种模式1.传统开发模式:前后端混合开发1.1.缺点:2.前后端分离开发模式2.1.特点3.补充老刘的相关博客:二、API接口1.作用2.说明三、接口测试工具postm......
  • 今日内容 API接口和drf的使用
    前后端开发模式详细见博客链接:https://www.cnblogs.com/liuqingzheng/p/10900502.html补充:前后端混合开发使用模板语法渲染模板后端人员要通过前端写好的html页面......
  • 微服务系列之Api文档 swagger整合
    1.前言微服务架构随之而来的前后端彻底分离,且服务众多,无论是前后端对接亦或是产品、运营翻看,一个现代化、规范化、可视化、可尝试的文档是多么重要,所以我们这节就说说......
  • API接口、接口测试工具postman、restful规范、序列化与反序列化、djangorestframework
    API接口通过网络,规定了前台后台信息交流规则的url链接,也就是前后台信息交互的媒介API接口的样子url:长得像返回数据的url链接https://api.map.baidu.com/place/v2/s......
  • 使用 Docker 的个性化排名 API
    使用Docker的个性化排名API在这篇文章中,我们将探讨如何设置和使用Metarank的个性化排名API和Redis持久化和码头工人撰写.数据准备Metarank是一个开源个......