static byte[] rowKeyTemplate(final TSDB tsdb, final String metric,
final Map<String, String> tags) {
final short metric_width = tsdb.metrics.width();
final short tag_name_width = tsdb.tag_names.width();
final short tag_value_width = tsdb.tag_values.width();
final short num_tags = (short) tags.size();
int row_size = (Const.SALT_WIDTH() + metric_width + Const.TIMESTAMP_BYTES
+ tag_name_width * num_tags + tag_value_width * num_tags);
final byte[] row = new byte[row_size];
short pos = (short) Const.SALT_WIDTH();
copyInRowKey(row, pos,
(tsdb.config.auto_metric() ? tsdb.metrics.getOrCreateId(metric)
: tsdb.metrics.getId(metric)));
pos += metric_width;
pos += Const.TIMESTAMP_BYTES;
for (final byte[] tag : Tags.resolveOrCreateAll(tsdb, tags)) {
copyInRowKey(row, pos, tag);
pos += tag.length;
}
return row;
}OpenTSDB是通过metric和tags生成rowkey的。以下述提交的json请求为例。
{
"metric": "power",
"timestamp": 1346233200,
"value": 26.76,
"tags": {
"deviceId": "1",
"localtionId": "2"
}
}首先计算rowkey的长度
int row_size =(Const.SALT_WIDTH()+ metric_width + Const.TIMESTAMP_BYTES + tag_name_width * num_tags + tag_value_width * num_tags);加盐长度为0,并不往rowkey中加盐。
Const.SALT_WIDTH()metric 长度
metric_width 时间戳
Const.TIMESTAMP_BYTEStags的数量
tag_name_width * num_tags + tag_value_width * num_tags);上述例子的长度是19,计算如下:
0 + 3 + 4 + 3*2 + 3*2 = 19
之后便向rowkey数组中添加数据。
首先metric生成的ID是0 0 2
之后tags生成的ID是
0 0 2 0 0 2 0 0 3 0 0 3
最终未添加的时间戳的rowkey为
0 0 2 0 0 0 0 0 0 2 0 0 2 0 0 3 0 0 3
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69946831/viewspace-3126778/ ,如需转载,请注明出处,否则将追究法律责任。