使用Phoenix将SQL代码移植至HBase - 数据库服务器 - 最新IT资讯_电脑知识大全_网络安全教程 -.docx
使用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.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.hbase.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="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.zookeeper.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!=connection)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(tableName)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.printStackTrace();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.getBytes();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()ListStringtables=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.printStackTrace();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.toBytes(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*paramrowKeypublicvoiddeleteWithKey(StringtableName,StringrowKey)if(null!=connection)tryHTableInterfacetable=connection.getTable(tableName);Deletedelete=newDelete(rowKey.getBytes();table.delete(delete);catch(IOExceptione)e.printStackTrace();elseSystem.out.println("hbasecouldnotconnected!");*得到一个表中的所有元素*paramtableNamepublicvoidgetAllData(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(CellUtil.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();elseSystem.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);admin.deleteTable(tableName);System.out.println(tableName+"hasbeendeleted.");admin.close();catch(IOExceptione)e.printStackTrace();elseSystem.out.println("hbasecouldnotconnected!");*paramargspublicstaticvoidmain(Stringargs)HBaseSamplesample=newHBaseSample();/1.createtableandinsertdatasample.createTable("student","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();