Ubuntu 14.04 LTS下HBase开发实例学习

在进行Hbase开发前,需要安装JDK、Hadoop和HBase,选择一款合适的开发IDE,具体安装方法就不介绍了,网上有很多参考资料,这里给出我的开发环境:

操作系统:Ubuntu 14.04 LTS

Java版本:jdk1.7.0_79

Hadoop版本:hadoop-2.6.0-cdh5.7.1

HBase版本:hbase-1.2.0-cdh5.7.1

Ecipse版本:Eclipse Java EE LunaRelease

Hadoop+HBase搭建云存储总结 PDF

Ubuntu Server 14.04 下 Hbase数据库安装 

HBase 结点之间时间不一致造成regionserver启动失败

Hadoop+ZooKeeper+HBase集群配置

Hadoop集群安装&HBase实验环境搭建

基于Hadoop集群的HBase集群的配置

Hadoop安装部署笔记之-HBase完全分布模式安装

单机版搭建HBase环境图文教程详解

使用Maven构建项目,在pom.xml中添加hbase的依赖如下:

<repositories> <repository> <id>cloudera</id> <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.6.0-cdh5.7.1</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.6.0-cdh5.7.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.2.0-cdh5.7.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.2.0-cdh5.7.1</version> </dependency> </dependencies> 2 初始化配置

首先需要设置HBase的配置,如ZooKeeper的地址、端口号等等。可以通过org.apache.hadoop.conf.Configuration.set方法手工设置HBase的配置信息,也可以直接将HBase的hbase-site.xml配置文件引入项目即可。下面给出配置代码:

// 声明静态配置 private static Configuration conf = null; static { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "localhost"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } 3 常见API的使用

HBase的常用操作包括建表、插入表数据、删除表数据、获取一行数据、表扫描、删除列族、删除表等等,下面给出具体代码。

3.1 创建数据库表 // 创建数据库表 public static void createTable(String tableName, String[] columnFamilys) throws IOException { // 建立一个数据库的连接 Connection conn = ConnectionFactory.createConnection(conf); // 创建一个数据库管理员 HBaseAdmin hAdmin = (HBaseAdmin) conn.getAdmin(); if (hAdmin.tableExists(tableName)) { System.out.println(tableName + "表已存在"); conn.close(); System.exit(0); } else { // 新建一个表描述 HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName)); // 在表描述里添加列族 for (String columnFamily : columnFamilys) { tableDesc.addFamily(new HColumnDescriptor(columnFamily)); } // 根据配置好的表描述建表 hAdmin.createTable(tableDesc); System.out.println("创建" + tableName + "表成功"); } conn.close(); } 3.2 添加一条数据 // 添加一条数据 public static void addRow(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException { // 建立一个数据库的连接 Connection conn = ConnectionFactory.createConnection(conf); // 获取表 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); // 通过rowkey创建一个put对象 Put put = new Put(Bytes.toBytes(rowKey)); // 在put对象中设置列族、列、值 put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); // 插入数据,可通过put(List<Put>)批量插入 table.put(put); // 关闭资源 table.close(); conn.close(); } 3.3 获取一条数据 // 通过rowkey获取一条数据 public static void getRow(String tableName, String rowKey) throws IOException { // 建立一个数据库的连接 Connection conn = ConnectionFactory.createConnection(conf); // 获取表 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); // 通过rowkey创建一个get对象 Get get = new Get(Bytes.toBytes(rowKey)); // 输出结果 Result result = table.get(get); for (Cell cell : result.rawCells()) { System.out.println( "行键:" + new String(CellUtil.cloneRow(cell)) + "\t" + "列族:" + new String(CellUtil.cloneFamily(cell)) + "\t" + "列名:" + new String(CellUtil.cloneQualifier(cell)) + "\t" + "值:" + new String(CellUtil.cloneValue(cell)) + "\t" + "时间戳:" + cell.getTimestamp()); } // 关闭资源 table.close(); conn.close(); } 3.4 全表扫描 // 全表扫描 public static void scanTable(String tableName) throws IOException { // 建立一个数据库的连接 Connection conn = ConnectionFactory.createConnection(conf); // 获取表 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); // 创建一个扫描对象 Scan scan = new Scan(); // 扫描全表输出结果 ResultScanner results = table.getScanner(scan); for (Result result : results) { for (Cell cell : result.rawCells()) { System.out.println( "行键:" + new String(CellUtil.cloneRow(cell)) + "\t" + "列族:" + new String(CellUtil.cloneFamily(cell)) + "\t" + "列名:" + new String(CellUtil.cloneQualifier(cell)) + "\t" + "值:" + new String(CellUtil.cloneValue(cell)) + "\t" + "时间戳:" + cell.getTimestamp()); } } // 关闭资源 results.close(); table.close(); conn.close(); } 3.5 删除一条数据 // 删除一条数据 public static void delRow(String tableName, String rowKey) throws IOException { // 建立一个数据库的连接 Connection conn = ConnectionFactory.createConnection(conf); // 获取表 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); // 删除数据 Delete delete = new Delete(Bytes.toBytes(rowKey)); table.delete(delete); // 关闭资源 table.close(); conn.close(); } 3.6 删除多条数据 // 删除多条数据 public static void delRows(String tableName, String[] rows) throws IOException { // 建立一个数据库的连接 Connection conn = ConnectionFactory.createConnection(conf); // 获取表 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); // 删除多条数据 List<Delete> list = new ArrayList<Delete>(); for (String row : rows) { Delete delete = new Delete(Bytes.toBytes(row)); list.add(delete); } table.delete(list); // 关闭资源 table.close(); conn.close(); } 3.7 删除列族 // 删除列族 public static void delColumnFamily(String tableName, String columnFamily) throws IOException { // 建立一个数据库的连接 Connection conn = ConnectionFactory.createConnection(conf); // 创建一个数据库管理员 HBaseAdmin hAdmin = (HBaseAdmin) conn.getAdmin(); // 删除一个表的指定列族 hAdmin.deleteColumn(tableName, columnFamily); // 关闭资源 conn.close(); } 3.8 删除数据库表 // 删除数据库表 public static void deleteTable(String tableName) throws IOException { // 建立一个数据库的连接 Connection conn = ConnectionFactory.createConnection(conf); // 创建一个数据库管理员 HBaseAdmin hAdmin = (HBaseAdmin) conn.getAdmin(); if (hAdmin.tableExists(tableName)) { // 失效表 hAdmin.disableTable(tableName); // 删除表 hAdmin.deleteTable(tableName); System.out.println("删除" + tableName + "表成功"); conn.close(); } else { System.out.println("需要删除的" + tableName + "表不存在"); conn.close(); System.exit(0); } } 3.9 追加插入 // 追加插入(将原有value的后面追加新的value,如原有value=a追加value=bc则最后的value=abc) public static void appendData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException { // 建立一个数据库的连接 Connection conn = ConnectionFactory.createConnection(conf); // 获取表 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); // 通过rowkey创建一个append对象 Append append = new Append(Bytes.toBytes(rowKey)); // 在append对象中设置列族、列、值 append.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); // 追加数据 table.append(append); // 关闭资源 table.close(); conn.close(); } 3.10 符合条件后添加数据 // 符合条件后添加数据(只能针对某一个rowkey进行原子操作) public static boolean checkAndPut(String tableName, String rowKey, String columnFamilyCheck, String columnCheck, String valueCheck, String columnFamily, String column, String value) throws IOException { // 建立一个数据库的连接 Connection conn = ConnectionFactory.createConnection(conf); // 获取表 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); // 设置需要添加的数据 Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); // 当判断条件为真时添加数据 boolean result = table.checkAndPut(Bytes.toBytes(rowKey), Bytes.toBytes(columnFamilyCheck), Bytes.toBytes(columnCheck), Bytes.toBytes(valueCheck), put); // 关闭资源 table.close(); conn.close(); return result; } 3.11 符合条件后删除数据 // 符合条件后刪除数据(只能针对某一个rowkey进行原子操作) public static boolean checkAndDelete(String tableName, String rowKey, String columnFamilyCheck, String columnCheck, String valueCheck, String columnFamily, String column) throws IOException { // 建立一个数据库的连接 Connection conn = ConnectionFactory.createConnection(conf); // 获取表 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); // 设置需要刪除的delete对象 Delete delete = new Delete(Bytes.toBytes(rowKey)); delete.addColumn(Bytes.toBytes(columnFamilyCheck), Bytes.toBytes(columnCheck)); // 当判断条件为真时添加数据 boolean result = table.checkAndDelete(Bytes.toBytes(rowKey), Bytes.toBytes(columnFamilyCheck), Bytes.toBytes(columnCheck), Bytes.toBytes(valueCheck), delete); // 关闭资源 table.close(); conn.close(); return result; } 3.12 计数器 // 计数器(amount为正数则计数器加,为负数则计数器减,为0则获取当前计数器的值) public static long incrementColumnValue(String tableName, String rowKey, String columnFamily, String column, long amount) throws IOException { // 建立一个数据库的连接 Connection conn = ConnectionFactory.createConnection(conf); // 获取表 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); // 计数器 long result = table.incrementColumnValue(Bytes.toBytes(rowKey), Bytes.toBytes(columnFamily), Bytes.toBytes(column), amount); // 关闭资源 table.close(); conn.close(); return result; } 4 内置过滤器的使用

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/97f77dcf3685a1c7f4e92aa468f8a008.html