Cassandra将Token转换为长整型。

huangapple go评论65阅读模式
英文:

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))!我怎样才能将它转换为longString以便在查询中使用:

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) &gt;= -9220405531215751472 AND token(_id) &lt; -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 = &quot;SELECT count(*) FROM mytable WHERE token(_id) &gt;= &quot;+start+&quot; AND token(_id) &lt; &quot;+end+&quot; AND state=true ALLOW FILTERING;&quot;;
    System.out.println(i +&quot;:&quot; + sql);
}

huangapple
  • 本文由 发表于 2020年9月1日 14:15:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63682339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定