今天作了一個跨地區機房的壓測小程序,主要的思路就是基於事先準備好的rowkey文件,利用多線程模擬併發的rowkey查詢,能夠實現併發數的自由控制。主要是整個流程下來,遇到了點打包的坑,因此特地記錄下。java
rowkey文件的準備就不說了。首先是HbaseClient的查詢接口,因爲建立鏈接的代價很重,所以這裏採用HBase的ConnectionFactory工廠:node
static { try { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.zookeeper.quorum", "此處不可描述"); connection = ConnectionFactory.createConnection(conf); } catch (IOException e) { e.printStackTrace(); } } private static Table getTable(String table) throws IOException { return connection.getTable(TableName.valueOf(table)); }
查詢的時候直接使用get Api便可:apache
try (Table tab = getTable(table)) { Get get = new Get(Bytes.toBytes(key)); Cell cell = tab.get(get).getColumnLatestCell(COLUMN_FAMILY, Bytes.toBytes(field)); column = Bytes.toString(CellUtil.cloneValue(cell)); } catch (Exception e) { logger.error("查詢請求出錯:" + e.getMessage()); }
爲了模擬併發,我這邊直接使用了Fixed線程池,而且基於java8的lambda表達式建立線程池:小程序
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(100); for(String line : lines){ fixedThreadPool.execute(() -> { Long start = System.currentTimeMillis(); // ... todo ... 我這裏只想統計一下平均的訪問時間,因此就簡單的作減法就好了 Long end = System.currentTimeMillis(); System.out.println(end-start); }); }
總體的項目結構大體以下:
api
觀察文件MANIFEST.MF
能夠看到裏面包含的內容:服務器
Manifest-Version: 1.0 Class-Path: commons-beanutils-core-1.8.0.jar netty-all-4.0.23.Final.ja r hadoop-auth-2.5.1.jar snappy-java-1.0.4.1.jar protobuf-java-2.5.0.j ar jcodings-1.0.8.jar hadoop-yarn-common-2.5.1.jar httpclient-4.2.5.j ar commons-math3-3.1.1.jar commons-lang-2.6.jar findbugs-annotations- 1.3.9-1.jar jaxb-api-2.2.2.jar slf4j-api-1.6.1.jar commons-el-1.0.jar commons-beanutils-1.7.0.jar commons-collections-3.2.2.jar commons-ht tpclient-3.1.jar commons-io-2.4.jar avro-1.7.4.jar hamcrest-core-1.3. jar hbase-client-1.3.1.jar slf4j-log4j12-1.6.1.jar commons-logging-1. 2.jar hadoop-yarn-api-2.5.1.jar hbase-protocol-1.3.1.jar netty-3.6.2. Final.jar commons-configuration-1.6.jar hadoop-annotations-2.5.1.jar jackson-core-asl-1.9.13.jar paranamer-2.3.jar junit-4.12.jar metrics- core-2.2.0.jar jsr305-1.3.9.jar stax-api-1.0-2.jar hadoop-common-2.5. 1.jar commons-compress-1.4.1.jar apacheds-i18n-2.0.0-M15.jar api-asn1 -api-1.0.0-M20.jar jackson-mapper-asl-1.9.13.jar commons-codec-1.9.ja r xz-1.0.jar htrace-core-3.1.0-incubating.jar activation-1.1.jar hado op-mapreduce-client-core-2.5.1.jar commons-net-3.1.jar commons-digest er-1.8.jar hbase-annotations-1.3.1.jar jsch-0.1.42.jar commons-cli-1. 2.jar xmlenc-0.52.jar httpcore-4.2.4.jar joni-2.1.2.jar api-util-1.0. 0-M20.jar apacheds-kerberos-codec-2.0.0-M15.jar log4j-1.2.17.jar jett y-util-6.1.26.jar guava-12.0.1.jar zookeeper-3.4.6.jar hbase-common-1 .3.1.jar Main-Class: Test
咱們須要的文件就都保存在/project_home/out目錄下了,多線程
首先進入對應的out目錄,執行下面的命令:併發
tar -cvf hbase_test.tar hbase_test_jar
使用scp命令拷貝到遠程服務器:app
scp hbase_test.tar xingoo@hnode10:/home/xingoo/
登陸到遠程服務器,解壓:ide
tar -xvf hbase_test.tar
進入對應的目錄直接執行jar包:
java -jar hbase-test.jar
結果200ms還能夠接受吧...