2022.9.25周总结
Hbase数据库,由java_api操作
public class HBaseUtil { /** <br>功能描述: 初始化配置 <br>处理逻辑: <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 11:45 <br>修改记录: {修改人 修改原因 修改时间} * @param * @throws * @return org.apache.hadoop.conf.Configuration * @see # */ public static Configuration initConfig(){ Configuration config = new Configuration(); //记住 写服务名 config.set("hbase.zookeeper.quorum","hbasehost"); return config; } /** <br>功能描述: 获取数据库管理员对象 <br>处理逻辑: <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 11:46 <br>修改记录: {修改人 修改原因 修改时间} * @throws * @return org.apache.hadoop.hbase.client.Admin * @see # */ public static Admin getAdmin() throws IOException { Configuration conf = initConfig(); Connection connection = ConnectionFactory.createConnection(conf); Admin admin = connection.getAdmin(); return admin; } /** <br>功能描述: 检查hbase是否可用 <br>处理逻辑: <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 12:30 <br>修改记录: {修改人 修改原因 修改时间} * @param * @throws * @return void * @see # */ public static void isAvailable() throws IOException { Configuration conf = initConfig(); HBaseAdmin.available(conf); } /** <br>功能描述: 获取表名集合 <br>处理逻辑: <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 12:34 <br>修改记录: {修改人 修改原因 修改时间} * @param * @throws * @return java.util.List<java.lang.String> * @see # */ public static List<String> getTables() throws IOException { Admin admin = getAdmin(); TableName[] tableNames = admin.listTableNames(); List<String> tables = new ArrayList<String>(); for(TableName tableName:tableNames){ tables.add(tableName.getNameAsString()); } return tables; } /** <br>功能描述: 创建表 <br>处理逻辑: <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 15:09 <br>修改记录: {修改人 修改原因 修改时间} * @param tableName * @param cols * @throws * @return void * @see # */ public static void createTable(String tableName,List<String> cols) throws IOException { TableName tn = TableName.valueOf(tableName); HBaseAdmin hBaseAdmin = (HBaseAdmin) getAdmin(); if (hBaseAdmin.tableExists(tn)) { System.out.println("talbe is exists!"); } else { TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(tn); for (String col : cols) { ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.of(col); tdb.setColumnFamily(cfd); } hBaseAdmin.createTable(tdb.build()); } hBaseAdmin.close(); } /** <br>功能描述: 往表里面插值 <br>处理逻辑: <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 15:33 <br>修改记录: {修改人 修改原因 修改时间} * @param tableName * @param rowKey * @param columnFamilys * @param colValue * @throws * @return void * @see # */ public static void put(String tableName,String rowKey,String columnFamilys,Map<String,Object> colValue) throws IOException { Connection conn = getAdmin().getConnection(); Table table = conn.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowKey)); for(String col:colValue.keySet()){ put.addColumn(Bytes.toBytes(columnFamilys), Bytes.toBytes(col), ByteArrayUtils.objectToBytes(colValue.get(col)).get()); } table.put(put); table.close(); } /** <br>功能描述: 删除某行数据 <br>处理逻辑: <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 15:35 <br>修改记录: {修改人 修改原因 修改时间} * @param tableName * @param rowKey * @throws * @return void * @see # */ public static void delete(String tableName,String rowKey) throws IOException { Connection conn = getAdmin().getConnection(); Table table = conn.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowKey)); table.delete(delete); table.close(); } /** <br>功能描述: 获取行 <br>处理逻辑: <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 17:07 <br>修改记录: {修改人 修改原因 修改时间} * @param tableName * @param rowKey * @throws * @return java.util.List<com.alibaba.fastjson.JSONObject> * @see # */ public static List<JSONObject> get(String tableName, String rowKey) throws IOException { return get(tableName,rowKey,null, (List<String>) null); } /** <br>功能描述: 获取行的具体单元格数据 <br>处理逻辑: <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 17:07 <br>修改记录: {修改人 修改原因 修改时间} * @param tableName * @param rowKey * @param columnFamilys * @param col * @throws * @return com.alibaba.fastjson.JSONObject * @see # */ public static JSONObject get(String tableName,String rowKey,String columnFamilys,String col) throws IOException { List<String> cols = new ArrayList<>(); cols.add(col); List<JSONObject> objs = get(tableName,rowKey,columnFamilys,cols); if(objs!=null&&objs.size()>0){ return objs.get(0); } return null; } /** <br>功能描述: 获取行指定列簇的多个单元格数据 <br>处理逻辑: <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 17:08 <br>修改记录: {修改人 修改原因 修改时间} * @param tableName * @param rowKey * @param columnFamilys * @param cols * @throws * @return java.util.List<com.alibaba.fastjson.JSONObject> * @see # */ public static List<JSONObject> get(String tableName,String rowKey,String columnFamilys,List<String> cols) throws IOException { Connection conn = getAdmin().getConnection(); Table table = conn.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); if(cols!=null){ for(String col:cols){ get.addColumn(Bytes.toBytes(columnFamilys),Bytes.toBytes(col)); } } Result result = table.get(get); Cell[] cells = result.rawCells(); List<JSONObject> objs = new ArrayList<>(); for(Cell cell:cells){ JSONObject obj = new JSONObject(); obj.put("columnFamilys",Bytes.toString(CellUtil.cloneFamily(cell))); obj.put("qualifie",Bytes.toString(CellUtil.cloneQualifier(cell))); obj.put("value",ByteArrayUtils.bytesToObject(CellUtil.cloneValue(cell)).get()); objs.add(obj); } return objs; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
2021-09-27 每日总结:基本数据类型 (2021.9.27)