使用Java对Hbase操作总结及示例代码
前面已经给大家讲解过如何使用Hbase建表,以及基本的操作和一些常用shell命令,今天就给大家介绍下如何使用java对Hbase进行各种操作。
没印象的话可以再去浏览下:
Hbase入门教程,shell命令大全讲解
Java操作Hbase主要方法:
1.Configuration
在使用JavaAPI时,Client端需要知道HBase的配置环境,如存储地址,zookeeper等信息。
这些信息通过Configuration对象来封装,可通过如下代码构建该对象:
Configurationconfig=HBaseConfiguration.create();
在调用HBaseConfiguration.create()方法时,HBase首先会在classpath下查找hbase-site.xml文件,将里面的信息解析出来封装到Configuration对象中,如果hbase-site.xml文件不存在,则使用默认的hbase-core.xml文件。
2.HBaseAdmin
HBaseAdmin用于创建数据库表格,并管理表格的元数据信息,通过如下方法构建:
HBaseAdminadmin=newHBaseAdmin(config);
3.HTableDescriptor
在HTableDescriptor中,建立了一个表结构,HTableDescriptor封装表格对象,对表格的增删改查操作主要通过它来完成,构造方法如下:
HTableDescriptortable=newHTableDescriptor(TableName.valueOf(“表名”));
4.addFamily
addFamily用于建立表下的列簇,并存放到表结构,方法如下:
HColumnDescriptorbase=newHColumnDescriptor(“列簇名”);
table.addFamily(base);
代码如下:
首先建一个maven工程,导入依赖包导pom.xml
org.apache.hbase hbase-client 1.2.0 org.apache.hbase hbase-common 1.2.0 org.apache.hbase hbase-server 1.2.0 1、创建表操作
publicclassHBaseClient{ publicvoidcreateTable()throwsIOException{ //1.创建配置 Configurationconf=HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum","ip1"); //hbase主默认端口是60000 conf.set("hbase.master","ip1:60000"); //zookeeper客户端的端口号2181 conf.set("hbase.zookeeper.property.clientPort","2181"); //2.创建连接 Connectionconn=ConnectionFactory.createConnection(conf); //3.获得一个建表、删表的对象hbaseAdmin()是继承admin() Adminadmin=conn.getAdmin(); //4.创建表的描述信息 HTableDescriptorstudent=newHTableDescriptor(TableName.valueOf("表名")); //5.添加列簇 student.addFamily(newHColumnDescriptor("列簇名1")); student.addFamily(newHColumnDescriptor("列簇名2")); //6.调用API进行建表操作 admin.createTable(student); } }2、判断表是否存在
publicvoidisTableExists()throwsIOException{ //1.创建配置 Configurationconf=HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum","ip1"); conf.set("hbase.zookeeper.property.clientPort","2181"); //2.创建连接 Connectionconn=ConnectionFactory.createConnection(conf); //3.创建admin Adminadmin=conn.getAdmin(); //4.调用API进行判断表是否存在 System.out.println(admin.tableExists(TableName.valueOf("表名"))); }3、向表中插入数据
publicvoidputData2Table()throwsIOException{ //1.创建配置 Configurationconf=HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum","ip1"); conf.set("hbase.zookeeper.property.clientPort","2181"); //2.创建连接 Connectionconn=ConnectionFactory.createConnection(conf); //3.创建Table类 Tablestudent=conn.getTable(TableName.valueOf("表名")); //4.创建Put类 Putput=newPut(Bytes.toBytes("1001")); //5.向Put中添加列簇,列名,值注意:需要转化成字节数组 put.addColumn(Bytes.toBytes("列簇1"),Bytes.toBytes("列1"),Bytes.toBytes("zhangsan")); put.addColumn(Bytes.toBytes("列簇1"),Bytes.toBytes("列2"),Bytes.toBytes("female")); put.addColumn(Bytes.toBytes("列簇2"),Bytes.toBytes("列3"),Bytes.toBytes("math")); put.addColumn(Bytes.toBytes("列簇2"),Bytes.toBytes("列4"),Bytes.toBytes("89")); //6.调用API进行插入数据 student.put(put); }4、查看一条数据
publicvoidgetDataFromTable()throwsIOException{ //1.创建配置 Configurationconf=HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum","ip1"); conf.set("hbase.zookeeper.property.clientPort","2181"); //2.创建连接 Connectionconn=ConnectionFactory.createConnection(conf); //3.创建Table类 Tablestudent=conn.getTable(TableName.valueOf("表名")); //4.创建Get类 Getget=newGet(Bytes.toBytes("1001")); //5.调用API进行获取数据 Resultresult=student.get(get); //6.将返回的结果进行遍历输出 Cell[]cells=result.rawCells(); for(Cellcell:cells){ System.out.println("rowkey:"+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))); System.out.println("----------------"); } }5、删除表操作
publicvoiddropTable()throwsIOException{ //1.创建配置 Configurationconf=HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum","ip1"); conf.set("hbase.zookeeper.property.clientPort","2181"); //2.创建连接 Connectionconn=ConnectionFactory.createConnection(conf); //3.创建admin Adminadmin=conn.getAdmin(); //4.调用API禁用表 admin.disableTable(TableName.valueOf("表名")); //5.调用API删除表 admin.deleteTable(TableName.valueOf("表名")); } }6、删除hbase中的table里面的rowkey
publicstaticvoiddeleteRow(StringtableName,StringrowKey)throwsException{ HTablehTable=newHTable(configuration,tableName); Deletedelete=newDelete(rowKey.getBytes()); Listlist=newArrayList (); list.add(delete); hTable.delete(list); } 7、查询row=rowKey的数据
publicstaticvoidgetRow(StringtableName,StringrowKey)throwsException{ HTablehTable=newHTable(configuration,tableName); Getget=newGet(rowKey.getBytes()); Resultresult=hTable.get(get); for(KeyValuevalue:result.raw()){ System.out.println("cf:"+newString(value.getFamily())+newString(value.getQualifier())+"="+newString(value.getValue())); } }8、查询rowkey在startRow和endRow之间的数据,及rowkey的范围查询
Put、Delete与Get对象都是Row的子类,从该继承关系中我们就可以了解到Get、Delete与Pu对象本身就只能进行单行的操作,
HBase客户端还提供了一套能够进行全表扫描的API,方便用户能够快速对整张表进行扫描,以获取想要的结果—scan:publicstaticvoidgetBetweenRow(StringtableName,StringstartRow,StringstopRow)throwsException{ HTabletable=newHTable(configuration,tableName); Scanscan=newScan(); scan.addColumn("cf1".getBytes(),"colum1".getBytes()); scan.addColumn("cf1".getBytes(),"colum2".getBytes()); scan.addColumn("cf1".getBytes(),"colum3".getBytes()); scan.setStartRow(startRow.getBytes()); scan.setStopRow(stopRow.getBytes()); ResultScannerscanner=table.getScanner(scan); for(Resultresult:scanner){ for(KeyValuevalue:result.raw()){ System.out.println("cf:"+newString(value.getFamily())+newString(value.getQualifier())+"="+newString(value.getValue())); } } }9、批量写入
publicvoidputs(StringtableName,Map items){ if(items==null||items.isEmpty()){ LOG.error("[HBase]Addingnull/emptyitemmap!"); return; } intmaxSize=10000; Tabletable=null; try{ table=con.getTable(TableName.valueOf(tableName)); inteachSize=Math.min(maxSize,items.size()); List puts=newArrayList (eachSize); inthandled=0; for(Entry entry:items.entrySet()){ StringultimateRowKey=getHashedID(entry.getKey()); Objectvalue=entry.getValue(); if(ultimateRowKey==null||ultimateRowKey.isEmpty()){ LOG.error("[HBase]Addingnull/emptyhashedkey!Originalkeyis"+entry.getKey()); handled++; continue; } Putput=newPut(Bytes.toBytes(ultimateRowKey)); put.addColumn(Bytes.toBytes(familyName1),Bytes.toBytes("ab"),Bytes.toBytes(value.getAb())); put.addColumn(Bytes.toBytes(familyName1),Bytes.toBytes("dt"),Bytes.toBytes(value.getDt())); put.addColumn(Bytes.toBytes(familyName1),Bytes.toBytes("hb"),Bytes.toBytes(value.getHb())); Gsongson=newGson(); Stringvaluestr=gson.toJson(value); put.addColumn(Bytes.toBytes(familyName2),Bytes.toBytes("js"),Bytes.toBytes(valuestr)); puts.add(put); handled++; //每隔10000,写一次 if(handled==eachSize){ LOG.info("[HBase]Adding"+eachSize+"rows!"); table.put(puts); puts=newArrayList (eachSize); } } if(puts.size()>0) table.put(puts); }catch(IOExceptione){ LOG.error("[HBase]Errorwhileputtingdata"+e.getMessage()); }finally{ try{ if(table!=null) table.close(); }catch(IOExceptione){ LOG.error("[HBase]Errorwhileclosingtable"+e.getMessage()); } } } 到此这篇关于使用Java对Hbase操作总结及示例代码的文章就介绍到这了,更多相关Java操作hbase总结内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!