Cassandra访问器/映射器未映射udt字段 [英] Cassandra Accessor / Mapper not mapping udt field

查看:91
本文介绍了Cassandra访问器/映射器未映射udt字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用datastax cassandra 3.1.2。我在cassandra中创建了下表并插入了一条记录。

I am using datastax cassandra 3.1.2. I have created the following table in cassandra and inserted a record.

CREATE TYPE memory ( capacity text );
create TABLE laptop ( id uuid primary key, model text, ram frozen<memory> );
select * from laptop ;

 id                                   | model         | ram
--------------------------------------+---------------+-------------------
 e55cba2b-0847-40d5-ad56-ae97e793dc3e | Dell Latitude | {capacity: '8gb'}

当我尝试从冻结类型的内存中获取容量字段时Java使用Cassandra Accessor和以下代码:

When I am trying to fetch the capacity field from frozen type memory in Java using Cassandra Accessor with the below code:

this.cluster = Cluster.builder().addContactPoint(node).withPort(port).build();
session = cluster.connect();
MappingManager manager = new MappingManager(session);
LaptopAccessor laptopAccessor = manager.createAccessor(LaptopAccessor.class);
Result<Laptop> cp = laptopAccessor.getOne(UUID.fromString("e55cba2b-0847-40d5-ad56-ae97e793dc3e"));
System.out.println(cp.one());

它给ram数据点本身为空。

It is giving ram datapoint itself is null.

id = null model = null ram = null

我期望映射器在映射时会创建ram实例,然后将容量字段映射到其中,并返回Laptop Bean。

I was expecting that the mapper would create ram instance while mapping and map capacity field into it and return the Laptop bean.

我具有以下访问器接口:

I have the following Accessor interface:

@Accessor
interface LaptopAccessor {
   @Query("SELECT ram.capacity FROM user_info.laptop where id=?")
   Result<Laptop> getOne(UUID id);
}

上表中有以下Java Bean。

I have the following java beans for the above table.

@Table(keyspace = "user_info", name = "laptop")
public class Laptop {

    private UUID id;
    private String model;
    private Memory ram;

    @PartitionKey
    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }


    public String getModel() {
        return model;
    }


    public void setModel(String model) {
        this.model = model;
    }

    @Frozen
    public Memory getRam() {
        return ram;
    }


    public void setRam(Memory ram) {
        this.ram = ram;
    }

    @Override
    public String toString() {
        return "id = " + id + " model = " + model + " ram = " + ram;
    }

}

@UDT(keyspace = "user_info", name = "memory")
public class Memory {

    private String capacity;

    @Field
    public String getCapacity() {
        return capacity;
    }


    public void setCapacity(String capacity) {
        this.capacity = capacity;
    }

    @Override
    public String toString() {
        return "capacity = " + capacity ;
    }

}

当我进行更改时,代码可以正常工作查询以检索整个ram UDT。有人可以告诉我为什么在查询的udt中选择某些字段时映射器为什么不起作用吗?

The code works fine when I change the query to retrieve entire ram UDT. Could somebody please tell that why the mapper doesn't work when I select some field from the udt in the query?

cassandra不支持吗?任何获取UDT字段的解决方法?

Doesn't cassandra support this? Any workaround to fetch the UDT fields?

推荐答案

我认为问题在于访问器的返回类型:

I think the issue is the return type on your accessor:

@Accessor
interface LaptopAccessor {
   @Query("SELECT ram.capacity FROM user_info.laptop where id=?")
   Result<Laptop> getOne(UUID id);
}

由于您的查询仅选择 ram.capacity ,所有驱动程序返回的都是带有单个列的行,该行是 String ,名称为 ram。 不会映射到 Laptop 中的任何字段。

Since your query is only selecting ram.capacity, all the driver is getting back is a Row with a single column that is a String with a name of ram.capacity which does not map to any field in Laptop.

就像您想要的只是与该查询匹配的1行一样,您可以将您的访问器更改为:

Instead, since it looks like all you want is the 1 row matching that query, you could change your Accessor to:

@Accessor
interface LaptopAccessor {
   @Query("SELECT ram.capacity FROM user_info.laptop where id=?")
   ResultSet getOne(UUID id);
}

访问器现在返回 ResultSet ,您可以为其调用 one()。getString(0)以恢复容量。如果您不想直接处理 ResultSet ,但效果很好,那不是理想的选择。

The accessor now returns a ResultSet for which you can call one().getString(0) to get the capacity back. It's not ideal if you don't want to deal with ResultSet directly, but works well.

您不应是否真的需要整个 Laptop 对象,因为您所要求的只是UDT的字段?

You shouldn't really need the whole Laptop object anyways since all you are requesting is a field of a UDT right?

这篇关于Cassandra访问器/映射器未映射udt字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆