Using aquiles to access Cassandra in.Net (two)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Above, we have established the basic structure of the project, today realize the data changes. In NoSQL, usually add and modify all think is a Mutation of data.


1 to describe the modifications to the Row entity.

    public class RowMutation
    {
        public string Key { get; set; }

        public IList<CellMutation> Mutations { get; set; }

        public RowMutation()
        {

        }

        public RowMutation(string key)
        {
            Key = key;
        }

        public RowMutation(string key, IList<CellMutation> mutations)
        {
            Key = key;
            Mutations = mutations;
        }
    }

    public class CellMutation
    {
        public string ColumnName { get; set; }

        public string DataValue { get; set; }

        public CellMutation()
        {

        }

        public CellMutation(string name)
        {
            ColumnName = name;
        }

        public CellMutation(string name, string value)
        {
            ColumnName = name;
            DataValue = value;
        }
    }

2 a reference to the aquiles namespace.

using Aquiles.Helpers.Encoders;
using Aquiles.Cassandra10;
using Aquiles.Core.Cluster;

3 members are initialized variables

        private string _clusterName { get; set; }
        private ConsistencyLevel _consistencyLevel { get; set; }
        private string _keyspaceName { get; set; }
        private ICluster _cluster = null;

        public AquilesDemo()
        {
            _clusterName = "xxxx";
            _consistencyLevel = ConsistencyLevel.LOCAL_QUORUM;
           _keyspaceName = "xxxx";

            _cluster = AquilesHelper.RetrieveCluster(_clusterName);
        }

4 to establish a general modified rows and columns method, other changes can be completed on the basis of calls, such as modifying one column, a row columns etc.

       public void Mutate(string columnFamily, IList<RowMutation> rowMutations)
        {
            if (string.IsNullOrWhiteSpace(columnFamily)) throw new ArgumentNullException("columnFamily");

            rowMutations = PrepareMutationList(rowMutations);
            Dictionary<byte[], Dictionary<string, List<Apache.Cassandra.Mutation>>> mutation_map = new Dictionary<byte[], Dictionary<string, List<Apache.Cassandra.Mutation>>>();

            foreach (var rowMutation in rowMutations)
            {
                byte[] key = ByteEncoderHelper.UTF8Encoder.ToByteArray(rowMutation.Key);

                Dictionary<string, List<Apache.Cassandra.Mutation>> cfMutation = new Dictionary<string, List<Apache.Cassandra.Mutation>>();
                List<Apache.Cassandra.Mutation> mutationList = new List<Apache.Cassandra.Mutation>();

                foreach (CellMutation cellMutation in rowMutation.Mutations)
                {
                    if (cellMutation.DataValue == null) continue;

                    Apache.Cassandra.Mutation mutation = new Apache.Cassandra.Mutation()
                    {
                        Column_or_supercolumn = new ColumnOrSuperColumn()
                        {
                            Column = new Column()
                            {
                                Name = ByteEncoderHelper.UTF8Encoder.ToByteArray(cellMutation.ColumnName),
                                Timestamp = DateTime.Now.ToUnixTimestamp(),
                                Value = ByteEncoderHelper.UTF8Encoder.ToByteArray(cellMutation.DataValue),
                            },
                        },
                    };
                    mutationList.Add(mutation);
                }

                if (mutationList.Count > 0)
                {
                    cfMutation.Add(columnFamily, mutationList);
                    mutation_map.Add(key, cfMutation);
                }
            }

            if (mutation_map.Count == 0) return;
            _cluster.Execute(new ExecutionBlock(delegate(Apache.Cassandra.Cassandra.Client client)
            {
                client.batch_mutate(mutation_map, _consistencyLevel);
                return null;
            }), _keyspaceName);

        }

 5 as the result of a call to end may be the same line changes into multiple RowMutation afferent, so the above code using the PrepareMutationList method based on RowKey for polymerization process.

        /// <summary>
        /// The same item polymerization rowkey list for the same Mutation. 
        /// </summary>
        /// <param name="rowMutations"></param>
        /// <returns></returns>
        private IList<RowMutation> PrepareMutationList(IList<RowMutation> rowMutations)
        {
            if (rowMutations == null) return null;

            //According to the rowkey grouping
            var rowMutationGroups = rowMutations.GroupBy(rm => rm.Key);

            //The total number of the same group, is that rowkey is the only in the list, do not do polymerization process
            if (rowMutations.Count == rowMutationGroups.Count()) return rowMutations;

            //Traversing the grouping results
            IList<RowMutation> result = new List<RowMutation>();
            foreach (var rmg in rowMutationGroups)
            {
                RowMutation rowMutation = new RowMutation();
                rowMutation.Key = rmg.Key;

                //All CellMutation the same packet in a list
                List<CellMutation> cellMutations = new List<CellMutation>();
                foreach (var rm in rmg)
                {
                    cellMutations.AddRange(rm.Mutations);
                }

                if (cellMutations.Count() > 0)
                {
                    rowMutation.Mutations = cellMutations;
                    result.Add(rowMutation);
                }
            }
            return result;
        }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Cheney at November 13, 2013 - 7:40 PM