协思

协作、思考、感悟、进步

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

数据的持久化我们都已经完成了,和所有应有程序一样,最重要的是要向用户展示数据。下面我们就推出这部分代码,读取任意行任何列:

        public IList<TRowResult> ExecuteRowListResult(string columnFamily, IList<string> rowKeys, IList<string> columnNames)
        {
            if (string.IsNullOrWhiteSpace(columnFamily)) throw new ArgumentNullException("columnFamily");

            List<byte[]> keys = rowKeys.Select(rowKey => ByteEncoderHelper.UTF8Encoder.ToByteArray(rowKey)).ToList();
            ColumnParent columnPath = new ColumnParent()
            {
                Column_family = columnFamily,
            };

            SlicePredicate sp = null;
            if (columnNames == null || columnNames.Count == 0)
            {
                sp = new SlicePredicate()
                {
                    Slice_range = new SliceRange
                    {
                        Count = int.MaxValue,
                        Reversed = false,
                        Start = new byte[0],
                        Finish = new byte[0]
                    },
                };
            }
            else
            {
                sp = new SlicePredicate()
                {
                    Column_names = columnNames.Select(c => ByteEncoderHelper.UTF8Encoder.ToByteArray(c)).ToList()
                };
            }

            Dictionary<byte[], List<ColumnOrSuperColumn>> queryResult = _cluster.Execute(new ExecutionBlock(delegate(Apache.Cassandra.Cassandra.Client client)
            {
                return client.multiget_slice(keys, columnPath, sp, _consistencyLevel);
            }), _keyspaceName) as Dictionary<byte[], List<ColumnOrSuperColumn>>;


            if (queryResult != null && queryResult.Count > 0)
            {
                IList<TRowResult> rows = new List<TRowResult>();
                foreach (var dic in queryResult)
                {
                    if (dic.Value.Count == 0) continue;

                    TRowResult trow = new TRowResult();
                    trow.Row = ByteEncoderHelper.UTF8Encoder.FromByteArray(dic.Key);
                    trow.Columns = new Dictionary<string, TCell>();
                    foreach (ColumnOrSuperColumn column in dic.Value)
                    {
                        string name = ByteEncoderHelper.UTF8Encoder.FromByteArray(column.Column.Name);
                        string value = ByteEncoderHelper.UTF8Encoder.FromByteArray(column.Column.Value);
                        trow.Columns.Add(name, new TCell()
                        {
                            ColumnName = name,
                            Value = value,
                            Timestamp = column.Column.Timestamp
                        });
                    }
                    rows.Add(trow);
                }

                return rows;
            }

            return null;
        }

 

posted on 2013-07-30 22:05  协思  阅读(456)  评论(0编辑  收藏  举报