我尝试在Spring Boot应用程序启动时在cassandra db中创建表,但是它似乎无法创建表。下面是我的配置。我的应用程序类中有@EnableCassandraRepositories
。默认情况下,我已经创建了密钥空间。因此,这只是我要创建的表。
配置
@Configuration
public class CassandraConfig extends AbstractCassandraConfiguration {
@Value("${cassandra.contactpoints}")
private String contactPoints;
@Value("${cassandra.port}")
private int port;
@Value("${cassandra.keyspace}")
private String keySpace;
@Value("${cassandra.basePackages}")
private String basePackages;
@Autowired
private Environment environment;
@Override
protected String getKeyspaceName() {
return keySpace;
}
@Override
@Bean
public CassandraClusterFactoryBean cluster() {
final CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setContactPoints(contactPoints);
cluster.setPort(port);
return cluster;
}
@Override
@Bean
public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
return new BasicCassandraMappingContext();
}
}
实体
@Table
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class AssessmentAttemptDetailsEntity implements Serializable {
@PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED)
private String assessmentId;
@PrimaryKeyColumn(type = PrimaryKeyType.CLUSTERED)
private String attempid;
}
应用
@SpringBootApplication
@ComponentScan(basePackages = {"com.lte.assessmentanalytics.service","com.lte.assessmentanalytics.config", "com.lte.assessmentanalytics.model", "com.lte.assessmentanalytics.listener"})
@EnableCassandraRepositories("com.lte.assessmentanalytics.model")
public class AssessmentanalyticsApplication {
@Autowired
private AssessmentAttemptRepository assessmentAttemptRepository;
public static void main(String[] args) {
SpringApplication.run(AssessmentanalyticsApplication.class, args);
}
}
存储库
@Repository
public interface AssessmentAttemptRepository extends CassandraRepository<AssessmentAttemptDetailsEntity, Long> {
}
答案 0 :(得分:0)
通过修改我的CassandraConfig
类,我得以解决此问题。
@Configuration
@EnableCassandraRepositories("com.lte.assessmentanalytics.model")
public class CassandraConfig extends AbstractCassandraConfiguration {
@Value("${cassandra.contactpoints}")
private String contactPoints;
@Value("${cassandra.port}")
private int port;
@Value("${cassandra.keyspace}")
private String keySpace;
@Value("${cassandra.basePackages}")
private String basePackages;
@Override
protected String getKeyspaceName() {
return keySpace;
}
@Override
protected String getContactPoints() {
return contactPoints;
}
@Override
protected int getPort() {
return port;
}
@Override
public SchemaAction getSchemaAction() {
return SchemaAction.CREATE_IF_NOT_EXISTS;
}
@Override
public String[] getEntityBasePackages() {
return new String[] {basePackages};
}
}