英文:
Cassandra convert Token to long
问题
我想在表上运行类似这样的查询:
SELECT count(*) FROM mytable WHERE token(_id) >= -9220405531215751472 AND token(_id) < -9215701564955960000 AND state=true ALLOW FILTERING;
我通过metadata.getTokenMap()
读取一系列的token,然后使用tokenMap.getTokenRanges()
。但是它会给我一个起始token和结束token,类型为Token Object(例如 Murmur3Token(9187337865070158013))!我怎样才能将它转换为long或String以便在查询中使用:
CqlSession session = builder.build();
Metadata metadata = session.getMetadata();
if (metadata.getTokenMap().isPresent()) {
TokenMap tokenMap = metadata.getTokenMap().get();
for(TokenRange range : tokenMap.getTokenRanges()) {
System.out.println(range.getStart()); // 输出 Murmur3Token(9187337865070158013)
}
}
英文:
I wanna run a query like this on a table:
SELECT count(*) FROM mytable WHERE token(_id) >= -9220405531215751472 AND token(_id) < -9215701564955960000 AND state=true ALLOW FILTERING;
I read range of tokens by metadata.getTokenMap()
and then tokenMap.getTokenRanges()
. But it gives me start and end token as Token Object (e.g. Murmur3Token(9187337865070158013)
)! How I can convert it to long or String to use in query:
CqlSession session = builder.build();
Metadata metadata = session.getMetadata();
if (metadata.getTokenMap().isPresent()) {
TokenMap tokenMap = metadata.getTokenMap().get();
for(TokenRange range : tokenMap.getTokenRanges()) {
System.out.println(range.getStart()); // output Murmur3Token(9187337865070158013)
}
}
答案1
得分: 0
我找到了如何进行转换!必须明确将其转换为 `Murmur3Token`,然后调用 `getValue` 方法。
long start, end;
String sql;
for (TokenRange range : tokenMap.getTokenRanges()) {
start = ((Murmur3Token) range.getStart()).getValue();
end = ((Murmur3Token) range.getEnd()).getValue();
sql = "SELECT count(*) FROM mytable WHERE token(_id) >= " + start + " AND token(_id) < " + end + " AND state=true ALLOW FILTERING;";
System.out.println(i + ":" + sql);
}
英文:
I found how I can convert it! It must be converted to Murmur3Token
explicitly and call getValue
method.
long start,end;
String sql;
for(TokenRange range : tokenMap.getTokenRanges()) {
start = ((Murmur3Token) range.getStart()).getValue();
end = ((Murmur3Token) range.getEnd()).getValue();
sql = "SELECT count(*) FROM mytable WHERE token(_id) >= "+start+" AND token(_id) < "+end+" AND state=true ALLOW FILTERING;";
System.out.println(i +":" + sql);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论