我在bigtable中有一个小表(大小为100Mb),有10个实例。当我尝试每1分钟扫描/获取一次时,呼叫的延迟超过300毫秒。如果我点击更频繁的呼叫,例如每秒一次,则延迟为50-60ms。我不确定如何通过低频呼叫提高性能。这是预期的行为。或者我做错了什么。
这是我的测试代码。我为两个大型表的hbase客户端连接创建了一个执行器。但是低频连接响应比进行更频繁呼叫的连接慢。
有什么建议吗?
package com.bids;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.util.Bytes;
import org.fusesource.jansi.AnsiConsole;
public class BTConnectTest {
public static void main(String[] args) throws IOException, InterruptedException {
Configuration hBaseConfig = HBaseConfiguration.create();
hBaseConfig.set("google.bigtable.project.id", "xxxxxxx");
hBaseConfig.set("google.bigtable.cluster.name", "hbase-test1");
hBaseConfig.set("google.bigtable.zone.name", "us-central1-b");
hBaseConfig.set("hbase.client.connection.impl", "com.google.cloud.bigtable.hbase1_1.BigtableConnection");
ExecutorService executor = Executors.newSingleThreadExecutor();
final Connection bigTableConnection1 = ConnectionFactory.createConnection(hBaseConfig, executor);
final Connection bigTableConnection2 = ConnectionFactory.createConnection(hBaseConfig, executor);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
long before = System.nanoTime();
try {
makeACall2Bigtable(bigTableConnection2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// bigTableConnection.close();
long after = System.nanoTime();
long diff = after - before;
System.out.println("\t\t\t\t\t\t connection: " + 1 + " diff: " + diff / (1000 * 1000));
}
}
});
t.start();
long sum = 0;
int n = 0;
while (true) {
if (n > 60) {
Thread.sleep(60000);
}
long before = System.nanoTime();
Connection bigTableConnection = bigTableConnection1;
int label = -1;
makeACall2Bigtable(bigTableConnection);
long after = System.nanoTime();
long diff = after - before;
n = n + 1;
sum += diff;
long avg = sum / (n * 1000 * 1000);
AnsiConsole a = new AnsiConsole();
System.out.println("connection: " + 0 + " diff: " + diff / (1000 * 1000) + " avg: " + avg);
}
// bigTableConnection.close();
}
private static void makeACall2Bigtable(Connection bigTableConnection) throws IOException {
Table table = bigTableConnection.getTable(TableName.valueOf("customer"));
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("101"));
scan.setStopRow(Bytes.toBytes("102"));
List<String> cols = new ArrayList<String>(3);
cols.add("name");
cols.add("age");
cols.add("weight");
String keyName = "id";
final String DEFAULT_COLFAM = "z";
for (String col : cols) {
scan.addColumn(Bytes.toBytes(DEFAULT_COLFAM), Bytes.toBytes(col));
}
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
Map<String, String> columnValueMap = new LinkedHashMap<String, String>();
for (String col : cols) {
if (result.containsColumn(Bytes.toBytes(DEFAULT_COLFAM), Bytes.toBytes(col))) {
columnValueMap.put(col, new String(CellUtil.cloneValue(
result.getColumnLatestCell(Bytes.toBytes(DEFAULT_COLFAM), Bytes.toBytes(col)))));
} else {
if (cols.contains(keyName)) {
columnValueMap.put(col, null);
}
}
}
}
resultScanner.close();
table.close();
}
}
答案 0 :(得分:4)
我希望有所帮助。
答案 1 :(得分:3)
如果你真的打算在生产中做低频请求,你可能希望运行一个后台线程,每隔几秒就会向你的表发一个随机请求。
Bigtable实际上针对频繁访问的大量数据进行了优化。一段时间内的第一个请求可能要求再次读入数据。定期请求将保持有效。