[英]Thrift type modelling for List<HashMap<String,Object>>
I've a service method in Java with following return type: 我在Java中使用以下返回类型的服务方法:
List<HashMap<String,Object>>
How can I best model it in thrift? 我怎样才能在节俭中最好地塑造它?
Pretty straightforward: 很简单:
struct MyObjectData {
// data of your objects
}
list< map< string, MyObjectData>>
You may want to make it a type: 你可能想把它变成一个类型:
typedef list< map< string, MyObjectData>> MyObjectStructure
The caveat lies in the data structure of MyObjectData
. 需要注意的是MyObjectData
的数据结构。 If by Object
you literally mean any Object
, then we've got a problem. 如果按Object
你的字面意思是任何 Object
,那么我们就遇到了问题。 Thrift can't handle generic cases like this, because it does not support structures derived from each other (like you can do with class
). Thrift无法处理这样的通用情况,因为它不支持彼此派生的结构(就像你可以用class
做的那样)。 What you can do is, to use a union
holding different kinds of structs, where only one is used at a time: 你可以做的是,使用一个包含不同类型结构的union
,其中一次只使用一个结构:
struct Foo { /* some fields */ }
struct Bar { /* some fields */ }
// a union allows us to store different kinds of structs in one list
union Generic {
1: Foo foo
2: Bar bar
}
// technically, a union is more or less a struct with optional fields,
struct Alternative {
1: optional Foo foo
2: optional Bar bar
}
In case you need derived structures, I have solved this problem for me by doing this: 如果您需要派生结构,我通过这样做解决了这个问题:
struct Base {
// some fields
}
struct Derived {
1: Base base_
// some more fields
}
which works quite well for me. 这对我很有用。 If you have a deep inheritance tree, it might become somewhat painful to work with, but that's not the case in my particular case. 如果你有一个深度继承树,它可能会变得有点痛苦,但在我的特定情况下情况并非如此。
AFAIK thrift does not directly support generic Object types which can be type casted to/from any object you like. AFAIK thrift不直接支持通用对象类型,这些类型可以是从您喜欢的任何对象进行类型转换的类型。 You'll have to specifically define your object as in above example. 你必须像上面的例子那样专门定义你的对象。 You can't have a return type as Object. 您不能将返回类型作为Object。 There's a work around mentioned here: Generic objects in Apache Thrift 这里有一个解决方法: Apache Thrift中的通用对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.