【问题标题】:Handling C# Nested Classes in Cassandra在 Cassandra 中处理 C# 嵌套类
【发布时间】:2019-12-22 11:44:52
【问题描述】:

我有一个如下所示的 C# 类

public class User
{
    public string UserId { get; set; }
    public string Name { get; set; }

    public List<Address> Address { get; set; }
}

public class Address
{
    public string Line1 { get; set; }
}

我创建了如下的键空间和表

ISession session;
        IMapper Mapper;
    Table<User> table;

    var mapping = MappingConfiguration.Global.Define(
                     new Map<User>()
                    .TableName(typeof(User).Name)
                    .PartitionKey(u => u.UserId))

                    ;

        Dictionary<string, string> replication = new Dictionary<string, string>();

        replication.Add("class", "SimpleStrategy");
        replication.Add("replication_factor", "3");
        var cluster = Cluster.Builder()
                                       .AddContactPoints("127.0.1")
                                       .WithPort(9042)
                                       .WithLoadBalancingPolicy(new DCAwareRoundRobinPolicy("test"))
                                       .WithReconnectionPolicy(new FixedReconnectionPolicy(400, 5000, 2 * 60000, 60 * 60000))
                                       .Build();


        session = cluster.Connect();
        session.CreateKeyspaceIfNotExists("demo1", replication);
        session = cluster.Connect("demo1");


        table = new Table<User>(session, mapping, typeof(User).Name, "demo1");
        table.CreateIfNotExists();

我收到以下错误

Cassandra.InvalidTypeException: 'CLR 的未知 Cassandra 目标类型 键入 Casandra.Address'

【问题讨论】:

    标签: c# cassandra


    【解决方案1】:

    您的 C# 类 Address 应与 Apache Cassandra 数据库中的字段类型匹配。

    在这种情况下,您似乎想使用User-Defined Types (UDT)。 DataStax C# 驱动程序不支持使用Table&lt;T&gt;.Create() 创建嵌套子类型。

    理想情况下,您应该首先定义您的架构,该架构应该针对您要用于获取数据的查询进行建模。我建议您先在 CQL 中执行此操作,然后在您的 C# 代码中添加 mapping configuration

    【讨论】:

      【解决方案2】:

      试试下面的

      var mapping = MappingConfiguration.Global.Define(
                           For<User>()
                          .TableName(typeof(User).Name)
                          .PartitionKey(u => u.UserId);
      

      【讨论】:

      • 感谢回复,For是在哪个命名空间,我用的是CassandraCSharpDriver 3.11.0版。
      • 您好,我注意到您的 .AddContactPoints("127.0.1") 不是有效 IP。如果您在本地托管,请将其更改为 .AddContactPoints("127.0.0.1")
      猜你喜欢
      • 2018-01-31
      • 1970-01-01
      • 2023-01-31
      • 2023-02-10
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      相关资源
      最近更新 更多