列式存储使用NativeJAVAAPI访问

使用NativeJavaAPI访问

NativeJavaAPI是最常规高效的访问方式,使用方法请参考

HbaseUtil.java

packagecom..common;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.;importorg.apache.hadoop.hbase.client.;importorg.apache.hadoop.hbase.util.Bytes;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;publicclassHbaseUtil{/获取配置@return/publicstaticConfigurationgetConfiguration(){Configurationconf=null;try{conf=HBaseConfiguration.create();conf.set(&quot;hbase.zookeeper.quorum&quot;,&quot;116.196.81.217&quot;);conf.set(&quot;hbase.zookeeper.property.clientPort&quot;,&quot;2181&quot;);//Wecanalsoreadtheconfigfromfilesbelow//configuration.addResource(newPath(&quot;/etc/hbase/conf/hbase-site.xml&quot;));//configuration.addResource(newPath(&quot;/etc/hadoop/conf/core-site.xml&quot;));}catch(Exceptione){e.printStackTrace();}returnconf;}/创建连接@paramconf@return/publicstaticConnectiongetConnection(Configurationconf){//CreateConnectionandgetAdminholdoffConnectionconn=null;try{conn=ConnectionFactory.createConnection(conf);}catch(Exceptione){e.printStackTrace();}returnconn;}/获取admin@paramconn@return/publicstaticAdmingetAdmin(Connectionconn){Adminadmin=null;try{admin=conn.getAdmin();}catch(Exceptione){e.printStackTrace();}returnadmin;}/关闭连接@paramconn@paramadmin/publicstaticvoidclose(Connectionconn,Adminadmin){try{if(admin!=null){admin.close();}if(conn!=null}}catch(Exceptione2){e2.printStackTrace();}}/创建NameSpace@paramnameSpace/publicstaticvoidcreateNameSpace(StringnameSpace){System.out.println(&quot;CreatingNamespace:&quot;+nameSpace);Configurationconf=getConfiguration();Connectionconn=getConnection(conf);Adminadmin=getAdmin(conn);try{NamespaceDescriptornsd=NamespaceDescriptor.create(nameSpace).build();admin.createNamespace(nsd);System.out.println(&quot;NamespaceCreated:&quot;+nameSpace);}catch(Exceptione){e.printStackTrace();}finally{close(conn,admin);}}/NameSpace列表/publicstaticvoidlistNameSpace(){Configurationconf=getConfiguration();Connectionconn=getConnection(conf);Adminadmin=getAdmin(conn);try{NamespaceDescriptor[]nameSpaceList=admin.listNamespaceDescriptors();System.out.println(&quot;ListofNamespaceavailableintheHBase&quot;);System.out.println(&quot;___________________________________________&quot;);for(NamespaceDescriptornameSpace:nameSpaceList){System.out.println(nameSpace);}}catch(Exceptione){e.printStackTrace();}finally{close(conn,admin);}}/创建表@paramtableName@paramcols/publicstaticvoidcreateTable(StringtableName,String[]cols){System.out.println(&quot;CreatingTable:&quot;+tableName);Configurationconf=getConfiguration();Connectionconn=getConnection(conf);Adminadmin=getAdmin(conn);try{TableNametName=TableName.valueOf(tableName);if(admin.tableExists(tName)){System.out.println(tableName+&quot;exists.&quot;);}else{HTableDescriptorhTableDesc=newHTableDescriptor(tName);for(Stringcol:cols){HColumnDescriptorhColumnDesc=newHColumnDescriptor(col);hTableDesc.addFamily(hColumnDesc);}admin.createTable(hTableDesc);}System.out.println(&quot;TableCreated:&quot;+tableName);}catch(Exceptione){e.printStackTrace();}finally{close(conn,admin);}}/删除表@paramtableName/publicstaticvoiddeleteTable(StringtableName){System.out.println(&quot;DeleteTable:&quot;+tableName);Configurationconf=getConfiguration();Connectionconn=getConnection(conf);Adminadmin=getAdmin(conn);try{TableNametName=TableName.valueOf(tableName);if(admin.tableExists(tName)){admin.disableTable(tName);admin.deleteTable(tName);}else{System.out.println(tableName+&quot;notexists.&quot;);}System.out.println(&quot;TableDeleted:&quot;+tableName);}catch(Exceptione){e.printStackTrace();}finally{close(conn,admin);}}/列出所有表/publicstaticvoidlistTables(){Configurationconf=getConfiguration();Connectionconn=getConnection(conf);Adminadmin=getAdmin(conn);try{TableName[]tableList=admin.listTableNames();System.out.println(&quot;ListofTablesavailableintheHBase&quot;);System.out.println(&quot;___________________________________________&quot;);for(TableNametable:tableList){System.out.println(table);}}catch(Exceptione){e.printStackTrace();}finally{close(conn,admin);}}/删除数据@paramtableName@paramrowKey@paramcolFamily@paramcol/publicstaticvoiddelete(StringtableName,StringrowKey,StringcolFamily,Stringcol){Configurationconf=getConfiguration();Connectionconn=getConnection(conf);Adminadmin=getAdmin(conn);//InstantiatingHTableclasstry{Tabletable=conn.getTable(TableName.valueOf(tableName));Deletedel=newDelete(Bytes.toBytes(rowKey));if(colFamily!=null){del.addFamily(Bytes.toBytes(colFamily));}if(colFamily!=null}/批量删除List<delete>deleteList=newArrayList<delete>();deleteList.add(delete);table.delete(deleteList);/table.delete(del);table.close();}catch(IOExceptione){e.printStackTrace();}finally{close(conn,admin);}}/删除数据@paramtableName@paramrowKey/publicstaticvoiddelete(StringtableName,StringrowKey){delete(tableName,rowKey,null,null);}/批量删除@paramtableName@paramrows/publicstaticvoiddelete(StringtableName,String[]rows){Configurationconf=getConfiguration();Connectionconn=getConnection(conf);Adminadmin=getAdmin(conn);//InstantiatingHTableclasstry{Tabletable=conn.getTable(TableName.valueOf(tableName));List<delete>list=newArrayList<delete>();for(Stringrow:rows){Deleted=newDelete(row.getBytes());list.add(d);}if(list.size()&gt;0){table.delete(list);}table.close();}catch(IOExceptione){e.printStackTrace();}finally{close(conn,admin);}}/写入数据@paramtableName@paramrowKey@paramcolFamily@paramcol@paramvalue/publicstaticvoidput(StringtableName,StringrowKey,StringcolFamily,Stringcol,Stringvalue){Configurationconf=getConfiguration();Connectionconn=getConnection(conf);Adminadmin=getAdmin(conn);//InstantiatingHTableclasstry{Tabletable=conn.getTable(TableName.valueOf(tableName));Putput=newPut(Bytes.toBytes(rowKey));put.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col),Bytes.toBytes(value));table.put(put);/批量插入List<put>putList=newArrayList<put>();puts.add(put);table.put(putList);///closingHTabletable.close();}catch(IOExceptione){e.printStackTrace();}finally{close(conn,admin);}}/批量写入@paramtableName@paramputList@return/publicstaticbooleanput(StringtableName,List<put>putList){Configurationconf=getConfiguration();Connectionconn=getConnection(conf);Adminadmin=getAdmin(conn);try{Tabletable=conn.getTable(TableName.valueOf(tableName));table.put(putList);}catch(IOExceptione){e.printStackTrace();}finally{close(conn,admin);}returntrue;}/根据RowKey获取数据@paramtableName表名称@paramrowKeyRowKey名称@paramcolFamily列族名称@paramcol列名称@throwsIOException/publicstaticvoidget(StringtableName,StringrowKey,StringcolFamily,Stringcol){Configurationconf=getConfiguration();Connectionconn=getConnection(conf);Adminadmin=getAdmin(conn);try{Tabletable=conn.getTable(TableName.valueOf(tableName));Getget=newGet(Bytes.toBytes(rowKey));if(colFamily!=null){get.addFamily(Bytes.toBytes(colFamily));}if(colFamily!=null}Resultresult=table.get(get);showCell(result);table.close();}catch(IOExceptione){e.printStackTrace();}finally{close(conn,admin);}}/获取数据@paramtableName@paramrowKey@paramcolFamily/publicstaticvoidget(StringtableName,StringrowKey,StringcolFamily){get(tableName,rowKey,colFamily,null);}/获取数据@paramtableName@paramrowKey/publicstaticvoidget(StringtableName,StringrowKey){get(tableName,rowKey,null);}/批量获取@paramtableName@paramrows/publicstaticvoidget(StringtableName,String[]rows){Configurationconf=getConfiguration();Connectionconn=getConnection(conf);Adminadmin=getAdmin(conn);Result[]results=null;try{Tabletable=conn.getTable(TableName.valueOf(tableName));List<get>gets=newArrayList<get>();for(Stringrow:rows){if(row!=null){gets.add(newGet(Bytes.toBytes(String.valueOf(row))));}else{thrownewRuntimeException(&quot;hbasehavenodata&quot;);}}if(gets.size()&gt;0){results=table.get(gets);}for(Resultresult:results){showCell(result);}table.close();}catch(IOExceptione){e.printStackTrace();}finally{close(conn,admin);}}/scan数据@paramtableName@paramcolFamily@paramcols/publicstaticvoidscan(StringtableName,StringcolFamily,String[]cols){Configurationconf=getConfiguration();Connectionconn=getConnection(conf);Adminadmin=getAdmin(conn);try{//InstantiatingHTableclassTabletable=conn.getTable(TableName.valueOf(tableName));Scanscan=newScan();//Scanningtherequiredcolumnsif(colFamily!=null){scan.addFamily(Bytes.toBytes(colFamily));}if(colFamily!=null}}//GettingthescanresultResultScannerscanner=table.getScanner(scan);//Readingvaluesfromscanresultfor(Resultresult=scanner.next();result!=null;result=scanner.next())showCell(result);//System.out.println(&quot;Foundrow:&quot;+result);//closingthescannerscanner.close();}catch(IOExceptione){e.printStackTrace();}finally{close(conn,admin);}}/scan@paramtableName@paramcolFamily/publicstaticvoidscan(StringtableName,StringcolFamily){scan(tableName,colFamily,null);}/scan@paramtableName/publicstaticvoidscan(StringtableName){scan(tableName,null);}/格式化输出@paramresult/publicstaticvoidshowCell(Resultresult){Cell[]cells=result.rawCells();for(Cellcell:cells){System.out.println(newString(CellUtil.cloneRow(cell))+&quot;&quot;+cell.getTimestamp()+&quot;&quot;+newString(CellUtil.cloneFamily(cell))+&quot;&quot;+newString(CellUtil.cloneQualifier(cell))+&quot;&quot;+newString(CellUtil.cloneValue(cell)));}}publicstaticvoidmain(Stringargs[]){//创建表Stringcf[]={&quot;cf&quot;};createTable(&quot;test5&quot;,cf);//列出所有表listTables();//添加数据put(&quot;test5&quot;,&quot;101&quot;,&quot;cf&quot;,&quot;name&quot;,&quot;zhang3&quot;);put(&quot;test5&quot;,&quot;101&quot;,&quot;cf&quot;,&quot;age&quot;,&quot;18&quot;);put(&quot;test5&quot;,&quot;102&quot;,&quot;cf&quot;,&quot;name&quot;,&quot;li4&quot;);put(&quot;test5&quot;,&quot;102&quot;,&quot;cf&quot;,&quot;age&quot;,&quot;20&quot;);//获取数据get(&quot;test5&quot;,&quot;101&quot;,&quot;cf&quot;,&quot;name&quot;);get(&quot;test5&quot;,&quot;101&quot;,&quot;cf&quot;,&quot;age&quot;);//System.out.println(&quot;-------------------------&quot;);//删除数据//delete(&quot;test5&quot;,&quot;101&quot;,&quot;cf&quot;,&quot;name&quot;);//get(&quot;test5&quot;,&quot;101&quot;,&quot;cf&quot;,&quot;name&quot;);//System.out.println(&quot;-------------------------&quot;);//get(&quot;test5&quot;,&quot;101&quot;,&quot;cf&quot;,&quot;age&quot;);//System.out.println(&quot;-------------------------&quot;);//扫描name列//String[]cols={&quot;name&quot;};//scan(&quot;test5&quot;,&quot;cf&quot;,cols);//System.out.println(&quot;-------------------------&quot;);//扫描name和age列//String[]cols1={&quot;name&quot;,&quot;age&quot;};//scan(&quot;test5&quot;,&quot;cf&quot;,cols1);//System.out.println(&quot;-------------------------&quot;);//扫描cf列族//scan(&quot;test5&quot;,&quot;cf&quot;);System.out.println(&quot;-------------------------&quot;);//扫描全表scan(&quot;test5&quot;);}}</get></get></put></put></put></delete></delete></delete></delete>