HBase API实战案例
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.判断表是否存在
package cn.org.yinzhengjie.bigdata.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.HBaseAdmin; import java.io.IOException; public class HBaseAPI { public static Configuration conf; //获取Configuration对象 static{ //使用HBaseConfiguration的单例方法实例化,该方法会自动帮咱们加载"hbase-default.xml"和"hbase-site.xml"文件. conf = HBaseConfiguration.create(); //如果你没有服务器的配置配置文件,那就得手动配置zookeeper地址及端口 conf.set("hbase.zookeeper.quorum", "hadoop101.yinzhengjie.org.cn"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } public static boolean isTableExist(String tableName) throws IOException { //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); TableName tablename = TableName.valueOf(tableName); return admin.tableExists(tablename); } public static void main(String[] args) throws IOException { String tableName = "teacher"; boolean exist = isTableExist(tableName); if (exist){ System.out.println(tableName + "表存在"); }else { System.out.println(tableName + "表不存在!"); } } }
二.创建表
package cn.org.yinzhengjie.bigdata.hbase; 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.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.HBaseAdmin; import java.io.IOException; public class HBaseAPI { public static Configuration conf; //获取Configuration对象 static{ //使用HBaseConfiguration的单例方法实例化,该方法会自动帮咱们加载"hbase-default.xml"和"hbase-site.xml"文件. conf = HBaseConfiguration.create(); //如果你没有服务器的配置配置文件,那就得手动配置zookeeper地址及端口 conf.set("hbase.zookeeper.quorum", "hadoop101.yinzhengjie.org.cn"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } public static boolean isTableExist(String tableName) throws IOException { //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); TableName tablename = TableName.valueOf(tableName); return admin.tableExists(tablename); } public static void createTable(String tableName, String... columnFamily) throws IOException{ Connection conn = ConnectionFactory.createConnection(conf); HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); //判断表是否存在 if(isTableExist(tableName)){ System.out.println("表" + tableName + "已存在"); System.exit(0); }else{ //创建表属性对象,表名需要转字节 HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName)); //创建多个列族 for(String cf : columnFamily){ descriptor.addFamily(new HColumnDescriptor(cf)); } //根据对表的配置,创建表 admin.createTable(descriptor); System.out.println("表" + tableName + "创建成功!"); } } public static void main(String[] args) throws IOException { String tableName = "engineer"; createTable(tableName,"professional_skill","project_experience","synopsis"); } }
三.插入数据
package cn.org.yinzhengjie.bigdata.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class HBaseAPI { public static Configuration conf; //获取Configuration对象 static{ //使用HBaseConfiguration的单例方法实例化,该方法会自动帮咱们加载"hbase-default.xml"和"hbase-site.xml"文件. conf = HBaseConfiguration.create(); //如果你没有服务器的配置配置文件,那就得手动配置zookeeper地址及端口 conf.set("hbase.zookeeper.quorum", "hadoop101.yinzhengjie.org.cn"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } public static boolean isTableExist(TableName tName) throws IOException { //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); return admin.tableExists(tName); } public static void addRowData(TableName tableName, String rowKey, String columnFamily, String column, String value) throws IOException{ //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); //获取指定的表对象 Table hTable = conn.getTable(tableName); //向表中插入数据 Put put = new Put(Bytes.toBytes(rowKey)); //向Put对象中组装数据 put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); hTable.put(put); hTable.close(); System.out.println("插入数据成功"); } public static void main(String[] args) throws IOException { String tableName = "engineer"; TableName tname = TableName.valueOf(tableName); addRowData(tname,"10001","synopsis","name","Jason Yin"); } }
四.获取某一行数据
package cn.org.yinzhengjie.bigdata.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class HBaseAPI { public static Configuration conf; //获取Configuration对象 static{ //使用HBaseConfiguration的单例方法实例化,该方法会自动帮咱们加载"hbase-default.xml"和"hbase-site.xml"文件. conf = HBaseConfiguration.create(); //如果你没有服务器的配置配置文件,那就得手动配置zookeeper地址及端口 conf.set("hbase.zookeeper.quorum", "hadoop101.yinzhengjie.org.cn"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } public static boolean isTableExist(TableName tName) throws IOException { //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); return admin.tableExists(tName); } public static void getRow(TableName tableName, String rowKey) throws IOException{ //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); //获取指定的表对象 Table hTable = conn.getTable(tableName); Get get = new Get(Bytes.toBytes(rowKey)); //遍历查询结果 Result result = hTable.get(get); for(Cell cell : result.rawCells()){ System.out.println("行键: " + Bytes.toString(result.getRow())); System.out.println("列族: " + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("列: " + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("值: " + Bytes.toString(CellUtil.cloneValue(cell))); System.out.println("时间戳: " + cell.getTimestamp()); } } public static void main(String[] args) throws IOException { String tableName = "engineer"; TableName tname = TableName.valueOf(tableName); getRow(tname,"10001"); } }
五.获取某一行指定列族的列数据
package cn.org.yinzhengjie.bigdata.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class HBaseAPI { public static Configuration conf; //获取Configuration对象 static{ //使用HBaseConfiguration的单例方法实例化,该方法会自动帮咱们加载"hbase-default.xml"和"hbase-site.xml"文件. conf = HBaseConfiguration.create(); //如果你没有服务器的配置配置文件,那就得手动配置zookeeper地址及端口 conf.set("hbase.zookeeper.quorum", "hadoop101.yinzhengjie.org.cn"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } public static boolean isTableExist(TableName tName) throws IOException { //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); return admin.tableExists(tName); } public static void getRowQualifier(TableName tableName, String rowKey, String family, String qualifier) throws IOException{ //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); //获取指定的表对象 Table hTable = conn.getTable(tableName); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier)); Result result = hTable.get(get); for(Cell cell : result.rawCells()){ System.out.println("行键: " + Bytes.toString(result.getRow())); System.out.println("列族: " + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("列: " + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("值: " + Bytes.toString(CellUtil.cloneValue(cell))); } } public static void main(String[] args) throws IOException { String tableName = "engineer"; TableName tname = TableName.valueOf(tableName); getRowQualifier(tname,"10001","synopsis","name"); } }
六.获取所有数据
package cn.org.yinzhengjie.bigdata.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class HBaseAPI { public static Configuration conf; //获取Configuration对象 static{ //使用HBaseConfiguration的单例方法实例化,该方法会自动帮咱们加载"hbase-default.xml"和"hbase-site.xml"文件. conf = HBaseConfiguration.create(); //如果你没有服务器的配置配置文件,那就得手动配置zookeeper地址及端口 conf.set("hbase.zookeeper.quorum", "hadoop101.yinzhengjie.org.cn"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } public static boolean isTableExist(TableName tName) throws IOException { //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); return admin.tableExists(tName); } public static void getAllRows(TableName tname) throws IOException{ //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); //获取指定的表对象 Table hTable = conn.getTable(tname); //得到用于扫描region的对象 Scan scan = new Scan(); //使用HTable得到resultcanner实现类的对象 ResultScanner resultScanner = hTable.getScanner(scan); for(Result result : resultScanner){ Cell[] cells = result.rawCells(); for(Cell cell : cells){ //得到rowkey System.out.println("行键: " + Bytes.toString(CellUtil.cloneRow(cell))); //得到列族 System.out.println("列族: " + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("列: " + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("值: " + Bytes.toString(CellUtil.cloneValue(cell))); } } } public static void main(String[] args) throws IOException { String tableName = "engineer"; TableName tname = TableName.valueOf(tableName); getAllRows(tname); } }
七.删除一行数据
package cn.org.yinzhengjie.bigdata.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class HBaseAPI { public static Configuration conf; //获取Configuration对象 static{ //使用HBaseConfiguration的单例方法实例化,该方法会自动帮咱们加载"hbase-default.xml"和"hbase-site.xml"文件. conf = HBaseConfiguration.create(); //如果你没有服务器的配置配置文件,那就得手动配置zookeeper地址及端口 conf.set("hbase.zookeeper.quorum", "hadoop101.yinzhengjie.org.cn"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } public static void deleteRow(TableName tName,String rowkey) throws IOException{ //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); //获取指定的表对象 Table hTable = conn.getTable(tName); //删除数据 Delete delete = new Delete(Bytes.toBytes(rowkey)); hTable.delete(delete); System.out.println("删除数据"); } public static void main(String[] args) throws IOException { String tableName = "engineer"; TableName tname = TableName.valueOf(tableName); deleteRow(tname,"10002"); } }
八.删除多行数据
package cn.org.yinzhengjie.bigdata.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class HBaseAPI { public static Configuration conf; //获取Configuration对象 static{ //使用HBaseConfiguration的单例方法实例化,该方法会自动帮咱们加载"hbase-default.xml"和"hbase-site.xml"文件. conf = HBaseConfiguration.create(); //如果你没有服务器的配置配置文件,那就得手动配置zookeeper地址及端口 conf.set("hbase.zookeeper.quorum", "hadoop101.yinzhengjie.org.cn"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } public static boolean isTableExist(TableName tName) throws IOException { //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); return admin.tableExists(tName); } public static void deleteMultiRow(TableName tname, String... rows) throws IOException{ //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); //获取指定的表对象 Table hTable = conn.getTable(tname); List<Delete> deleteList = new ArrayList<Delete>(); for(String row : rows){ Delete delete = new Delete(Bytes.toBytes(row)); deleteList.add(delete); } hTable.delete(deleteList); hTable.close(); System.out.println("数据删除成功...."); } public static void main(String[] args) throws IOException { String tableName = "engineer"; TableName tname = TableName.valueOf(tableName); deleteMultiRow(tname,"10001","10003"); } }
九.删除表
package cn.org.yinzhengjie.bigdata.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.HBaseAdmin; import java.io.IOException; public class HBaseAPI { public static Configuration conf; //获取Configuration对象 static{ //使用HBaseConfiguration的单例方法实例化,该方法会自动帮咱们加载"hbase-default.xml"和"hbase-site.xml"文件. conf = HBaseConfiguration.create(); //如果你没有服务器的配置配置文件,那就得手动配置zookeeper地址及端口 conf.set("hbase.zookeeper.quorum", "hadoop101.yinzhengjie.org.cn"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } public static boolean isTableExist(TableName tName) throws IOException { //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); return admin.tableExists(tName); } public static void dropTable(TableName tName) throws IOException{ //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); if(isTableExist(tName)){ admin.disableTable(tName); admin.deleteTable(tName); System.out.println("表" + tName + "删除成功!"); }else{ System.out.println("表" + tName + "不存在!"); } } public static void main(String[] args) throws IOException { String tableName = "engineer"; TableName tname = TableName.valueOf(tableName); dropTable(tname); } }
package cn.org.yinzhengjie.bigdata.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import java.io.IOException; public class HBaseAPI { public static Configuration conf; //获取Configuration对象 static{ //使用HBaseConfiguration的单例方法实例化,该方法会自动帮咱们加载"hbase-default.xml"和"hbase-site.xml"文件. conf = HBaseConfiguration.create(); //如果你没有服务器的配置配置文件,那就得手动配置zookeeper地址及端口 conf.set("hbase.zookeeper.quorum", "hadoop101.yinzhengjie.org.cn"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } public static void dropTable(TableName tName) throws IOException{ //在HBase中管理、访问表需要先创建HBaseAdmin对象 Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin(); if (admin.tableExists(tName)){ //先禁用表 admin.disableTable(tName); //然后再删除表 admin.deleteTable(tName); System.out.println("表已经删除...."); } } public static void main(String[] args) throws IOException { String tableName = "engineer"; TableName tname = TableName.valueOf(tableName); dropTable(tname); } }
十.过滤器应用案例(无索引定位,因此效率较低,不推荐使用,生产环境中使用建议新建表做二级索引以空间换时间。)
package cn.org.yinzhengjie.bigdata.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class HBaseAPI { public static void main(String[] args) throws IOException { //创建配置对象 Configuration conf = HBaseConfiguration.create(); //获取HBase的连接对象 Connection conn = ConnectionFactory.createConnection(conf); //获取TableName对象 TableName tableName = TableName.valueOf("yinzhengjie2020:teacher"); Table table = conn.getTable(tableName); Scan scan = new Scan(); /** * //指定过滤条件,大于等于"10010"的ROWKEY * BinaryComparator bc = new BinaryComparator(Bytes.toBytes("10010")); * Filter f = new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL,bc); * scan.setFilter(f); * * //基于正则表示过滤只包含3个数字的行 * RegexStringComparator rsc = new RegexStringComparator("^\d{3}$"); * Filter f = new RowFilter(CompareFilter.CompareOp.EQUAL,rsc); * scan.setFilter(f); * * FilterList.Operator.MUST_PASS_ALL : * 表示AND关系. * FilterList.Operator.MUST_PASS_ONE: * 表示OR的关系 */ FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE); BinaryComparator bc = new BinaryComparator(Bytes.toBytes("10010")); RegexStringComparator rsc = new RegexStringComparator("^\d{3}$"); Filter f = new RowFilter(CompareFilter.CompareOp.EQUAL,rsc); RowFilter rf = new RowFilter(CompareFilter.CompareOp.EQUAL,bc); list.addFilter(f); list.addFilter(rf); /** * 扫描数据时,增加过滤器(效率较低,不推荐使用) * 所谓的过滤,其实每条数据都会筛选过滤,性能比较低,因此生产环境中使用时建议新建一张表做二级索引(以空间换时间)。 */ scan.setFilter(list); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { Cell[] cells = result.rawCells(); for(Cell cell : cells){ //得到rowkey System.out.println("行键: " + Bytes.toString(CellUtil.cloneRow(cell))); //得到列族 System.out.println("列族: " + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("列: " + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("值: " + Bytes.toString(CellUtil.cloneValue(cell))); } } //释放资源 table.close(); conn.close(); } }
十一.HBase API封装思路
package cn.org.yinzhengjie.bigdata.hbase.util; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * HBase操作工具类 */ public class HBaseUtil { //创建ThreadLocal对象(会在各个线程里单独开辟一块共享内存空间),目的是为了同一个线程内实现数据共享,它的作用并不是解决线程安全问题哟~ private static ThreadLocal<Connection> connHolder = new ThreadLocal<Connection>(); /** * 将构造方法私有化,禁止该工具类被实例化 */ private HBaseUtil(){} /** * 获取HBase连接对象 */ public static void makeHbaseConnection() throws IOException { //获取连接 Connection conn = connHolder.get(); //第一次获取连接为空,因此需要手动创建Connection对象 if (conn == null){ //使用HBaseConfiguration的单例方法实例化,该方法会自动帮咱们加载"hbase-default.xml"和"hbase-site.xml"文件. Configuration conf = HBaseConfiguration.create(); conn = ConnectionFactory.createConnection(conf); connHolder.set(conn); } } /** * 增加数据 */ public static void insertData (String tableName, String rowKey, String columnFamily, String column, String value) throws IOException{ //获取连接 Connection conn = connHolder.get(); //获取表 Table table = conn.getTable(TableName.valueOf(tableName)); //创建Put对象,需要指定往哪个"RowKey"插入数据 Put put = new Put(Bytes.toBytes(rowKey)); //记得添加列族信息 put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value)); //执行完这行代码数据才会被真正写入到HBase哟~ table.put(put); //记得关闭表 table.close(); } /** * 关闭连接 */ public static void close() throws IOException { //获取连接 Connection conn = connHolder.get(); if (conn != null){ conn.close(); //关闭连接后记得将其从ThreadLocal的内存中移除以释放空间。 connHolder.remove(); } } }
package cn.org.yinzhengjie.bigdata.hbase; import cn.org.yinzhengjie.bigdata.hbase.util.HBaseUtil; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class HBaseAPI { public static void main(String[] args) throws IOException { //获取HBase连接对象 HBaseUtil.makeHbaseConnection(); //插入数据 String tableName = "yinzhengjie2020:teacher"; //指定表名称 String rowkey = "10010"; //指定rowkey String family = "synopsis"; //指定列族 String column = "name"; //指定字段 String value = "尹正杰"; //指定字段对应的值 HBaseUtil.insertData(tableName,rowkey,family,column,value); //关闭连接 HBaseUtil.close(); } }
十二.小试牛刀
先判断"yinzhengjie2020:teacher"表是否存在,若不存在则创建该表,表中有一个"synopsis"列族,该列族有一个字段名为"name",其值为"JasonYin"。
使用HBase API查询"yinzhengjie2020:teacher"表中的数据.
package cn.org.yinzhengjie.bigdata.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * 测试HBase API */ public class TestHbaseAPI { public static void main(String[] args) throws IOException { //创建配置对象,获取HBase连接 Configuration conf = HBaseConfiguration.create(); //获取HBase的连接对象 Connection conn = ConnectionFactory.createConnection(conf); //获取操作对象 Admin admin = conn.getAdmin(); //操作数据库 //判断命名空间是否存在,若不存在就创建该命名空间. try { admin.getNamespaceDescriptor("yinzhengjie2020"); }catch (NamespaceNotFoundException e){ NamespaceDescriptor nd = NamespaceDescriptor.create("yinzhengjie2020").build(); admin.createNamespace(nd); } //判断HBase中的表是否存在,若不存在就创建该表 TableName tablename = TableName.valueOf("yinzhengjie2020:teacher"); //创建TableName对象 boolean flag = admin.tableExists(tablename); if (flag){ //获取指定的表对象 Table table = conn.getTable(tablename); //查询数据 String rowkey = "100001"; //Bytes.toBytes工具会进行字符编码,默认转换为"UTF-8" Get get = new Get(Bytes.toBytes(rowkey)); //获取查询结果 Result result = table.get(get); boolean empty = result.isEmpty(); if (empty){ //新增数据 Put put = new Put(Bytes.toBytes(rowkey)); String family = "synopsis"; //指定列族 String column = "name"; //指定字段 String value = "JasonYin"; //指定字段对应的值 put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value)); table.put(put); //执行完这行代码数据才会被真正写入到HBase哟~ System.out.println("数据增加成功...."); }else { //展示数据 for (Cell cell : result.rawCells()) { //使用Cell的工具类直接拿到咱们想要的数据 System.out.println("RowKey = " + Bytes.toString(CellUtil.cloneRow(cell))); System.out.println("Family = " + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("Column = " + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("Value = " + Bytes.toString(CellUtil.cloneValue(cell))); } } }else { //创建表描述对象 HTableDescriptor tableDescriptor = new HTableDescriptor(tablename); //增加列族 HColumnDescriptor synopsis = new HColumnDescriptor("synopsis"); tableDescriptor.addFamily(synopsis); //创建表 admin.createTable(tableDescriptor); System.out.println("表创建成功...."); } //关闭数据库连接 conn.close(); } }