HBase基础几个关键点——你能答出多少?

本文探讨了HBase中Region与Scan的rowkey范围特性,并分析了HTablePool的线程安全性。此外,还提供了HBase数据读取及批量写入的具体实现方式,并针对存在的问题进行了深入讨论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Region的rowkey范围是开区间还是闭区间?描述一下

2. Scan的rowkey范围是开区间还是闭区间?描述一下

3. HTablePool线程安全么?如果线程安全,为什么?如果线程不安全,有什么影响?

4. 下面的代码有没有问题

public List<Message> getData(Date start, Dateend, Long to)

throws IOException {

HTableInterfacetable = tablePool.getTable(tableName);

List<Message>ret = null;

byte[] bytesTo = Bytes.toBytes(to);

byte[] startRow = Bytes.add(bytesTo,Bytes.toBytes(start.getTime()));

byte[] endRow = Bytes.add(bytesTo, Bytes.toBytes(end.getTime()));

byte[] family = "f".getBytes();

Scanscan = new Scan();

scan.setStartRow(startRow);

scan.setStopRow(endRow);

scan.setCaching(200);

scan.addFamily(family);

ResultScannerrs = null;

try {

rs= table.getScanner(scan);

Resultresult = rs.next();

while (result != null) {

if (ret == null) {

ret= new ArrayList<Message>();

}

Messagemsg = new Message();

msg.setTo(to);

long id = Bytes.toLong(result.getValue(family,"id".getBytes()));

long from = Bytes.toLong(result.getValue(family, "from".getBytes()));

Stringmessage = Bytes.toString(result.getValue(family,

"msg".getBytes()));

long time = Bytes.toLong(Arrays.copyOfRange(result.getRow(),8,

16));

msg.setId(id);

msg.setFrom(from);

msg.setMessage(message);

msg.setTime(new Date(time));

ret.add(msg);

result.close();

result= rs.next();

}

}finally {

table.close(); // important

}

return ret;

}

5. 请优化:

publicList<Put> getPut(String row, String… values) {

List<Put> puts = new ArrayList<Put>(values.length);

for(String value : values) {

Put put = new Put(row.getBytes());

put.add("family".getBytes(),"from".getBytes(), Bytes.toBytes(value));

puts.add(put);

}

return puts;

}

6. scan.setBatch(1)和scan.setCaching(2)的区别

7. 判断

a)put(List<Put> puts)比put(Put put)效率高。

b)HBase的底层是HDFS,有多副本,所以HBase不会丢失数据。

c)写数据一定将自动flush设置为true以提高性能。

d)如果我要存30天的数据,但列都一样,那我可以用30个version来保存。

e)为了提升处理能力,我们应该把一批Put,分成多线程put到HBase集群

f)变长字段组合成rowkey的时候,将字段之间用#隔开就可以了。

g)一个Put对应于一个KeyValue。

h)HBase中建两张表不如建两个family。

i)假设pool是HTablePool对象,可以HTable table=(HTable) pool.get(“table”)。

j)使用HTablePool缓存的table(HTableInterface对象),将autoFlush设置为false的时候,用table.close就会自动flushCommits()。

k)一批Put最多需要当前表的region个数个RPC请求

l)HTablePool实现了网络连接的复用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值