返回 Kafka 结果时出现 ArrayIndexOutOfBoundsException

ArrayIndexOutOfBoundsException while returning Kafka results

我正在 Java 中开发自定义 Talend 组件以从 Kafka 主题读取数据,我的目标是实时处理这些数据并将它们写入文件。

当我得到的数据不是太大时,我的 Java 代码可以正常工作,否则我会得到这个错误:

JAVA ERROR: ArrayIndexOutOfBoundsException

我了解到我发送的数据太大了,所以我有两个问题:

这是我的 Java 代码:

@Producer
    public Record next() {
        while (true){
            ConsumerRecords<String, Object> records = consumer.poll(Duration.ofMillis(10));
            System.out.println("new poll");
            ArrayList<String> elements = new ArrayList<>();
            Integer count = 0;
            for (ConsumerRecord<String, Object> record : records) {
                count ++;
                String current_line = record.value().toString();
                JSONObject jsonObject = new JSONObject(current_line);
                 test = jsonObject.get("type").toString();
                 elements.add(current_line);
                 System.out.println("in= " + test);
        }
            System.out.println("count = " + count.toString());

             if (elements.size()>0) {
                 return builderFactory.newRecordBuilder().withString("name", elements.toString()).build();
             }
            consumer.commitAsync();
        }
    }

感谢您的帮助,

此致,

托马斯

编辑内存和 JVM 设置 要在运行时和启动 Talend Studio 时获得性能,您可以在 .ini 文件中编辑内存设置。

默认情况下,.ini 文件设置以下 JVM 参数:

--launcher.XXMaxPermSize512m -vmargs -Xms64m -Xmx768m -Xdock:icon=../Resources/talend.icns -XstartOnFirstThread -Dfile.encoding=UTF-8

在 64 位系统上可用 8 GB 内存的情况下,最佳设置可以是:

--launcher.XXMaxPermSize512m -vmargs -Xms2014m -Xmx4096m -XstartOnFirstThread -Dfile.encoding=UTF-8