通过 cassandra-cli 删除行后,通过 thrift-client 插入 Cassandra 不起作用

发布于 2024-09-28 23:02:51 字数 2400 浏览 7 评论 0原文

我编写了一个简单的测试来验证我自己对 Cassandra 的 thrift 接口的理解。它只是将一行插入数据库(使用 cassandra 安装中预先配置的键空间和列族),然后从数据库中读取它并比较结果。

public class CassandraAPITest {
    @Test
    public void testCassandraAPI() throws Exception {
        TTransport tr = new TSocket("localhost", 9160);
        tr.open();
        Client client = new Cassandra.Client(new TBinaryProtocol(tr));
        String key = "123";
        byte[] value = { 52, 53, 54 };
        ColumnPath columnPath = new ColumnPath("Standard1");
        columnPath.setColumn("abc".getBytes("UTF8"));
        long timestamp = System.currentTimeMillis();

       client.insert("Keyspace1", key, columnPath, value, timestamp, ConsistencyLevel.ONE);

        SlicePredicate predicate = new SlicePredicate();
        SliceRange sliceRange = new SliceRange();
        sliceRange.setStart(new byte[0]);
        sliceRange.setFinish(new byte[0]);
        predicate.setSlice_range(sliceRange);

        List<ColumnOrSuperColumn> result = client.get_slice("Keyspace1", key, new ColumnParent("Standard1"), predicate, ConsistencyLevel.ONE);

        assertEquals(1, result.size());
        byte[] actual = result.get(0).column.value;
        assertArrayEquals(value, actual);

        // client.remove("Keyspace1", key, columnPath, System.currentTimeMillis(), ConsistencyLevel.ONE);

        tr.close();
    }
}

这个测试运行良好。当然,它会在数据库中留下一行。我可以通过取消注释上面的 client.remove 语句来删除测试结束时的行(这也可以正常工作)。但我尝试的是通过命令行界面删除该行:

cassandra> connect localhost/9160                   
Connected to: "Test Cluster" on localhost/9160
cassandra> get Keyspace1.Standard1['123']
=> (column=616263, value=456, timestamp=1287909211506)
Returned 1 results.
cassandra> del Keyspace1.Standard1['123']   
row removed.
cassandra> get Keyspace1.Standard1['123']
Returned 0 results.

之后测试失败。将行插入数据库似乎不再有任何效果,因此行 assertEquals(1, result.size()) 失败:

java.lang.AssertionError: expected:<1> but was:<0>
    at org.junit.Assert.fail(Assert.java:91)
    at org.junit.Assert.failNotEquals(Assert.java:618)
    at org.junit.Assert.assertEquals(Assert.java:126)
    at org.junit.Assert.assertEquals(Assert.java:443)
    at org.junit.Assert.assertEquals(Assert.java:427)
    at test.package.CassandraAPITest.testCassandraAPI(CassandraAPITest.java:48)

我没有收到任何错误消息(无论是在客户端还是在服务器上),并且我没有了解问题的原因可能是什么。

I wrote a simpe test to validate my own understanding of the thrift interface for Cassandra. It just inserts a row into the database (using the keyspace and column familty that come preconfigured with the cassandra installation), then reads it from the database and compares the results.

public class CassandraAPITest {
    @Test
    public void testCassandraAPI() throws Exception {
        TTransport tr = new TSocket("localhost", 9160);
        tr.open();
        Client client = new Cassandra.Client(new TBinaryProtocol(tr));
        String key = "123";
        byte[] value = { 52, 53, 54 };
        ColumnPath columnPath = new ColumnPath("Standard1");
        columnPath.setColumn("abc".getBytes("UTF8"));
        long timestamp = System.currentTimeMillis();

       client.insert("Keyspace1", key, columnPath, value, timestamp, ConsistencyLevel.ONE);

        SlicePredicate predicate = new SlicePredicate();
        SliceRange sliceRange = new SliceRange();
        sliceRange.setStart(new byte[0]);
        sliceRange.setFinish(new byte[0]);
        predicate.setSlice_range(sliceRange);

        List<ColumnOrSuperColumn> result = client.get_slice("Keyspace1", key, new ColumnParent("Standard1"), predicate, ConsistencyLevel.ONE);

        assertEquals(1, result.size());
        byte[] actual = result.get(0).column.value;
        assertArrayEquals(value, actual);

        // client.remove("Keyspace1", key, columnPath, System.currentTimeMillis(), ConsistencyLevel.ONE);

        tr.close();
    }
}

This test runs fine. Of course it leaves a row behind in the database. I could delete the row at the end of the test by uncommenting the client.remove statement above (this also works fine). But what I tried instead was deleting the row via the command-line interface:

cassandra> connect localhost/9160                   
Connected to: "Test Cluster" on localhost/9160
cassandra> get Keyspace1.Standard1['123']
=> (column=616263, value=456, timestamp=1287909211506)
Returned 1 results.
cassandra> del Keyspace1.Standard1['123']   
row removed.
cassandra> get Keyspace1.Standard1['123']
Returned 0 results.

The test fails afterwards. Inserting the row into the database seems to have no effect anymore, so the line assertEquals(1, result.size()) fails:

java.lang.AssertionError: expected:<1> but was:<0>
    at org.junit.Assert.fail(Assert.java:91)
    at org.junit.Assert.failNotEquals(Assert.java:618)
    at org.junit.Assert.assertEquals(Assert.java:126)
    at org.junit.Assert.assertEquals(Assert.java:443)
    at org.junit.Assert.assertEquals(Assert.java:427)
    at test.package.CassandraAPITest.testCassandraAPI(CassandraAPITest.java:48)

I don't get any error messages (neither on the client nor on the server) and I have no idea what the cause of the problem might be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城°AllureLove 2024-10-05 23:02:51

您正在以毫秒分辨率插入,但 CLI(和其他高级客户端)使用微秒。因此,与删除相比,您的第二次插入是过去的,因此 Cassandra 正确地忽略了它。

You are inserting with millisecond resolution but the CLI (and other high level clients) uses microseconds. So your second insert is in the past, compared to the delete, so Cassandra correctly ignores it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文