HBase Java API 1 导入pom依赖 <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.3.1</version> </dependency> 2 添加配置文件 在resource目录下创建hbase-site.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>hbase.zookeeper.quorum</name> <value>zk01,zk02,zk03</value> <description>The directory shared by region servers. </description> </property> </configuration> 3 连接Hbase // hbase的两种连接方式:1)读取配置文件 只需要配置zookeeper Configuration config = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(config); //2)通过代码配置 Configuration configuration = new Configuration(); configuration.set("hbase.zookeeper.quorum", "zk01:2181,zk02:2181,zk03:2181"); connection = ConnectionFactory.createConnection(); 4 创建表 public static void main(String[] args) throws IOException { // 1.连接HBase // 1.1 HBaseConfiguration.create(); Configuration config = HBaseConfiguration.create(); // 1.2 创建一个连接 Connection connection = ConnectionFactory.createConnection(config); // 1.3 从连接中获得一个Admin对象 Admin admin = connection.getAdmin(); // 2.创建表 // 2.1 判断表是否存在 TableName tableName = TableName.valueOf("user"); if (!admin.tableExists(tableName)) { //2.2 如果表不存在就创建一个表 HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); hTableDescriptor.addFamily(new HColumnDescriptor("base_info")); admin.createTable(hTableDescriptor); System.out.println("创建表"); } } 5 打印表的信息 @Before public void initConnection() { try { connection = ConnectionFactory.createConnection(config); } catch (IOException e) { System.out.println("连接数据库失败"); } } @Test public void tableInfo() throws IOException { // 1.定义表的名称 TableName tableName = TableName.valueOf("user"); // 2.获取表 Table table = connection.getTable(tableName); // 3.获取表的描述信息 HTableDescriptor tableDescriptor = table.getTableDescriptor(); // 4.获取表的列簇信息 HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies(); for (HColumnDescriptor columnFamily : columnFamilies) { // 5.获取表的columFamily的字节数组 byte[] name = columnFamily.getName(); // 6.使用hbase自带的bytes工具类转成string String value = Bytes.toString(name); // 7.打印 System.out.println(value); } } 6 添加数据(PUT) @Before public void initConnection() { try { connection = ConnectionFactory.createConnection(config); } catch (IOException e) { System.out.println("连接数据库失败"); } } @Test public void put() throws IOException { // 1.定义表的名称 TableName tableName = TableName.valueOf("user"); // 2.获取表 Table table = connection.getTable(tableName); // 3.准备数据 String rowKey = "rowkey_10"; Put zhangsan = new Put(Bytes.toBytes(rowKey)); zhangsan.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("张三")); zhangsan.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("sex"), Bytes.toBytes("1")); zhangsan.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("address"), Bytes.toBytes("北京市")); zhangsan.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("birthday"), Bytes.toBytes("2014-07-10")); // 4. 添加数据 table.put(zhangsan); table.close(); } 7 获取数据(Get) @Test public void get() throws IOException { // 1.定义表的名称 TableName tableName = TableName.valueOf("user"); // 2.获取表 Table table = connection.getTable(tableName); // 3.准备数据 String rowKey = "rowkey_10"; // 4.拼装查询条件 Get get = new Get(Bytes.toBytes(rowKey)); // 5.查询数据 Result result = table.get(get); // 6.打印数据 获取所有的单元格 List<Cell> cells = result.listCells(); for (Cell cell : cells) { // 打印rowkey,family,qualifier,value System.out.println(Bytes.toString(CellUtil.cloneRow(cell)) + "==> " + Bytes.toString(CellUtil.cloneFamily(cell)) + "{" + Bytes.toString(CellUtil.cloneQualifier(cell)) + ":" + Bytes.toString(CellUtil.cloneValue(cell)) + "}"); } } 8 全表扫描(scan 慎用) @Test public void scan() throws IOException { // 1.定义表的名称 TableName tableName = TableName.valueOf("user"); // 2.获取表 Table table = connection.getTable(tableName); // 3.全表扫描 Scan scan = new Scan(); // 4.获取扫描结果 ResultScanner scanner = table.getScanner(scan); Result result = null; // 5. 迭代数据 while ((result = scanner.next()) != null) { // 6.打印数据 获取所有的单元格 List<Cell> cells = result.listCells(); for (Cell cell : cells) { // 打印rowkey,family,qualifier,value System.out.println(Bytes.toString(CellUtil.cloneRow(cell)) + "==> " + Bytes.toString(CellUtil.cloneFamily(cell)) + "{" + Bytes.toString(CellUtil.cloneQualifier(cell)) + ":" + Bytes.toString(CellUtil.cloneValue(cell)) + "}"); } } }