【Hbase】Java调用Hbase接口实现数据库操作
2015-03-04 10:42
507 查看
继上一篇文章
我们首先来替换下hbase的lib目录下hadoop相关的jar包,换成2.6的
在eclipse的java project中,直接引入hbase的lib下的所有jar
省的到时候这个类找不到,那个类找不到的,不然你就一个一个的jar去添加,尝试,找个最小集
下面看下java代码的实现吧
HBaseUtil
HBaseDemo
大家自行去测试吧,下面说下代码原理
1、判断表是否存在
2、实例化表的描述符
3、依次添加列族
4、创建表
我们首先来替换下hbase的lib目录下hadoop相关的jar包,换成2.6的
在eclipse的java project中,直接引入hbase的lib下的所有jar
省的到时候这个类找不到,那个类找不到的,不然你就一个一个的jar去添加,尝试,找个最小集
下面看下java代码的实现吧
HBaseUtil
package hbase; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; public class HBaseUtil { private Configuration conf; private HBaseAdmin admin; public HBaseUtil() throws IOException { Configuration cnf = new Configuration(); this.conf = HBaseConfiguration.create(cnf); this.admin = new HBaseAdmin(this.conf); } public HBaseUtil(Configuration conf) throws IOException { this.conf = HBaseConfiguration.create(conf); this.admin = new HBaseAdmin(this.conf); } public void createTable(String tableName, String columnFamily[]) { try { if (this.admin.tableExists(tableName)) { System.out .println("Table : " + tableName + " already exists !"); } else { HTableDescriptor td = new HTableDescriptor(tableName); int len = columnFamily.length; for (int i = 0; i < len; i++) { HColumnDescriptor family = new HColumnDescriptor( columnFamily[i]); td.addFamily(family); } admin.createTable(td); System.out.println(tableName + " 表创建成功!"); } } catch (Exception e) { e.printStackTrace(); System.out.println(tableName + " 表创建失败!"); } } public void deleteTable(String tableName) { try { if (this.admin.tableExists(tableName)) {
admin.disableTable(tableName); admin.deleteTable(tableName); System.out.println(tableName + " 表删除成功!"); } else { System.out.println(tableName + " 表不存在!"); } } catch (Exception e) { e.printStackTrace(); System.out.println(tableName + " 表删除失败!"); } } public void insertRecord(String tableName, String rowKey, String columnFamily, String qualifier, String value) { try { HTable table = new HTable(this.conf, tableName); Put put = new Put(rowKey.getBytes()); put.add(columnFamily.getBytes(), qualifier.getBytes(), value.getBytes()); table.put(put); System.out.println(tableName + " 表插入数据成功!"); } catch (Exception e) { e.printStackTrace(); System.out.println(tableName + " 表插入数据失败!"); } } public void deleteRecord(String tableName, String rowKey) { try { HTable table = new HTable(this.conf, tableName); Delete del = new Delete(rowKey.getBytes()); table.delete(del); System.out.println(tableName + " 表删除数据成功!"); } catch (Exception e) { e.printStackTrace(); System.out.println(tableName + " 表删除数据失败!"); } } public Result getOneRecord(String tableName, String rowKey) { try { HTable table = new HTable(this.conf, tableName); Get get = new Get(rowKey.getBytes()); Result rs = table.get(get); System.out.println(tableName + " 表获取数据成功!"); return rs; } catch (IOException e) { e.printStackTrace(); return null; } } public List<Result> getAllRecords(String tableName) { try { HTable table = new HTable(this.conf, tableName); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); List<Result> list = new ArrayList<Result>(); for (Result r : scanner) { list.add(r); } scanner.close(); System.out.println(tableName + " 表获取所有记录成功!"); return list; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }以上工具类是包含了所有的表的操作,下面再来看一个测试类
HBaseDemo
package hbase; import java.io.IOException; import org.apache.hadoop.conf.Configuration; public class HbaseDemo { public static void main(String[] args) { Configuration config = new Configuration(); config.set("hbase.zookeeper.quorum", "com23.authentication,com22.authentication,com21.authentication"); config.set("hbase.zookeeper.property.clientPort", "2181"); //config.set("hbase.master", "192.168.11.179:6000"); try { HBaseUtil hbase = new HBaseUtil(config); String tableName = "flume"; String[] columnFamily = {"chiwei","lining","taotao"}; hbase.createTable(tableName, columnFamily); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
大家自行去测试吧,下面说下代码原理
HBaseConfiguration
对hbase进行配置Configuration cnf = new Configuration(); this.conf = HBaseConfiguration.create(cnf); this.admin = new HBaseAdmin(this.conf); Configuration config = new Configuration(); config.set("hbase.zookeeper.quorum", "com23.authentication,com22.authentication,com21.authentication"); config.set("hbase.zookeeper.property.clientPort", "2181"); |
HBaseAdmin
管理Hbase数据库表信息的接口返回值 | 函数 | 描述 |
void | addColumn(String tableName, HColumnDescriptor column) | 向一个已经存在的表添加咧 |
checkHBaseAvailable(HBaseConfiguration conf) | 静态函数,查看HBase是否处于运行状态 | |
createTable(HTableDescriptor desc) | 创建一个表,同步操作 | |
deleteTable(byte[] tableName) | 删除一个已经存在的表 | |
enableTable(byte[] tableName) | 使表处于有效状态 | |
disableTable(byte[] tableName) | 使表处于无效状态 | |
HTableDescriptor[] | listTables() | 列出所有用户控件表项 |
void | modifyTable(byte[] tableName, HTableDescriptor htd) | 修改表的模式,是异步的操作,可能需要花费一定的时间 |
boolean | tableExists(String tableName) | 检查表是否存在 |
1、创建表
public void createTable(String tableName, String columnFamily[]) { try { if (this.admin.tableExists(tableName)) { System.out .println("Table : " + tableName + " already exists !"); } else { HTableDescriptor td = new HTableDescriptor(tableName); int len = columnFamily.length; for (int i = 0; i < len; i++) { HColumnDescriptor family = new HColumnDescriptor( columnFamily[i]); td.addFamily(family); } admin.createTable(td); System.out.println(tableName + " 表创建成功!"); } } catch (Exception e) { e.printStackTrace(); System.out.println(tableName + " 表创建失败!"); } }HTableDescriptor表的名字及其表的列族
返回值 | 函数 | 描述 |
void | addFamily(HColumnDescriptor) | 添加一个列族 |
HColumnDescriptor | removeFamily(byte[] column) | 移除一个列族 |
byte[] | getName() | 获取表的名字 |
byte[] | getValue(byte[] key) | 获取属性的值 |
void | setValue(String key, String value) | 设置属性的值 |
2、实例化表的描述符
3、依次添加列族
4、创建表
2、删除表
public void deleteTable(String tableName) { try { if (this.admin.tableExists(tableName)) { admin.deleteTable(tableName); System.out.println(tableName + " 表删除成功!"); } else { System.out.println(tableName + " 表不存在!"); } } catch (Exception e) { e.printStackTrace(); System.out.println(tableName + " 表删除失败!"); } }通过HBaseAdmin接口来删除表
3、插入一行记录
public void insertRecord(String tableName, String rowKey, String columnFamily, String qualifier, String value) { try { HTable table = new HTable(this.conf, tableName); Put put = new Put(rowKey.getBytes()); put.add(columnFamily.getBytes(), qualifier.getBytes(), value.getBytes()); table.put(put); System.out.println(tableName + " 表插入数据成功!"); } catch (Exception e) { e.printStackTrace(); System.out.println(tableName + " 表插入数据失败!"); } }实例化一个put对象,rowkey按照是每行的一个关键字,相当于主键,然后put添加列族和列修饰符以及列内容,最后调用table的put方法
4、查询一条记录
public Result getOneRecord(String tableName, String rowKey) { try { HTable table = new HTable(this.conf, tableName); Get get = new Get(rowKey.getBytes()); Result rs = table.get(get); System.out.println(tableName + " 表获取数据成功!"); return rs; } catch (IOException e) { e.printStackTrace(); return null; } }查询需要实例化Get对象,通过rowkey主键来查询
Result rs = hbase.getOneRecord("flume", "love"); for (Cell cell : rs.rawCells()) { System.out.println(new String(cell.getRow()) + " " + new String(cell.getFamily()) + " " + new String(cell.getQualifier()) + " " + new String(cell.getValue())); }
5、删除一条记录
public void deleteRecord(String tableName, String rowKey) { try { HTable table = new HTable(this.conf, tableName); Delete del = new Delete(rowKey.getBytes()); table.delete(del); System.out.println(tableName + " 表删除数据成功!"); } catch (Exception e) { e.printStackTrace(); System.out.println(tableName + " 表删除数据失败!"); } }实例化Delete对象,通过rowkey删除
6、查询所有记录
public List<Result> getAllRecords(String tableName) { try { HTable table = new HTable(this.conf, tableName); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); List<Result> list = new ArrayList<Result>(); for (Result r : scanner) { list.add(r); } scanner.close(); System.out.println(tableName + " 表获取所有记录成功!"); return list; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }通过实例化Scanner对象,逐条添加到list集合中
List<Result> list = hbase.getAllRecords("flume"); Iterator<Result> it = list.iterator(); while(it.hasNext()) { Result rs = it.next(); for (Cell cell : rs.rawCells()) { System.out.println(new String(cell.getRow()) + " " + new String(cell.getFamily()) + " " + new String(cell.getQualifier()) + " " + new String(cell.getValue())); } }
相关文章推荐
- java实现hbase数据库的增删改查操作(新API)
- 调用存储过程,java中通过bboss persistent实现数据库存储过程的调用(位置变量绑定)
- java调用mysql的工具类实现数据库远程备份,还原,生成创建表的sql脚本
- 基于java实现的数据库操作
- Java实现Neo4j数据库的相关操作
- 存储过程调用,java中通过bboss persistent实现数据库存储过程的调用(命名变量绑定)
- DB2数据库、触发器、存储过程、JAVA数据库链接(JDBC)、调用层接口(CLI)
- JAVA连接oracle数据库以及调用weka接口连接数据库
- java接口类型变量能调用已经被实现的这个接口的方法吗
- java--接口实现的操作实例
- 利用Java+sqlserver 实现对数据库的操作
- 在JAVA中实现数据库存储过程的统一调用
- 关于Java中Scanner对象的hasNext()方法对实现Readable接口的对象中的read()方法调用的探讨
- 基于Java操作HBase数据库
- JAVA调用接口实现
- java中通过调用oracle的function进行数据库操作
- (转)如何在linux C/C++语言中调用 sqlite 的函数接口来实现对数据库的管理
- [转]在Java中实现.net中DataTable功能以及操作双数据库的List连接问题解决方案探究
- Java在Eclipse环境下调用Subclipse接口完成SVN操作(附源码)
- Oracle 调用接口 (Orale Call Interface,OCI) 【实现各种语言操作Oracle数据库】简介