如何在Java中的FlatBuffer中存储字典(map) [英] How to store dictionaries(map) in flatbuffer in Java

查看:237
本文介绍了如何在Java中的FlatBuffer中存储字典(map)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从链接中学习平面缓冲区,没有示例来演示如何存储字典(地图).在链接中提到了在java/Csharp中存储字典",但对此我并不太了解.我来自Java背景.如何在Java的Flatbuffers中存储字典/地图的任何示例都将有所帮助

I was learning flatbuffers from this link , there was no example to demonstrate how to store dictionary(map). There was a mention of "Storing dictionaries in java/Csharp" in this link , but i did not understand much about it. I am from java background. Any example of how to store dictionary/map in flatbuffers in java would be helpful

推荐答案

我意识到这是一个老问题,但是当我试图找出同一件事时遇到了这个问题.这是我获取字典/地图"的方法

I realize this is an old question but I came across it when I was trying to figure out the same thing. Here is what I did to get a "dictionary/map"

namespace com.dictionary;

table DataItem{
  keyValue:string(key);
  name:string;
  age:int;
  codes[string];
}

table DictionaryRoot{
  items:[DataItem];
}

root_type DictionaryRoot;

当您通过FlatBuffers编译器 flatc -j schema.fbs 运行此文件时,它将生成两个Java文件,一个名为 DictionaryRoot.java ,另一个名为DataItem.java .

When you run this through the FlatBuffers compiler flatc -j schema.fbs it will produce two Java files, one named DictionaryRoot.java, the other named DataItem.java.

使用这两个生成的Java文件,您将需要构造缓冲区.这必须从最内层的数据到最外层的数据完成.因此,您需要在 DictionaryRoot 之前构造 DataItems (并跟踪它们的偏移量).

Using those two generated Java files you will need to construct the buffer. This has to be done from the innermost data to the outermost. So you need to construct your DataItems (and keep track of their offsets) before your DictionaryRoot.

在此示例中,假设您有Java的对象映射,您需要从中创建缓冲区.

In this example, let's assume that you have a map of Objects in Java that you need to create the buffer from.

List<Integer> offsets = new ArrayList<>();
FlatBufferBuilder builder = new FlatBufferBuilder(1024);

for (Entry<String, DataObj> entry : map.entrySet()) {
  DataObj dataObj = entry.getValue();

  // use the builder to create the data and keep track of their offsets
  int keyValueOffset = builder.createString(entry.getKey());
  int nameOffset = builder.createString(dataObj.getName());
  int ageOffset = dataObj.getAge();
  int[] codesOffsets = dataObj.getCodes().stream().mapToInt(builder::createString)
      .toArray();

  // use the builder to create a vector using the offsets from above
  int codesVectorOffset = DataItem.createCodesVector(builder, codesOffsets);

  // now with all the inner data offsets, create the DataItem
  DataItem.startDataItem(builder);

  DataItem.addKeyValue(builder, keyValueOffset);
  DataItem.addName(builder, nameOffset);
  DataItem.addAge(builder, ageOffset);
  DataItem.addCodes(builder, codesVectorOffset);

  // ensure you 'end' the DataItem to get the offset
  int dataItemOffset = DataItem.endDataItem(builder);

  // track the offsets
  offsets.add(dataItemOffset);
}

// use the builder to create a sorted vector from your offsets. This is critical
int sortedVectorOffset = builder.createSortedVectorOfTables(new DataItem(),
    offsets.stream().mapToInt(Integer::intValue).toArray());

// now with your sorted vector, create the DictionaryRoot
DictionaryRoot.startDictionaryRoot(builder);

DictionaryRoot.addItems(builder, sortedVectorOffset);

int dictRootOffset = DictionaryRoot.endDictionaryRoot(builder);

// signal to the builder you are done
builder.finish(dictRootOffset);

// Write data to file
FileOutputStream outputStream = new FileOutputStream("output.bin");
outputStream.write(builder.sizedByteArray());
outputStream.close();


我希望这对使用FlatBuffers的人有所帮助.


I hope that will help someone else along their journey using FlatBuffers.

这篇关于如何在Java中的FlatBuffer中存储字典(map)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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