《使用Phoenix将SQL代码移植至HBase - 数据库服务器 - 最新IT资讯_电脑知识大全_网络安全教程 -.docx》由会员分享,可在线阅读,更多相关《使用Phoenix将SQL代码移植至HBase - 数据库服务器 - 最新IT资讯_电脑知识大全_网络安全教程 -.docx(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、使用Phoenix将SQL代码移植至HBase-数据库服务器-最新IT资讯_电脑知识大全_网络安全教程-次元立方网importorg.apache.hadoop.hbase.CellUtil;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache.hadoop.hbase.HTableDescriptor;importorg.apache.hadoop.hbase.client.Delete;importorg.apache
2、.hadoop.hbase.client.Get;importorg.apache.hadoop.hbase.client.HBaseAdmin;importorg.apache.hadoop.hbase.client.HConnection;importorg.apache.hadoop.hbase.client.HConnectionManager;importorg.apache.hadoop.hbase.client.HTableInterface;importorg.apache.hadoop.hbase.client.Put;importorg.apache.hadoop.hbas
3、e.client.Result;importorg.apache.hadoop.hbase.client.ResultScanner;importorg.apache.hadoop.hbase.client.Scan;importorg.apache.hadoop.hbase.util.Bytes;*authorwxbhbase的基本操作方法publicclassHBaseSamplepublicstaticStringhbase_svr_ip=192.168.1.104,192.168.1.105,192.168.1.106;publicstaticStringhbase_svr_port=
4、2181;publicstaticStringhbase_svr_hostname=Node4,Node5,Node6;privateHConnectionconnection=null;Configurationconfig=null;*构造函数,构造一个HBaseSample对象,必须在最后调用close方法来关闭所有的连接,释放所有的资源publicHBaseSample()config=HBaseConfiguration.create();config.set(hbase.zookeeper.quorum,hbase_svr_hostname);config.set(hbase.zo
5、okeeper.property.clientPort,hbase_svr_port);/System.out.println(config.get(hbase.zookeeper.quorum);/System.out.println(config.get(hbase.zookeeper.property.clientPort);tryconnection=HConnectionManager.createConnection(config);catch(IOExceptione)e.printStackTrace();*释放资源publicvoidclose()tryif(null!=co
6、nnection)connection.close();catch(IOExceptione)e.printStackTrace();*创立表格*paramtableName*paramcolumnFarilypublicvoidcreateTable(finalStringtableName,StringcolumnFarily)if(null!=config)System.out.println(begincreatetable.);HBaseAdminadmin=null;tryadmin=newHBaseAdmin(config);if(admin.tableExists(tableN
7、ame)System.out.println(tableName+isalreadyexist!);elseHTableDescriptortableDesc=newHTableDescriptor(tableName);tableDesc.addFamily(newHColumnDescriptor(columnFarily);admin.createTable(tableDesc);System.out.println(tableDesc.toString()+hasbeencreated.);admin.close();catch(IOExceptione)e.printStackTra
8、ce();elseSystem.out.println(hbasecouldnotconnected!);*向指定表格中添加一行数据*paramtable*paramkey*paramfamily*paramcol*paramdataIn*returnpublicbooleanaddOneRecord(Stringtable,Stringkey,Stringfamily,Stringcol,bytedataIn)if(null!=connection)tryHTableInterfacetb=connection.getTable(table);Putput=newPut(key.getByt
9、es();put.add(family.getBytes(),col.getBytes(),dataIn);tb.put(put);System.out.println(putdatakey=+key);returntrue;catch(IOExceptione)System.out.println(putdatafailed.);returnfalse;elseSystem.out.println(hbasecouldnotconnected!);returnfalse;*得到hbase中所有的表*returnpublicListStringgetAllTables()ListStringt
10、ables=null;if(connection!=null)tryHTableDescriptorallTable=connection.listTables();if(allTable.length0)tables=newArrayListStringfor(HTableDescriptorhTableDescriptor:allTable)tables.add(hTableDescriptor.getNameAsString();System.out.println(hTableDescriptor.getNameAsString();catch(IOExceptione)e.print
11、StackTrace();elseSystem.out.println(hbasecouldnotconnected!);returntables;publicbytegetValueWithKey(StringtableName,StringrowKey,Stringfamily,Stringqualifier)byterel=null;if(null!=connection)tryHTableInterfacetable=connection.getTable(tableName);Getget=newGet(rowKey.getBytes();get.addColumn(Bytes.to
12、Bytes(family),Bytes.toBytes(qualifier);Resultresult=table.get(get);if(!result.isEmpty()rel=result.getValue(Bytes.toBytes(family),Bytes.toBytes(qualifier);catch(IOExceptione)e.printStackTrace();elseSystem.out.println(hbasecouldnotconnected!);returnrel;*从表中删除一行*paramtableName*paramrowKeypublicvoiddele
13、teWithKey(StringtableName,StringrowKey)if(null!=connection)tryHTableInterfacetable=connection.getTable(tableName);Deletedelete=newDelete(rowKey.getBytes();table.delete(delete);catch(IOExceptione)e.printStackTrace();elseSystem.out.println(hbasecouldnotconnected!);*得到一个表中的所有元素*paramtableNamepublicvoid
14、getAllData(StringtableName)if(null!=connection)tryHTableInterfacetable=connection.getTable(tableName);Scanscan=newScan();ResultScannerrs=table.getScanner(scan);for(Resultr:rs)Cellcells=r.rawCells();System.out.println(Thisrowhave+cells.length+cells:);for(Cellcell:cells)Stringrow=Bytes.toString(CellUt
15、il.cloneRow(cell);Stringfamily=Bytes.toString(CellUtil.cloneFamily(cell);Stringqualifier=Bytes.toString(CellUtil.cloneQualifier(cell);Stringvalue=Bytes.toString(CellUtil.cloneValue(cell);System.out.println(String.format(%s:%s:%s:%s,row,family,qualifier,value);catch(IOExceptione)e.printStackTrace();e
16、lseSystem.out.println(hbasecouldnotconnected!);publicvoiddeleteTable(StringtableName)if(null!=config)System.out.println(begindeletetable.);HBaseAdminadmin=null;tryadmin=newHBaseAdmin(config);if(!admin.tableExists(tableName)System.out.println(tableName+isnotexist!);elseadmin.disableTable(tableName);a
17、dmin.deleteTable(tableName);System.out.println(tableName+hasbeendeleted.);admin.close();catch(IOExceptione)e.printStackTrace();elseSystem.out.println(hbasecouldnotconnected!);*paramargspublicstaticvoidmain(Stringargs)HBaseSamplesample=newHBaseSample();/1.createtableandinsertdatasample.createTable(st
18、udent,fam1);sample.addOneRecord(student,id1,fam1,name,Jack.getBytes();sample.addOneRecord(student,id1,fam1,address,HZ.getBytes();/2.listtablesample.getAllTables();/3.getValuebytevalue=sample.getValueWithKey(student,id1,fam1,address);System.out.println(value=+Bytes.toString(value);/4.addOneRecordanddelete/sample.addOneRecord(student,id2,fam1,name,wxb.getBytes();/sample.addOneRecord(student,id2,fam1,address,/here.getBytes();/sample.deleteWithKey(student,id2);/5.scantablesample.getAllData(student);/6.deletetable/sample.deleteTable(student);sample.close();