HBase综合测试(头歌实践教学项目)

HBase综合测试

第一关:HBase-shell命令
在命令行依次输入以下命令:

start-hbase.sh

hbase shell

create 'exam_tb1',  {NAME=>'user_info'},{NAME=>'class_info'}

put 'exam_tb1','row-1','user_info:name','jack'
put 'exam_tb1','row-1','user_info:age','32'
put 'exam_tb1','row-1','class_info:class_name','software'
put 'exam_tb1','row-1','class_info:class_id','201801'

put 'exam_tb1','row-2','user_info:name','rose'
put 'exam_tb1','row-2','user_info:age','28'
put 'exam_tb1','row-2','class_info:class_name','hardware'
put 'exam_tb1','row-2','class_info:class_id','201802'

第2关:HBase Java API

	/********* Begin *********/
	Configuration conf = HBaseConfiguration.create(); //使用create()静态方法就可以得到Configuration对象
	Connection conn = ConnectionFactory.createConnection(conf); //config为前文的配置对象
	Admin admin = conn.getAdmin(); //使用连接对象获取Admin对象
	TableName tableName = TableName.valueOf("emp_tb1");//定义表名
	HTableDescriptor htd = new HTableDescriptor(tableName);//定义表对象
	HColumnDescriptor hcd1 = new HColumnDescriptor("emp_info");//定义列族对象
    HColumnDescriptor hcd2 = new HColumnDescriptor("dept_info");//定义列族对象
	htd.addFamily(hcd1); //添加
    htd.addFamily(hcd2); //添加
	admin.createTable(htd);//创建表
	
    // 停用表
	admin.disableTable(TableName.valueOf("step2_tb0"));
    // 停用表
	admin.disableTable(TableName.valueOf("step2_tb1"));
	// 删除表
	admin.deleteTable(TableName.valueOf("step2_tb1"));

	// 获取一个操作指定表的table对象,进行DML操作
	Table table = conn.getTable(TableName.valueOf("emp_tb1"));
	
	// 构造要插入的数据为一个Put类型(一个put对象只能对应一个rowkey)的对象
	Put put = new Put(Bytes.toBytes("201101"));
	put.addColumn(Bytes.toBytes("emp_info"), Bytes.toBytes("emp_name"), Bytes.toBytes("lucy"));
	put.addColumn(Bytes.toBytes("emp_info"), Bytes.toBytes("emp_id"), Bytes.toBytes("1"));
	put.addColumn(Bytes.toBytes("dept_info"), Bytes.toBytes("gender"), Bytes.toBytes("man"));
	put.addColumn(Bytes.toBytes("dept_info"), Bytes.toBytes("dept_id"), Bytes.toBytes("2001"));
    put.addColumn(Bytes.toBytes("dept_info"), Bytes.toBytes("dept_name"), Bytes.toBytes("finance"));

	Put put2 = new Put(Bytes.toBytes("201102"));
	put2.addColumn(Bytes.toBytes("emp_info"), Bytes.toBytes("emp_name"), Bytes.toBytes("alpha"));
	put2.addColumn(Bytes.toBytes("emp_info"), Bytes.toBytes("emp_id"), Bytes.toBytes("2"));
	put2.addColumn(Bytes.toBytes("dept_info"), Bytes.toBytes("gender"), Bytes.toBytes("woman"));
	put2.addColumn(Bytes.toBytes("dept_info"), Bytes.toBytes("dept_id"), Bytes.toBytes("2003"));
    put2.addColumn(Bytes.toBytes("dept_info"), Bytes.toBytes("dept_name"), Bytes.toBytes("techenology"));		


	Put put3 = new Put(Bytes.toBytes("201103"));
	put3.addColumn(Bytes.toBytes("emp_info"), Bytes.toBytes("emp_name"), Bytes.toBytes("linus"));
	put3.addColumn(Bytes.toBytes("emp_info"), Bytes.toBytes("emp_id"), Bytes.toBytes("3"));
	put3.addColumn(Bytes.toBytes("dept_info"), Bytes.toBytes("gender"), Bytes.toBytes("man"));
	put3.addColumn(Bytes.toBytes("dept_info"), Bytes.toBytes("dept_id"), Bytes.toBytes("3002"));
    put3.addColumn(Bytes.toBytes("dept_info"), Bytes.toBytes("dept_name"), Bytes.toBytes("logistics"));
    
	ArrayList<Put> puts = new ArrayList<>();
	puts.add(put);
	puts.add(put2);
    puts.add(put3);
	
	// 插进去
	table.put(puts);
	
	table.close();
	conn.close();

	/********* End *********/

第三关:HBase扫描

/********* Begin *********/
Configuration config = new Configuration();
Connection conn = ConnectionFactory.createConnection(config);
Admin admin = conn.getAdmin();
TableName tablename = TableName.valueOf(tableName);
Table table = conn.getTable(tablename);
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));
scan.setStartRow(Bytes.toBytes("row-10"));//设置从...开始扫描 
scan.setStopRow(Bytes.toBytes("row-50"));//设置到...结束
ResultScanner scanner = table.getScanner(scan);
for(Result res : scanner){
    System.out.println(Bytes.toString(res.getRow()));
	for(Cell cell : res.listCells()){
        String family = Bytes.toString(CellUtil.cloneFamily(cell));
        String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
        String value = Bytes.toString(CellUtil.cloneValue(cell));
        System.out.println("\t" + family + ":" + qualifier + " " + value);
    }	
}
/********* End *********/

第四关:HBase过滤器

	/********* Begin *********/
	Configuration conf = HBaseConfiguration.create();
	Connection connection = ConnectionFactory.createConnection(conf);
    Admin admin = connection.getAdmin();
    TableName tablename = TableName.valueOf(tName);
	Table table = connection.getTable(tablename);

    //行键大于20
   Filter equalFilter1 = new RowFilter(CompareOperator.GREATER_OR_EQUAL,
   new BinaryComparator(Bytes.toBytes("row20")));
   
     //单列值过滤器 电话号码
    SingleColumnValueFilter valueFilter =new SingleColumnValueFilter(Bytes.toBytes("data"),
    Bytes.toBytes("phone"),CompareOperator.EQUAL,
   new RegexStringComparator("^1[3|4|5|7|8][0-9]{9}$"));
   
    List<Filter> list =new ArrayList<>();
	list.add(valueFilter);
	list.add(equalFilter1);

	FilterList filterList1 =new FilterList(FilterList.Operator.MUST_PASS_ALL,list);

	 Scan scan1 = new Scan();
	 scan1.setFilter(filterList1);

	 ResultScanner scanner1 = table.getScanner(scan1);
	 for (Result result : scanner1) {
		 System.out.println(Bytes.toString(result.getRow()));
          for(Cell cell : result.listCells()){
                 String family = Bytes.toString(CellUtil.cloneFamily(cell));
                 String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                 String value = Bytes.toString(CellUtil.cloneValue(cell));
                 System.out.println("\t" + family + ":" + qualifier + " " + value);
                  }
            }
       scanner1.close();

	connection.close();
	
/********* End *********/

转载他人

  • 14
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
HBase提供了一个名为计数器(Counter)的特殊数据类型,用于在分布式环境下进行计数操作。计数器在HBase中是一种特殊的列族,可以在多个客户端之间进行原子性的加减操作,同时还支持读取操作。 要使用计数器,首先需要在HBase表中创建一个列族,并将其属性设置为“计数器”类型。然后,在客户端程序中,可以使用HBase API提供的Counter类来操作计数器。 下面是一个示例代码,用于在HBase中创建一个名为“mytable”的表,其中包含一个名为“mycf”的计数器列族: ```java Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin(); HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf("mytable")); HColumnDescriptor cfDesc = new HColumnDescriptor("mycf"); cfDesc.setValue(Counter.COUNTER, "true"); tableDesc.addFamily(cfDesc); admin.createTable(tableDesc); ``` 创建计数器列族后,可以使用Counter类进行计数操作。例如,要对一个名为“mycounter”的计数器进行加1操作,可以使用以下代码: ```java Table table = conn.getTable(TableName.valueOf("mytable")); Counter counter = table.getCounter(new Get(Bytes.toBytes("rowkey"))); counter.incrementColumnValue(Bytes.toBytes("mycf"), Bytes.toBytes("mycounter"), 1); ``` 其中,“rowkey”为要操作的行键。在上面的代码中,首先通过getTable方法获取表对象,然后使用getCounter方法获取名为“mycounter”的计数器对象,并调用incrementColumnValue方法对其进行加1操作。 要读取计数器的值,可以使用getCounterValue方法。例如,要读取名为“mycounter”的计数器的值,可以使用以下代码: ```java long value = counter.getCounterValue(); ``` 这样就可以在HBase中使用计数器了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值