【问题标题】:Spring data Cassandra @Indexed annotationSpring数据Cassandra @Indexed注解
【发布时间】:2017-11-30 03:11:52
【问题描述】:

我正在处理 Spring 数据 Cassandra 在创建实体时,我正在为字段提供所需的注释,但是当我提供 @Indexed 注释以在架构中创建二级索引时,我无法查询索引属性给出允许过滤。有人可以告诉我如何在 Cassandra 中使用 Spring 数据注释创建二级索引

This is the sample cod that I am using creating a Sprind data Cassandra Entity.@Indexed annotation not creating a secondary index in Cassandra database

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.springframework.data.cassandra.mapping.CassandraType;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.Indexed;
import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;
import org.springframework.data.cassandra.mapping.UserDefinedType;
import com.datastax.driver.core.DataType;
import com.suntecgroup.xelerate.platform.demo.config.SpringContainer;
import com.suntecgroup.xelerate.platform.demo.store.CustomerCassandraStore;
import com.suntecgroup.xelerate.platform.demo.store.impl.CustomerCassandraStoreImpl;

@Table(value="CustomerCassandra")
public class CustomerCassandra extends CassandraEntity<Integer> {

    @PrimaryKey(value="cust_id")
    private Integer custId;
    @Indexed(value="name")
    private String name;
    @Indexed(value="email")
    private String email;
    @Column(value="domain")
    private String domain;
    @Column(value="category")
    private String category;
    @Column(value="created_at")
    private Date createdAt;
    @CassandraType(type=DataType.Name.LIST,typeArguments = { DataType.Name.UDT }, userTypeName = "address_demo_type") 
    @Column(value="addresses")
    private List<Address> addresses;
    @Override
    public Integer getId() {
        return custId;
    }
    @Override
    public void setId(Integer id) {
        this.custId = id;
    }
    public static CustomerCassandraStore getStore() {
        return (CustomerCassandraStore) SpringContainer.getBean(CustomerCassandraStoreImpl.class);
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(Address... addresses) {
        LinkedList<Address> addressList = new LinkedList<>();
        for(Address addr: addresses) {
            addressList.add(addr);
        }
        setAddresses(addressList);
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    @UserDefinedType(value="address_demo_type")
    public static class Address {

        @CassandraType(type = DataType.Name.TEXT) 
        @Column(value="street_address")
        private String streetAddress;

        @CassandraType(type = DataType.Name.TEXT) 
        @Column(value="city")   
        private String city;

        @CassandraType(type = DataType.Name.TEXT) 
        @Column(value="country")    
        private String country;

        @CassandraType(type = DataType.Name.TEXT) 
        @Column(value="pincode")    
        private String pincode;

        public String getStreetAddress() {
            return streetAddress;
        }
        public void setStreetAddress(String streetAddress) {
            this.streetAddress = streetAddress;
        }
        public String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        }
        public String getCountry() {
            return country;
        }
        public void setCountry(String country) {
            this.country = country;
        }
        public String getPincode() {
            return pincode;
        }
        public void setPincode(String pincode) {
            this.pincode = pincode;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((city == null) ? 0 : city.hashCode());
            result = prime * result + ((country == null) ? 0 : country.hashCode());
            result = prime * result + ((pincode == null) ? 0 : pincode.hashCode());
            result = prime * result
                    + ((streetAddress == null) ? 0 : streetAddress.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Address other = (Address) obj;
            if (city == null) {
                if (other.city != null)
                    return false;
            } else if (!city.equals(other.city))
                return false;
            if (country == null) {
                if (other.country != null)
                    return false;
            } else if (!country.equals(other.country))
                return false;
            if (pincode == null) {
                if (other.pincode != null)
                    return false;
            } else if (!pincode.equals(other.pincode))
                return false;
            if (streetAddress == null) {
                if (other.streetAddress != null)
                    return false;
            } else if (!streetAddress.equals(other.streetAddress))
                return false;
            return true;
        }}




}

【问题讨论】:

  • 能不能加个行不通的代码?或者你尝试了什么?
  • M.Mimpen 请您现在检查一下代码并告诉我有什么问题吗?

标签: java spring spring-data-cassandra


【解决方案1】:
Table tableAnno=AnnotationUtils.getAnnotation(MessagePO.class, Table.class);
    String tableName=tableAnno.value();
    log.info("initial table{}",tableName);
    /**
     * create table
     */
    adminTemplate.createTable(true, CqlIdentifier.fromCql(tableName), MessagePO.class, null);
    Field[] fields = MessagePO.class.getDeclaredFields();

    /**
     * create index
     */
    CqlOperations cqlOperations=adminTemplate.getCqlOperations();
    Arrays.stream(fields).forEach(field -> {
        Indexed indexed = AnnotationUtils.getAnnotation(field, Indexed.class);
        Column column = AnnotationUtils.getAnnotation(field, Column.class);
        if (indexed != null && column != null) {
            String indexVal = indexed.value();
            String indexSql=String.format("CREATE INDEX %s on %s(%s)",indexVal,tableName,column.value());
            cqlOperations.execute(String.format("DROP INDEX if EXISTS %s",indexVal));
            cqlOperations.execute(indexSql);
            log.info("create index {}",indexSql);
        }
    });

这是我的初始索引代码,效果很好

【讨论】:

    【解决方案2】:

    我在测试执行期间创建架构时遇到了类似的问题(不清楚 OP 如何根据注释创建表)。

    我的代码正在调用CassandraAdminOperations.createTable() 函数,我很快发现createTable 不包含索引,因此对(假定的)索引列的任何查询都失败了。我的原始代码只是:

    @Autowired
    private CassandraAdminOperations adminTemplate;
    
    @Before
    public void createTable() {
        adminTemplate.createTable(true, CqlIdentifier.of(DATA_TABLE_NAME), User.class, new HashMap<>());
    }
    

    为了根据注释生成索引,我不得不将我的@Before 方法修改为:

    @Autowired
    private CassandraAdminOperations adminTemplate;
    
    @Before
    public void createTable() {
        adminTemplate.createTable(true, CqlIdentifier.of(DATA_TABLE_NAME), User.class, new HashMap<>());
        CassandraPersistentEntity<?> entity = adminTemplate.getConverter().getMappingContext().getRequiredPersistentEntity(User.class);
        List<CreateIndexSpecification> indexSpecifications = adminTemplate.getConverter().getMappingContext().getCreateIndexSpecificationsFor(entity);
        indexSpecifications.forEach(is -> adminTemplate.getCqlOperations().execute((CreateIndexCqlGenerator.toCql(is))));
    }
    

    【讨论】:

      【解决方案3】:

      它看起来像是 spring-data-cassandra 库中的一些错误。我也无法通过注释创建索引。

      【讨论】:

        猜你喜欢
        • 2017-10-25
        • 2016-11-29
        • 2019-11-29
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        • 2010-12-29
        • 1970-01-01
        • 2015-11-16
        相关资源
        最近更新 更多