四 数据模型操作

数据模型的操作

    HBase 对数据模型的 4 个主要操作包括 Get、Put、Scan 和 Delete 通过 HTbale 实例进行操作。

    HBase 所有修改数据的操作都保证行级别的原子性。要么读到最新的数据,要么等待系统允许写入改行的修改。
    
1 Get 
    HTable 类中提供了 get() 方法, 同时还有与之对应的 Get 类Get 操作返回一行或多行数据
    get方法 获取数据时,HBase 返回的结果包含所有匹配的单元格数据,这些数据将被封装在一个 Result 实例中 返回给用户。Result提供的方法,可以从服务器端获取匹配指定行的特定返回值,这些值包括列族、列限定符和时间戳等。
 
               Configuration conf = HBaseConfiguration.create();
            Connection connection = ConnectionFactory.createConnection(conf);
            Table table=connection.getTable(TableName.valueOf(tableName));
            Get get=new Get(rowkey.getBytes());
            Result result = table.get(get);  
 
2 Put
    Put可以一次插入一行也可以一次插入一个集合。
    如果需要频繁修改某些行的数据,用户应该创建一个 RowLock 的实例来防止其他用户对该行数据进行修改(HBase线程,HBase事物,HBase锁)
    Put操作每次都会发起一次到服务器的RPC操作,如果有大量的数据要写入表中,就会有数千次 RPC 操作, 这样效率很低。 HBase客户端有一个缓冲区,负责将数据批量的仅通过一次 RPC 操作发送到服务端, 这样可以大大提高性能,默认客户端写缓冲区是关闭的,需要打开 (HBase优化)
 
    当一个 Put 集合提交到服务端的时候,可能会出现部分成功部分失败的情况,失败的数据会被保存到缓存区中进行重试。
 
    HBase 提供了一个 compare - and - set 操作, 这个操作先进行检查, 条件满足后再执行,这个操作对于行是有原子性的。 
    HBase 没有 Update 操作,是通过 Put 操作完成对数据的修改。
 
3 Scan
    Scan操作允许多行特定属性迭代,使用方式与 Get 的方式相似。 工作方式类似于迭代器。startRow 定义扫描 HBase 表的起始行键。stopRow 限定读取的停止。
    创建后默认扫描表的所有列族以及所有列。可以多种方法限定读取的数据。
    Scan 有很多优化,用好 Scan 将给 HBase 查询带来极大的提升。
 
 
public static void main(String[] args) throws IOException {
        //Scan类常用方法说明
        //指定需要的family或column ,如果没有调用任何addFamily或Column,会返回所有的columns; 
        // scan.addFamily(); 
        // scan.addColumn();
        // scan.setMaxVersions(); //指定最大的版本个数。如果不带任何参数调用setMaxVersions,表示取所有的版本。如果不掉用setMaxVersions,只会取到最新的版本.
        // scan.setTimeRange(); //指定最大的时间戳和最小的时间戳,只有在此范围内的cell才能被获取.
        // scan.setTimeStamp(); //指定时间戳
        // scan.setFilter(); //指定Filter来过滤掉不需要的信息
        // scan.setStartRow(); //指定开始的行。如果不调用,则从表头开始;
        // scan.setStopRow(); //指定结束的行(不含此行);
        // scan.setBatch(); //指定最多返回的Cell数目。用于防止一行中有过多的数据,导致OutofMemory错误。
        
        //过滤器
        //1、FilterList代表一个过滤器列表
        //FilterList.Operator.MUST_PASS_ALL -->and
        //FilterList.Operator.MUST_PASS_ONE -->or
        //eg、FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE);
        //2、SingleColumnValueFilter
        //3、ColumnPrefixFilter用于指定列名前缀值相等
        //4、MultipleColumnPrefixFilter和ColumnPrefixFilter行为差不多,但可以指定多个前缀。
        //5、QualifierFilter是基于列名的过滤器。
        //6、RowFilter
        //7、RegexStringComparator是支持正则表达式的比较器。
        //8、SubstringComparator用于检测一个子串是否存在于值中,大小写不敏感。
        
        HTable table=(HTable) getHTablePool().getTable("tb_stu");
        Scan scan=new Scan();
        scan.setMaxVersions();
        //指定最多返回的Cell数目。用于防止一行中有过多的数据,导致OutofMemory错误。
        scan.setBatch(1000);
 
        //scan.setTimeStamp(NumberUtils.toLong("1370336286283"));
        //scan.setTimeRange(NumberUtils.toLong("1370336286283"), NumberUtils.toLong("1370336337163"));
        //scan.setStartRow(Bytes.toBytes("quanzhou"));
        //scan.setStopRow(Bytes.toBytes("xiamen"));
        //scan.addFamily(Bytes.toBytes("info")); 
        //scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("id"));
        
        //查询列镞为info,列id值为1的记录
        //方法一(单个查询)
        // Filter filter = new SingleColumnValueFilter(
        //         Bytes.toBytes("info"), Bytes.toBytes("id"), CompareOp.EQUAL, Bytes.toBytes("1")); 
        // scan.setFilter(filter);
        
        //方法二(组合查询)
        //FilterList filterList=new FilterList();
        //Filter filter = new SingleColumnValueFilter(
        //    Bytes.toBytes("info"), Bytes.toBytes("id"), CompareOp.EQUAL, Bytes.toBytes("1"));
        //filterList.addFilter(filter);
        //scan.setFilter(filterList);
        
        ResultScanner rs = table.getScanner(scan);
        
        for (Result r : rs) {
            for (KeyValue kv : r.raw()) {
                System.out.println(String.format("row:%s, family:%s, qualifier:%s, qualifiervalue:%s, timestamp:%s.", 
                        Bytes.toString(kv.getRow()), 
                        Bytes.toString(kv.getFamily()), 
                        Bytes.toString(kv.getQualifier()), 
                        Bytes.toString(kv.getValue()),
                        kv.getTimestamp()));
            }
        }
        
        rs.close();
    }
 
 
 
 
 
 
 
 
                                                                                                                        God has given me a gift. Only one. I am the most complete fighter in the world. My whole life, I have trained. I must prove I am worthy of someting.                                                             rocky_24
 
 
 
 





God has given me a gift. Only one. I am the most complete fighter in the world. My whole life, I have trained. I must prove I am worthy of someting. rocky_24
原文地址:https://www.cnblogs.com/rocky24/p/48e5e17f94faf33ff036cf9ea2313c19.html