找不到请求操作的编解码器[decimal < ->java.lang.Double]

pes8fvy9  于 2023-01-01  发布在  Java
关注(0)|答案(2)|浏览(169)

我在Cassandra数据库中有十进制值,如何获得该值

cluster = Cluster.builder().addContactPoint("dev").withPort(9042).build();
   cluster.getConfiguration().getCodecRegistry().register(InstantCodec.decimal());
    Select select = QueryBuilder.select().from("app");
    session = cluster.connect(keyspace);

    ResultSet resultSet = session.execute(select);

    Map<String,Map<String,Long>>map = new HashMap();

   resultSet.forEach(x ->map.put(x.getString("signal_name"),x.getMap("config",String.class,Long.class)));
jgwigjjp

jgwigjjp1#

@sathish,decimal CQL数据类型Map到java类型中的BigDecimal。有关详细信息,请参阅CQL到Java类型Map文档。
话虽如此,下面是我利用DataStax Astra DB来演示这一点的示例:

    • 表格结构**:
CREATE TABLE ks.sodecimal (
    i int PRIMARY KEY,
    j decimal
);
    • 数据**:
token@cqlsh:ks> SELECT * FROM sodecimal ;

 i | j
---+-------
 1 | 0.124

(1 rows)
...
System.out.println("-----------");
ResultSet rs_dec = session.execute("SELECT j FROM ks.sodecimal WHERE i = 1");
BigDecimal bd = rs_dec.one().getBigDecimal("j");
System.out.println("Decimal value: " + bd.toString());
System.out.println("-----------");
...
    • 输出**:
-----------
Decimal value: 0.124
-----------

希望这能有所帮助!
👉 Please support the Apache Cassandra community by hovering over the cassandra tag then click on the Watch tag button. 🙏 Thanks!

6ju8rftf

6ju8rftf2#

你不需要去修改编解码器,这个错误是因为Cassandra DECIMAL类型没有Map到Java Double
假设我有一个数值类型的表,如下所示:

CREATE TABLE numerics (
    num INT PRIMARY KEY,
    dec DECIMAL,
    doub DOUBLE,
    flt FLOAT);

使用查询生成器,我可以查询和Map这些类型,如下所示:

Select query = QueryBuilder.selectFrom("stackoverflow", "numerics")
    .column("num")
    .column("dec")
    .column("doub")
    .column("flt")
    .whereColumn("num")
        .isEqualTo(QueryBuilder.literal(1));
SimpleStatement statement = query.build();

ResultSet rs = session.execute(statement);
Row row = rs.one();
          
System.out.println("num(int) = " + row.getInt("num"));
System.out.println("decimal = " + row.getBigDecimal("dec"));
System.out.println("double = " + row.getDouble("doub"));
System.out.println("float = " + row.getFloat("flt"));
    • TL;医生;**

一个Cassandra DECIMAL类型Map到一个BigDecimal,所以尝试使用它。
Github回购:https://github.com/aar0np/SelectNumerics

相关问题