コード例 #1
0
ファイル: HTable.cs プロジェクト: leegkon/NHBaseAPI
        /// <summary>
        ///    批量插入行操作
        /// </summary>
        /// <param name="exceptionMutations">插入异常的数据集合</param>
        /// <param name="batchMutation">行集合信息</param>
        /// <exception cref="IOErrorException">IO错误</exception>
        /// <exception cref="ArgumentNullException">参数不能为空</exception>
        /// <exception cref="CommunicationTimeoutException">通信超时</exception>
        /// <exception cref="CommunicationFailException">通信失败</exception>
        /// <returns>全部成功插入返回true,其它返回false</returns>
        public bool BatchInsert(out BatchMutation[] exceptionMutations, params BatchMutation[] batchMutation)
        {
            exceptionMutations = null;
            List <BatchMutation> exceptionMutationList = new List <BatchMutation>();

            if (batchMutation == null || batchMutation.Length == 0)
            {
                return(true);
            }
            Dictionary <IPEndPoint, List <BatchMutation> > dic = new Dictionary <IPEndPoint, List <BatchMutation> >();

            foreach (BatchMutation mutation in batchMutation)
            {
                IPEndPoint           iep = _regionManager.GetRegionByRowKey(mutation.RowKey);
                List <BatchMutation> list;
                if (!dic.TryGetValue(iep, out list))
                {
                    list = new List <BatchMutation>();
                }
                list.Add(mutation);
                dic[iep] = list;
            }
            foreach (KeyValuePair <IPEndPoint, List <BatchMutation> > pair in dic)
            {
                try
                {
                    _client.BatchInsert(TableName, pair.Key, pair.Value.ToArray());
                }
                catch (Exception ex)
                {
                    exceptionMutationList.AddRange(pair.Value);
                    _tracing.Error(string.Format("[{0}]BatchInsert exception : {1}", pair.Key.Address, ex.Message));
                    _tracing.Error(ex, null);
                }
            }
            if (exceptionMutationList.Count > 0)
            {
                exceptionMutations = exceptionMutationList.ToArray();
                return(false);
            }
            return(true);
        }