这篇文章将为大家详细讲解有关HBase基本API操作之CRUD的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
import java.io.IOException; import java.util.Arrays; import java.util.List; 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.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; 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; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes;
创建一张userinfo表,对其表进行相关操作。
public class TestHBase { public static void main(String[] args) throws IOException { //2.获得会话 Admin admin = null; Connection con = null; try { //操作hbase数据库 //1.建立连接 Configuration conf = HBaseConfiguration.create(); //获得配制文件对象 conf.set("hbase.zookeeper.quorum", "192.168.226.129"); con = ConnectionFactory.createConnection(conf); //获得连接对象 admin = con.getAdmin(); //3.操作 //建立数据库 //a.判断数据库是否存在 TableName tn = TableName.valueOf("userinfo"); //创建表名对象 if ( admin.tableExists(tn) ) { System.out.println("----> 表存在, 删除表....."); admin.disableTable(tn); //使用表失效 admin.deleteTable(tn); //删除表 System.out.println("----> 删除表成功...."); }else{ System.out.println("----> 表不存在, 创建表....."); } //创建表结构对象:用于描述表名和相关的列族 HTableDescriptor htd = new HTableDescriptor(tn); //创建族列结构对象 HColumnDescriptor hcd1 = new HColumnDescriptor("familycolumn1"); HColumnDescriptor hcd2 = new HColumnDescriptor("familycolumn2"); //描述相关的列族 htd.addFamily(hcd1); htd.addFamily(hcd2); //创建表 admin.createTable(htd); System.out.println("创建表成功...."); //在表中插入数据 //a.单个插入 //参数是行键 "row01".getBytes() Put put = new Put(Bytes.toBytes("row1")); //定位行: put.addColumn(family, qualifier, value) put.addColumn(Bytes.toBytes("familycolumn1"), Bytes.toBytes("name"), Bytes.toBytes("Berg")); //获得表对象 Table table = con.getTable(tn); table.put(put); //添加数据 //b.批量插入 Put put01 = new Put(Bytes.toBytes("row2")); put01.addColumn(Bytes.toBytes("familycolumn2"), Bytes.toBytes("sex"), Bytes.toBytes("male")). addColumn(Bytes.toBytes("familycolumn2"), Bytes.toBytes("age"), Bytes.toBytes("22")); Put put02 = new Put(Bytes.toBytes("row3")); //参数是行键 "row01".getBytes() put02.addColumn(Bytes.toBytes("familycolumn1"), Bytes.toBytes("sex"), Bytes.toBytes("female")); List<Put> puts = Arrays.asList(put01, put02); Table table02 = con.getTable(tn); //获得表对象 table02.put(puts); //********************************* //读取操作 //实例化scan对象。 Scan scan = new Scan(); Table table03 = con.getTable(tn); //获得表对象 ResultScanner rs = table03.getScanner(scan); for (Result result : rs) { List<Cell> cs = result.listCells(); for (Cell cell : cs) { String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); //取行键 long timestamp = cell.getTimestamp(); //取到时间戳 String family = Bytes.toString(CellUtil.cloneFamily(cell)); //取到族列 String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); //取到修饰名 String value = Bytes.toString(CellUtil.cloneValue(cell)); //取到值 System.out.println("rowKey : " + rowKey + ", timestamp : " + timestamp + ", family : " + family + ", qualifier : " + qualifier + ", value : " + value); } } System.out.println(" \n\n*****************get取数据*****************:"); //get Get get = new Get(Bytes.toBytes("row2")); get.addColumn(Bytes.toBytes("familycolumn2"), Bytes.toBytes("sex")); Table t04 = con.getTable(tn); Result r = t04.get(get); List<Cell> cs = r.listCells(); for (Cell cell : cs) { String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); //取行键 long timestamp = cell.getTimestamp(); //取到时间戳 String family = Bytes.toString(CellUtil.cloneFamily(cell)); //取到族列 String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); //取到修饰名 String value = Bytes.toString(CellUtil.cloneValue(cell)); //取到值 System.out.println("rowKey : " + rowKey + ", timestamp : " + timestamp + ", family : " + family + ", qualifier : " + qualifier + ", value : " + value); } //删除数据 System.out.println(" \n\n*****************delete删除数据*****************:"); Delete delete = new Delete(Bytes.toBytes("row2")); delete.addColumn(Bytes.toBytes("familycolumn2"), Bytes.toBytes("age")); Table t05 = con.getTable(tn); t05.delete(delete); System.out.println(" \n\n*****************delete删除数据后*****************:"); //scan scan = new Scan(); table03 = con.getTable(tn); //获得表对象 rs = table03.getScanner(scan); for (Result result : rs) { cs = result.listCells(); for (Cell cell : cs) { String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); //取行键 long timestamp = cell.getTimestamp(); //取到时间戳 String family = Bytes.toString(CellUtil.cloneFamily(cell)); //取到族列 String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); //取到修饰名 String value = Bytes.toString(CellUtil.cloneValue(cell)); //取到值 System.out.println("rowKey : " + rowKey + ", timestamp : " + timestamp + ", family : " + family + ", qualifier : " + qualifier + ", value : " + value); } } } catch (IOException e) { e.printStackTrace(); } //4.关闭 try { if (admin != null){ admin.close(); } if(con != null){ con.close(); } } catch (IOException e) { e.printStackTrace(); } } }
//运行结果:
----> 表存在, 删除表..... ----> 删除表成功.... 创建表成功.... rowKey : row1, timestamp : 1463486961279, family : familycolumn1, qualifier : name, value : Berg rowKey : row2, timestamp : 1463486961289, family : familycolumn2, qualifier : age, value : 22 rowKey : row2, timestamp : 1463486961289, family : familycolumn2, qualifier : sex, value : male rowKey : row3, timestamp : 1463486961289, family : familycolumn1, qualifier : sex, value : female *****************get取数据*****************: rowKey : row2, timestamp : 1463486961289, family : familycolumn2, qualifier : sex, value : male *****************delete删除数据*****************: *****************delete删除数据后*****************: rowKey : row1, timestamp : 1463486961279, family : familycolumn1, qualifier : name, value : Berg rowKey : row2, timestamp : 1463486961289, family : familycolumn2, qualifier : sex, value : male rowKey : row3, timestamp : 1463486961289, family : familycolumn1, qualifier : sex, value : female
关于“HBase基本API操作之CRUD的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。