首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用writingDocument时读取编年史队列?

如何在使用writingDocument时读取编年史队列?
EN

Stack Overflow用户
提问于 2021-11-17 09:27:31
回答 2查看 159关注 0票数 0

这是Chronicle的版本:

代码语言:javascript
复制
<groupId>net.openhft</groupId>
<artifactId>chronicle-queue</artifactId>
<version>5.21.93</version>

这是我如何使用附加器编写的:

代码语言:javascript
复制
        String basePath = OS.userDir() + "\\start";
        ChronicleQueue queue = SingleChronicleQueueBuilder.single(basePath).build();

        ExcerptAppender appender = queue.acquireAppender();
        DocumentContext documentContext = null;
        int i = 0;
        try {
            documentContext = appender.writingDocument();
            for (int j=0;j<1000;j++) {
                     documentContext.wire().write().text("text :: "+i++);
            }
        } finally {
            documentContext.close();
        }

我是这样读到的:

代码语言:javascript
复制
        ExcerptTailer b = queue.createTailer("b");

        String out = "";
        while ((out=b.readText())!=null){
            System.out.println(out);
        }

这是一个例外:

代码语言:javascript
复制
Exception in thread "main" net.openhft.chronicle.bytes.UTFDataFormatRuntimeException: 1010d
00010080                                         00 00 00               ···
........
00010100 00 00 00 00 00 00 00 00  5a 32 00 00 c0 e9 74 65 ········ Z2····te
00010110 78 74 20 3a 3a 20 30 c0  e9 74 65 78 74 20 3a 3a xt :: 0· ·text ::
00010120 20 31 c0 e9 74 65 78 74  20 3a 3a 20 32 c0 e9 74  1··text  :: 2··t
00010130 65 78 74 20 3a 3a 20 33  c0 e9 74 65 78 74 20 3a ext :: 3 ··text :
00010140 3a 20 34 c0 e9 74 65 78  74 20 3a 3a 20 35 c0 e9 : 4··tex t :: 5··
00010150 74 65 78 74 20                                   text             
... truncated
    at net.openhft.chronicle.bytes.internal.BytesInternal.parseUtf8_SB1(BytesInternal.java:503)
    at net.openhft.chronicle.bytes.internal.BytesInternal.parseUtf8(BytesInternal.java:221)
    at net.openhft.chronicle.bytes.StreamingDataInput.parseUtf8(StreamingDataInput.java:553)
    at net.openhft.chronicle.wire.BinaryWire.getStringBuilder(BinaryWire.java:775)
    at net.openhft.chronicle.wire.BinaryWire.readText(BinaryWire.java:1264)
    at net.openhft.chronicle.wire.BinaryWire.readText(BinaryWire.java:1270)
    at net.openhft.chronicle.wire.BinaryWire$BinaryValueIn.textTo(BinaryWire.java:2326)
    at net.openhft.chronicle.wire.ValueIn.text(ValueIn.java:61)
    at net.openhft.chronicle.wire.MarshallableIn.readText(MarshallableIn.java:103)
    at com.cq.test.Test.main(Test.java:48)
Caused by: net.openhft.chronicle.bytes.UTFDataFormatRuntimeException: malformed input around byte 2 was 116 101
    at net.openhft.chronicle.bytes.internal.BytesInternal.parseUtf82(BytesInternal.java:630)
    at net.openhft.chronicle.bytes.internal.BytesInternal.parseUtf8_SB1(BytesInternal.java:500)
    ... 9 more

Process finished with exit code 1

我的问题是,我如何读取数据,在哪里可以获得这种用法(github或他们的文档?)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-18 08:28:30

尝试以下操作:您可以在https://github.com/OpenHFT/Chronicle-Queue中找到文档

代码语言:javascript
复制
 public static void main(String[] args) {
    String basePath = OS.userDir() + "/start";
    ChronicleQueue queue = SingleChronicleQueueBuilder.single(basePath).build();

    ExcerptAppender appender = queue.acquireAppender();
    DocumentContext documentContext = null;
    int i = 0;
    try {
        documentContext = appender.writingDocument();
        for (int j=0;j<10;j++) {
            documentContext.wire().write("No "+i).text(" text :: "+i++);

        }
    }
    finally {
        documentContext.close();
    }

    ExcerptTailer b = queue.createTailer("b");

        int k=0;
        try (final DocumentContext dc = b.readingDocument()) {
            if (!dc.isPresent())
                return;
            for (i=0;i<10 ;i++ ) {
            final String output = dc.wire().read("No "+k++).text();

                System.out.println(output);

        }
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70001829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档