【发布时间】: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