简体   繁体   English

List <HashMap <String,Object >>的Thrift类型建模

[英]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.

相关问题 创建一个类型的HashMap <String , Object> - Creating a HashMap of type <String , Object> 哈希表<String,List<String> &gt; 到 HashMap <Object,List<String> &gt; 使用流 - HashMap<String,List<String>> to HashMap<Object,List<String>> using streams 为什么选择HashMap <String, Object> 不接受HashMap <String, List> 实例? - why HashMap<String, Object> not accept HashMap<String, List> instance? 如何从HashMap类型的Hashmap中删除重复项 <String, Object> ? - How to remove duplicates from Hashmap of type HashMap<String, Object>? 如何使用 stream 过滤器获取 hashmap 列表? ArrayList <hashmap<string, object> &gt; </hashmap<string,> - How use stream filter for list of hashmap? ArrayList<HashMap<String, Object>> 转换清单 <HashMap<String, Object> &gt;串流 - Convert List<HashMap<String, Object>> to stream 按键值排序列表&lt;HashMap &lt;String,Object &gt;&gt; - Sort List < HashMap < String, Object >> by value of key 使用清单 <type> 而不是ArrayList <Hashmap<String,String> &gt;用于列表视图 - Use a List<type> instead of ArrayList<Hashmap<String,String>> for list view 转换清单 <Object> 列出 <HashMap<String,Double> &gt;用于json数组 - convert List<Object> to List<HashMap<String,Double>> for a json array 取得清单 <String> 和HashMap <String, Object> 使用Java 8流 - Get List<String> and HashMap<String, Object> using Java 8 streams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM