HBase 最新版本(1.0以上)测试案例
来源:互联网 发布:添加删除windows组件 编辑:程序博客网 时间:2024/05/21 15:41
最近写HBase,使用最近的1.0版本,发现很多功能都不建议使用,依据官方的文档写了份关于增删改查的测试案例,供学习交流。
import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.io.compress.Compression;import org.apache.hadoop.hbase.util.Bytes;import org.junit.Before;import org.junit.Test;import java.io.IOException;/** * Hbase 基本CRUD 样例代码 覆盖Put Get Delete checkAndPut checkAndDelete Scan * 通过上面的各种操作的例子, 会基本覆盖Htable可以用的的所有方法 * 这里不涉及Hbase 管理代码的操作 * @author seagle * */public class HbaseCRUDTest { @Before public void beforeTest(){ } @Test public void createTest() throws IOException { Configuration conf = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(conf); String tablename = "test"; String familyname="info"; Admin admin = connection.getAdmin(); HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tablename)); table.addFamily(new HColumnDescriptor(familyname).setCompressionType(Compression.Algorithm.SNAPPY)); System.out.print("Creating table. "); // 判断是否存在,如果存在,先删除. if (admin.tableExists(table.getTableName())) { admin.disableTable(table.getTableName()); admin.deleteTable(table.getTableName()); // 删除存在的表 } // 创建表 admin.createTable(table); System.out.println(" Done."); } @Test public void putTest() throws IOException { Configuration conf = HBaseConfiguration.create();// Use below code for HBase version 1.x.x or above. Connection connection = ConnectionFactory.createConnection(conf); TableName tableName = TableName.valueOf("test"); Table table = connection.getTable(tableName); Put put = new Put(Bytes.toBytes("8")); // row key// put.addColumn(Bytes.toBytes("family"),Bytes.toBytes("qualifier"),Bytes.toBytes("value")); put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("level_1"),Bytes.toBytes("4")); put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("level_2"),Bytes.toBytes("4")); put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("level_3"),Bytes.toBytes("4")); put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("level_4"),Bytes.toBytes("4")); table.put(put); } @Test public void getTest() throws IOException { Configuration conf = HBaseConfiguration.create();// Use below code for HBase version 1.x.x or above. Connection connection = ConnectionFactory.createConnection(conf); TableName tableName = TableName.valueOf("test"); Table table = connection.getTable(tableName); StringBuffer sb = null; Get get = new Get(Bytes.toBytes("8")); Result result = table.get(get); for (Cell c : result.rawCells()) { sb = new StringBuffer(Bytes.toString(CellUtil.cloneRow(c))); sb.append("==> "); sb.append(Bytes.toString(CellUtil.cloneFamily(c))); sb.append("{"); sb.append(Bytes.toString(CellUtil.cloneQualifier(c))); sb.append(":"); sb.append(Bytes.toString(CellUtil.cloneValue(c))); sb.append("}"); System.out.println(sb); } } @Test public void scanTest() throws IOException { Configuration conf = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(conf); TableName tableName = TableName.valueOf("test"); Table table = connection.getTable(tableName); StringBuffer sb = null; Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for (Result res : scanner) { for (Cell c : res.rawCells()) { sb = new StringBuffer(Bytes.toString(CellUtil.cloneRow(c))); sb.append(" ==> "); sb.append(Bytes.toString(CellUtil.cloneFamily(c))); sb.append(" {"); sb.append(Bytes.toString(CellUtil.cloneQualifier(c))); sb.append(":"); sb.append(Bytes.toString(CellUtil.cloneValue(c))); sb.append("}"); System.out.println(sb.toString()); } } } @Test public void modifyTest() throws IOException{ Configuration conf = HBaseConfiguration.create(); try (Connection connection = ConnectionFactory.createConnection(conf); Admin admin = connection.getAdmin()) { TableName tableName = TableName.valueOf("test"); if (!admin.tableExists(tableName)) { System.out.println("表不存在"); System.exit(-1); } HTableDescriptor table = new HTableDescriptor(tableName); // add family// HColumnDescriptor newColumn = new HColumnDescriptor("testinfo");// newColumn.setCompactionCompressionType(Compression.Algorithm.GZ);// newColumn.setMaxVersions(HConstants.ALL_VERSIONS);// admin.addColumn(tableName, newColumn); // Update existing column family HColumnDescriptor existingColumn = new HColumnDescriptor("info"); existingColumn.setCompactionCompressionType(Compression.Algorithm.GZ); existingColumn.setMaxVersions(HConstants.ALL_VERSIONS); table.modifyFamily(existingColumn); admin.modifyTable(tableName, table); // Disable an existing table// admin.disableTable(tableName); // Delete an existing column family// admin.deleteColumn(tableName, "testinfo".getBytes("UTF-8")); // Delete a table (Need to be disabled first)// admin.deleteTable(tableName); } } @Test public void deleteColumnDataTest() throws IOException { Configuration conf = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(conf); TableName tableName = TableName.valueOf("test"); Table table = connection.getTable(tableName); Delete delete = new Delete(Bytes.toBytes("8")); table.delete(delete); System.out.println("删除数据成功"); }}
1 0
- HBase 最新版本(1.0以上)测试案例
- Visual SVN最新2.0以上版本破解 -测试过 可用
- android最新源码(4.4.2_r1版本以上)下载
- android最新源码(4.4.2_r1版本以上)下载
- android最新源码(4.4.2_r1版本以上)下载
- chrome54以上版本flash不是最新版本解决方法
- 用hbase(0.92版本以上)的协处理器实现快速返回查询结果总数
- 用hbase(0.92版本以上)的协处理器实现快速返回查询结果总数 .
- 用hbase(0.92版本以上)的协处理器实现快速返回查询结果总数
- 【甘道夫】HBase(0.96以上版本)过滤器Filter详解及实例代码
- 用hbase(0.92版本以上)的协处理器实现快速返回查询结果总数
- HBase(0.96以上版本)过滤器Filter详解及实例代码
- 【甘道夫】HBase(0.96以上版本)过滤器Filter详解及实例代码
- HBase(0.96以上版本)过滤器Filter详解及实例代码
- HBase(0.96以上版本)过滤器Filter详解及实例代码
- HBase(0.96以上版本)过滤器Filter详解及实例代码
- HBase(0.96以上版本)过滤器Filter详解及实例代码
- HBase(0.96以上版本)过滤器Filter详解及实例代码
- 一步一步学习TypeScript(02.基本类型)
- 快速排序之区间划分
- Sublime快捷键
- Android中让Dialog全屏
- at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:54
- HBase 最新版本(1.0以上)测试案例
- 设置max-age|expires进行优化网站
- Foundation 框架知识点
- Linux Shell编程入门
- mysql主从同步常见异常及恢复方法
- 计算字符串的多少来计算label大小
- kafka SimpleConsumerDemo 消费者
- 森林的带度数层次序列存储
- android data binding