使用 ifNotExists 插入 Datastax Cassandra

Datastax Cassandra Insert with ifNotExists

我已经创建了一个键空间

CREATE KEYSPACE xyz WITH replication = {'class': 'NetworkTopologyStrategy', 'DC1': '1', 'datacenter1': '1'}  AND durable_writes = true;

我只有两个节点,DC1 和 datacenter1 节点都已启动。现在,当我尝试执行一批插入语句时

Insert insert = QueryBuilder.insertInto(keyspace, table).ifNotExists()
.value("home", fieldsToUpdate.getHome())
.value("subCategoryName", fieldsToUpdate.getSubCategoryName())
.value("id", fieldsToUpdate.getId());
batch.add(insert);
session.execute(batch);

我得到一个例外说

Caused by: com.datastax.driver.core.exceptions.UnavailableException: Not enough replica available for query at consistency QUORUM (2 required but only 1 alive)

当我删除 .ifNotExists() 子句批处理时无任何异常地执行。

使用 datastax 驱动程序版本 2.1.7 .

我应该怎么做才能解决这个问题?

编辑: 节点工具状态

abhisheks-MacBook-Pro:bin abhishekagarwal$ sudo ./nodetool status
objc[3398]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address        Load       Tokens  Owns    Host ID                               Rack
UN  192.168.1.111  19.81 MB   256     ?       f2651124-abdf-486a-a6d7-53327bc2d98c  RAC1
UN  192.168.1.5    5.22 MB    256     ?       d0c72798-1186-4bcb-9e0f-634964a3d083  rack1

Note: Non-system keyspaces don't have the same replication settings, effective ownership information is meaningless

问题是您定义了密钥空间以在 'datacenter1' 中保留一个副本,在数据中心 'DC1' 中保留一个副本。但是您没有名为 'DC1' 的数据中心,因此无法在两个数据中心中为 if not exists 子句获得法定数量的副本。

所以你应该像这样定义键空间:

CREATE KEYSPACE xyz WITH replication = {'class': 'NetworkTopologyStrategy', 'datacenter1': '1'}