首先導入pom,這是datastax的驅動,很好用。spring
<dependency> <groupId>com.datastax.cassandra</groupId> <artifactId>cassandra-driver-mapping</artifactId> <version>3.0.2</version> </dependency>
創建session鏈接:session
PoolingOptions poolingOptions = new PoolingOptions(); poolingOptions .setConnectionsPerHost(HostDistance.LOCAL, 4, 10) .setConnectionsPerHost(HostDistance.REMOTE, 2, 4) .setMaxRequestsPerConnection(HostDistance.LOCAL, 32768) .setMaxRequestsPerConnection(HostDistance.REMOTE, 2000) .setHeartbeatIntervalSeconds(60); cluster = Cluster.builder() .addContactPoint(address) .withPoolingOptions(poolingOptions) .build(); Metadata metadata = cluster.getMetadata(); for (Host host : metadata.getAllHosts()) { log.info("Data center: " + host.getDatacenter() + "; Host: " + host.getAddress() + "; Rack: " + host.getRack()); } session = cluster.connect();
對於model類的創建好像官網沒提示多個PartitionKey怎麼寫,我也是猜出來的:app
@PartitionKey 後面的0 -5表明查詢時參數依次添加,不標明的話用mapper查詢會報錯ui
,原文:spa
Ordinal to add when the partition key has multiple components.
@Table(keyspace = "wumartcluster", name = "table_name") public class TestClass{ @PartitionKey(0) private Long exe_id; @PartitionKey(1) private String promo_plan; @PartitionKey(2) private String mandt; @PartitionKey private Date execute_by_date; @PartitionKey(4) private String shops_no; @PartitionKey(5) private String goods_no; private Date start_date;
}日誌
今天補充一點,datastax 的 Mapper 只初始化一次就行,否則會不停的報warn日誌警告component
Mapper<ObjectBean> mapper ;
if (mapper == null) { mapper = cassandraClient.getMapper(ObjectBean.class); }
這裏本身寫個單例模式就好了,或者springMVC整合,很少說。ip