Apache点燃C++结构到PiGielt对象不工作

2024-06-02 08:51:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Apache I点火ToCKER容器,我想用C++将数据写入缓存,然后用Python中的PyGielt访问它。我已经能够使用Apache IGITE C++ C++瘦客户端将关键值对添加到缓存中,然后使用PythIt从Python读取它们,如果它们的关键字和值是两种语言中的识别类型,例如int、浮点、字符串等。我想创建键值对,其中关键字是STD::string,值是STD::但是我不能让它在C++中工作,所以我决定按照方向{a1}来创建包含STD::map的C++结构。我可以在C++中获取/键入键值对,但是我不能用Python来获取它们。

这是工作的C++结构:

#include <stdio.h>
#include <stdlib.h>
#include <string>

#include <ignite/binary/binary.h>
#include <ignite/thin/ignite_client.h>
#include <ignite/thin/cache/cache_client.h>

using namespace ignite;
using namespace thin;
using namespace cache;


class MyMap
{
  friend struct binary::BinaryType<MyMap>;
public:
  MyMap() { }

  MyMap(std::map<std::string, float> mp) :
  mp(mp) { }

  std::map<std::string, float> GetMap() const
  {
    return mp;
  }

  std::string ToString()
  {
    std::ostringstream oss;

    for(std::map<std::string, float>::const_iterator it = mp.begin();
    it != mp.end(); ++it)
      {
    oss << it->first << ": " << it->second << "\n";
      }
    return oss.str();
  }
  std::map<std::string, float> mp;
};
typedef struct MyMap MyMap;

template<>
struct binary::BinaryType<MyMap>
{

  static int32_t GetTypeId()
  {
    return GetBinaryStringHashCode("MyMap");
  }

  static void GetTypeName(std::string& name)
  {
    name = "MyMap";
  }

  static int32_t GetFieldId(const char* name)
  {
    return GetBinaryStringHashCode(name);
  }

  static bool IsNull(const MyMap& obj)
  {
    return obj.GetMap().empty();
  }

  static void GetNull(MyMap& mymap)
  {
    mymap = MyMap();
  }

  static void Write(BinaryWriter& writer, const MyMap& obj)
  {
    BinaryMapWriter<std::string, float> bmap_writer = writer.WriteMap<std::string, float>("mp");

    for(std::map<std::string, float>::const_iterator it = obj.mp.begin(); it != obj.mp.end(); ++it)
      {
    bmap_writer.Write(it->first, it->second);
      }

    bmap_writer.Close();
  }

  static void Read(BinaryReader& reader, MyMap& mymap)
  {
    BinaryMapReader<std::string, float> bmap_reader = reader.ReadMap<std::string, float>("mp");
    std::string key;
    float val;
    std::map<std::string, float> tmp_map;
    while (bmap_reader.HasNext()){
      bmap_reader.GetNext(key, val);
      tmp_map[key] = val;
    }
    mymap.mp = tmp_map;
  }
};

下面是我如何填充缓存的:

IgniteClientConfiguration cfg;
cfg.SetEndPoints("ignite_cache");
IgniteClient client = IgniteClient::Start(cfg);

CacheClient<std::string, MyMap> cache = client.GetOrCreateCache<std::string, MyMap>("ignite_cache");

std::string key("my_test_key");
std::map<std::string, float> data_map;
data_map["a"] = 1.0;
data_map["b"] = 2.5;
MyMap mymap (data_map);

cache.Put(key, mymap);

以下是我试图实现的目标,但在Python中无法实现:

import pyignite

client = pyignite.Client()
client.connect("ignite_cache", 10800)
my_cache = client.get_or_create_cache("ignite_cache")

key = "my_test_key"

# This returns True
my_cache.contains_key(key)

# I was unable to get pyignite.register_binary_type to work so I tried this
res = pyignite.api.binary.put_binary_type(client, "MyMap", schema={'mp': pyignite.datatypes.MapObject})
# res.message = 'Success'

# This is where it doesn't work - it doesn't break, it just hangs.
my_cache.get(key)

我想我的问题是如何在Python中访问缓存。如果值是整数、浮点、字符串或内置的东西,那么我可以在Python中执行cache.get(key)并且它可以工作。由于C++代码对于^ {CD2}}和^ {CD3}}(C++)都起作用,所以我认为这是关于这两种语言之间的某种关系。

有没有人知道我做错了什么,或者我需要修改C++结构或如何在Python中定义二进制类型?


Tags: keyclientmapcachestringincludestaticit
1条回答
网友
1楼 · 发布于 2024-06-02 08:51:55

我不知道为什么会挂起,但是尝试着与客户端的{{CD1>}参数一起玩,它可能是C++节点需要的。p>

您还可以扫描节点的控制台日志以查找线索

作为补充说明,您不必在此处注册任何类型。只要my_cache.get(key)就足够了

相关问题 更多 >