Apache Thrift 中的通用对象 [英] Generic objects in Apache Thrift

查看:26
本文介绍了Apache Thrift 中的通用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用 IDL 语言在 Apache Thrift 中定义通用对象",类似于 Java 中的对象"类.

I want to know if I can define "generic objects" in Apache Thrift with IDL language, something like "Object" class in Java.

我需要发送任何类型的对象列表,但我不知道如何在 file.thrift 中定义它

I need to send objects's list of any type, but I don't know how can I define this in the file.thrift

像这样:

struct
{
   1: list<object> listObjects;
}

struct
{
   1: list<?> listObjects;
}

推荐答案

Apache Thrift 不支持结构继承或自引用结构.这使得您想做的事情,即直接传递多态列表变得不可能.有很多方法可以解决这个问题,例如将对象序列化为二进制文件,然后传递一个二进制文件列表,在另一端对它们进行反序列化.

Apache Thrift does not support struct inheritance or self referential structs. This makes what you would like to do, passing a polymorphic list directly, impossible. There are ways to work around this, for instance serializing objects to binary and then passing a list of binaries, de-serializing them on the other end.

struct abc {
    1: list<binary> myList,
}

所有 Apache Thrift 结构都有读取和写入方法,您可以使用它们将它们序列化到内存缓冲区 (TMemoryBuffer),然后您可以将其用作列表的二进制对象.另一种选择可能是使用联合.

All Apache Thrift structs have read and write methods which you can use to serialize them to a memory buffer (TMemoryBuffer) which you can then use as a binary object for the list. Another option might be to use a union.

union myTypes {
    1: double dbl
    2: i64 bigInt
    3: SomeOtherStruct sos
}

struct abc {
    1: list<myTypes> myList,
}

此方法创建了一个 myTypes 联合类型列表,但联合可以容纳您喜欢的任何 IDL 定义类型.

This approach creates a list of the myTypes union type, but the union can house any IDL defined type you like.

这篇关于Apache Thrift 中的通用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆