按条件查询与删除数据-----HBase(伪分布)学习笔记
admin
2023-02-08 06:40:05
0

        

 /*
  * 删除RowKey所有行
  * 非空判断√
  */
  
  
 public static void deleteRows(String tablename,String RowKey){
         HTable h=null;
         HBaseAdmin ha=null;
         Get get=new Get(Bytes.toBytes(RowKey));
             try{
                 ha=new HBaseAdmin(config);
                 if(ha.tableExists(tablename)){
                 h=new HTable(config,tablename);
                 Delete d=new Delete(Bytes.toBytes(RowKey));
                 if(h.exists(get)){
                     h.delete(d);
                     System.out.println("删除成功");
                     }else{
                         System.out.println("滚犊子!!!");
                     }
                     }else{
                 System.out.println("表呢?你的表那!!");
             }
         }catch(Exception e){
             e.printStackTrace();
         }finally{
             try {
                h.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
         }
     }
 
 
 /*
  * 删除指定一行的数据
  *
  */
  
  
  
 public static void deleteRow(String tablename,String RowKey,String family,String qualifier){
        HTable h=null;
         try {
                h=new HTable(config,tablename);
                Delete d=new Delete(Bytes.toBytes(RowKey));
                d.deleteColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
                h.delete(d);
                System.out.println("删除成功!");  
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    h.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
         }
         
         
 /*
  * 按条件查询(应用filter)
  */
  
  
 @SuppressWarnings("deprecation")
public static void GetByParam(String tablename,String Family,String qualifier,String value){
        HTable h=null;
        ResultScanner rs=null;
             try {
                h=new HTable(config,tablename);
                Filter f=new SingleColumnValueFilter(Bytes.toBytes(Family),Bytes.toBytes(qualifier),CompareOp.EQUAL,Bytes.toBytes(value) );
                Scan scan=new Scan();
                scan.setFilter(f);
                scan.addColumn(Bytes.toBytes(Family),Bytes.toBytes(qualifier));
                rs=h.getScanner(scan);
                System.out.println("行     列簇     列名     值             时间戳");
                for (Result r : rs) {  
                for (KeyValue kv : r.raw()) {  
                    System.out.println(kv.getRowOffset()+"    "+Family+"       "+qualifier+"    "+new String(kv.getValue())+"    "+ kv.getTimestamp());
                }  
            }  
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{
                    try{
                         rs.close();  
                         h.close();
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
             }
             
             
 /*
  * 按条件查询value
  * 参数:表名,行,列簇,列名
  */
  
  
 public static void get(String tablename,String rowKey,String Family,String qualifier)throws Exception{
         HConnection connection = HConnectionManager.createConnection(config);
         HTableInterface table = connection.getTable(TableName.valueOf(tablename));
             try {
                 System.out.println("开始获取------哔哔哔");
                 Get get=new Get(rowKey.getBytes());
                 get.addColumn(Family.getBytes(), qualifier.getBytes());
                 Result r=table.get(get);
                 for(KeyValue kv:r.raw())
                    {
                        System.out.println("您要查的值为"+new String(kv.getValue()));
                    }
                     } finally {
                        table.close();
                        connection.close();
                     } 
                 }


相关内容

热门资讯

我来教教您“麻友圈2.怎么装挂... 网上科普关于“麻友圈2有没有挂”话题很是火热,小编也是针对麻友圈2作*弊开挂的方法以及开挂对应的知识...
最新引进“长虹大厅.是不是有挂... 您好:长虹大厅这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款游戏...
今日重大通报“旺旺冲击麻将.有... 家人们!今天小编来为大家解答旺旺冲击麻将透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里...
重磅消息“蜀山四川麻将.到底有... 有 亲,根据资深记者爆料蜀山四川麻将是可以开挂的,确实有挂(咨询软件无需...
火箭发射高度活跃 产业链上市公... 本报记者 丁蓉 近日,火箭发射活动十分密集。12月13日,快舟十一号遥八运载火箭在酒泉卫星发射中心发...
终于明白“边锋老友麻将.究竟有... 家人们!今天小编来为大家解答边锋老友麻将透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里...
【第一财经】“丫丫古诗.辅助器... 家人们!今天小编来为大家解答丫丫古诗透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买很...
我来教教您“新永和牛牛.开挂器... 有 亲,根据资深记者爆料新永和牛牛是可以开挂的,确实有挂(咨询软件无需打...
玩家分享攻略“十胡卡.辅助开挂... 家人们!今天小编来为大家解答十胡卡透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里买很多...
【第一消息】“九九牌游十三水.... 网上科普关于“九九牌游十三水有没有挂”话题很是火热,小编也是针对九九牌游十三水作*弊开挂的方法以及开...