You need to enable JavaScript to run this app. 如何将Arrow向量存入Ignite?存储支持性、操作方法及报错咨询
优惠活动
大模型
产品
解决方案
定价
更多

如何将Arrow向量存入Ignite?存储支持性、操作方法及报错咨询

问题解答

1. Ignite是否支持存储Arrow格式数据?

Ignite没有内置Arrow格式的专属存储适配,但支持存储任意可序列化二进制数据,因此完全可以存储Arrow格式数据。你当前运行报错的原因是直接存储了Arrow的ValueVector实例,这类实例内部绑定了堆外内存引用、Netty内存分配器等无法直接被Ignite默认序列化器序列化的对象,才会抛出堆栈中所示的序列化/反序列化异常。

2. Arrow向量存入Ignite的实现方式

必须提前对Arrow向量做序列化处理,不能直接存储ValueVector实例,核心思路是将Arrow向量转换为标准的Arrow IPC格式字节数组后再存入Ignite,读取时再将字节数组反序列化为Arrow向量即可,具体实现参考如下:

正确实现步骤

  • 写入侧:将Arrow向量序列化为字节数组后存入Ignite缓存
  • 读取侧:从Ignite缓存取出字节数组,反序列化为Arrow向量使用

示例代码

private static final int DATA_SIZE = 1000;
private static final String CACHE_NAME = "arrow_cache";

// 序列化Arrow向量为字节数组的工具方法
public static byte[] serializeVector(ValueVector vector) throws IOException {
    try (ByteArrayOutputStream out = new ByteArrayOutputStream();
         VectorSchemaRoot root = new VectorSchemaRoot(List.of(vector.getField()), List.of(vector), vector.getValueCount());
         ArrowStreamWriter writer = new ArrowStreamWriter(root, null, Channels.newChannel(out))) {
        writer.start();
        writer.writeBatch();
        writer.end();
        return out.toByteArray();
    }
}

// 反序列化字节数组为Arrow向量的工具方法
public static ValueVector deserializeVector(byte[] data, BufferAllocator allocator) throws IOException {
    try (ByteArrayInputStream in = new ByteArrayInputStream(data);
         ArrowStreamReader reader = new ArrowStreamReader(Channels.newChannel(in), allocator)) {
        reader.loadNextBatch();
        VectorSchemaRoot root = reader.getVectorSchemaRoot();
        // 转移向量所有权,避免try-with-resources关闭root时释放向量内存
        ValueVector vector = root.getVector(0);
        vector.getReferenceManager().retain();
        return vector;
    }
}

public static void main(String[] args) throws Exception {
    try(Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        BufferAllocator bufferAllocator = new RootAllocator(1024 * 1024);
        IntVector intVector = new IntVector("int_vector", bufferAllocator);
        VarCharVector varCharVector = new VarCharVector("varchar_vector", bufferAllocator);

        intVector.allocateNew(DATA_SIZE);
        intVector.setValueCount(DATA_SIZE);
        varCharVector.allocateNew(DATA_SIZE);
        varCharVector.setValueCount(DATA_SIZE);

        for (int i = 0; i < DATA_SIZE; i++) {
            intVector.set(i, i);
            varCharVector.set(i, new Text("value-" + i));
        }

        try (IgniteCache<String, byte[]> cache = ignite.getOrCreateCache(CACHE_NAME)) {
            // 先序列化再存入
            cache.put("key_int", serializeVector(intVector));
            cache.put("key_varchar", serializeVector(varCharVector));
            
            // 读取测试
            byte[] intVecBytes = cache.get("key_int");
            IntVector deserializedIntVec = (IntVector) deserializeVector(intVecBytes, bufferAllocator);
            System.out.println(deserializedIntVec.get(0)); // 输出0验证正确性
        } finally {
            intVector.close();
            varCharVector.close();
            bufferAllocator.close();
            ignite.destroyCache(CACHE_NAME);
        }
    }
}

注意:需要确保项目中引入了Arrow IPC相关的依赖包,序列化时使用的Arrow版本和反序列化时的版本保持一致,避免出现兼容性问题。


内容的提问来源于stack exchange,提问作者Henrik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 03:15:03