【问题标题】:What is the fastest way to load a wide row from Cassandra to C#?将宽行从 Cassandra 加载到 C# 的最快方法是什么?
【发布时间】:2015-02-11 00:11:17
【问题描述】:

将单个(或几个)宽行从 Cassandra 加载到 C# 的最高效的方法是什么?我的宽行有 10.000-100.000 列。主键由多个值组成,但列键是单个字符串,列值是单个计数器(请参见下面的架构)。

在 cqlsh 中使用“跟踪”我可以看到 Cassandra 可以选择 44 m 中包含 17.000 列的宽行,但是使用 Datastax 驱动程序将这些数据一直加载到 C# 需要 700 毫秒。有更快的方法吗?我需要在 50-100 毫秒内加载完整的宽行。 (有更原生的方式吗?最小化网络流量的方式?更快的驱动程序?驱动程序的另一种配置?还是别的什么?)

我实际上并不需要所有 17.000 列。我只需要“支持”> = 2 的列或按“支持”降序排序的前 1000 列。但是由于“支持”是我的列值,所以我不知道在 CQL 中可以通过什么方式进行这样的查询。

这是我的桌子:

CREATE TABLE real_time.grouped_feature_support (
    algorithm_id int,
    group_by_feature_id int,
    select_feature_id int,
    group_by_feature_value text,
    select_feature_value text,
    support counter,
    PRIMARY KEY ((algorithm_id, group_by_feature_id, select_feature_id, group_by_feature_value), select_feature_value)

这是我使用 Datastax 驱动程序访问数据的方式:

var table = session.GetTable<GroupedFeatureSupportDataEntry>();
var query = table.Where(x => x.CustomerAlgorithmId == customerAlgorithmId
    && x.GroupByFeatureId == groupedFeatureId
    && myGroupedFeatureValues.Contains(x.GroupByFeatureValue)
    && x.GroupByFeatureValue == groupedFeatureValue
    && x.SelectFeatureId == selectFeatureId)
    .Select(x => new
    {
        x.GroupByFeatureValue,
        x.SelectFeatureValue,
        x.Support,
    })
    .Take(1000000);
var result = query.Execute();

【问题讨论】:

    标签: c# cassandra cql cql3 datastax


    【解决方案1】:

    如果您在检索大型结果集时寻求最佳性能,则不应使用 Linq-to-cql 或任何其他映射组件。

    您可以使用technique documented on the driver readme 检索行,在您的情况下,它将类似于:

    var query = "SELECT * from grouped_feature_support WHERE" + 
                " algorithm_id = ? AND group_by_feature_id = ? " +
                " AND select_feature_id = ? AND group_by_feature_value = ?";
    //Prepare the query once in your application lifetime
    var ps = session.Prepare(query);
    //Reuse the prepared statement by binding different parameters to it
    var rs = session.Execute(ps.Bind(parameters));
    foreach (var row in rs)
    {
      //The enumerator will yield all the rows from Cassandra
      //Retrieving them in the back in blocks of 5000 (determined by the pagesize).
    }
    //You can also use a IEnumerable<T> Linq Extensions to filter
    var filteredRows = rs.Where(r => r.GetValue<long>("support") > 2);
    

    【讨论】:

    • 是否可以通过将“support >= 2”要求移至集群来最小化网络负载,或者在集群中引入“order by support desc and take the top”?
    • 不,Cassandra 不可能按照您想要的方式进行服务器端过滤(至少在 C* 3.0 之前是这样)
    猜你喜欢
    • 2016-01-28
    • 2012-12-09
    • 2010-11-30
    • 1970-01-01
    • 2023-03-25
    • 2011-07-11
    • 2011-07-16
    • 1970-01-01
    • 2016-07-13
    相关资源
    最近更新 更多