今天做了一個跨地區機房的壓測小程序,主要的思路就是基於事先准備好的rowkey文件,利用多線程模擬並發的rowkey查詢,可以實現並發數的自由控制。主要是整個流程下來,遇到了點打包的坑,所以特意記錄下。
編寫代碼
rowkey文件的准備就不說了。首先是HbaseClient的查詢接口,由於創建連接的代價很重,因此這里采用HBase的ConnectionFactory工廠:
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即可:
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);
});
}
基於Idea打包
整體的項目結構大致如下:
點擊project structure
點擊add-->jar-->from models with dependencies...
選擇對應的資源文件加入到打包路徑中
點擊build-->build artifacts-->build進行打包
觀察文件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命令拷貝到遠程服務器:
scp hbase_test.tar xingoo@hnode10:/home/xingoo/
登錄到遠程服務器,解壓:
tar -xvf hbase_test.tar
進入對應的目錄直接執行jar包:
java -jar hbase-test.jar
結果200ms還可以接受吧...