/// <summary> /// 插入一行新的记录 /// </summary> /// <param name="rowKey">行键信息</param> /// <param name="columnInfos">插入的列信息</param> /// <exception cref="IOErrorException">IO错误</exception> /// <exception cref="ArgumentNullException">参数不能为空</exception> /// <exception cref="CommunicationTimeoutException">通信超时</exception> /// <exception cref="CommunicationFailException">通信失败</exception> /// <exception cref="RegionNotFoundException">找不到对应的RegionServer</exception> public void Insert(byte[] rowKey, params ColumnInfo[] columnInfos) { if (rowKey == null || rowKey.Length == 0) throw new ArgumentNullException("rowKey"); if (columnInfos == null || columnInfos.Length == 0) throw new ArgumentNullException("columnInfos"); Mutation[] mutations = new Mutation[columnInfos.Length]; for (int i=0; i < columnInfos.Length; i++) { mutations[i] = new Mutation(); mutations[i].ColumnName = string.Format("{0}:{1}", columnInfos[i].ColumnFamily, columnInfos[i].ColumnName); mutations[i].Value = columnInfos[i].Value; } IPEndPoint iep = _regionManager.GetRegionByRowKey(rowKey); if(iep == null) throw new RegionNotFoundException(string.Format("#Couldn't found any matched RS by specified row key: {0}", rowKey)); _client.InsertRow(TableName, rowKey, iep, mutations); }
/// <summary> /// 插入一行数据 /// </summary> /// <param name="tableName">表名</param> /// <param name="rowkey">行键</param> /// <param name="iep">对应的Region的服务器地址</param> /// <param name="mutations">列信息</param> /// <param name="attributes"></param> /// <exception cref="IOErrorException">IO错误</exception> /// <exception cref="IllegalArgumentException">参数错误</exception> /// <exception cref="CommunicationTimeoutException">通信超时</exception> /// <exception cref="CommunicationFailException">通信失败</exception> /// <returns></returns> internal bool InsertRow(string tableName, byte[] rowkey, IPEndPoint iep, Mutation[] mutations, Dictionary<string,string> attributes=null) { if (string.IsNullOrEmpty(tableName)) throw new ArgumentNullException("tableName"); if (rowkey == null || rowkey.Length == 0) throw new ArgumentNullException("rowkey"); if (mutations == null || mutations.Length == 0) throw new ArgumentNullException("mutations"); IThriftConnectionAgent agent = _connectionPool.GetChannel(iep, "RegionServer", _protocolStack, _transactionManager); if (agent == null) throw new NoConnectionException(); Exception ex = null; ThriftMessageTransaction transaction = agent.CreateTransaction(); AutoResetEvent autoResetEvent = new AutoResetEvent(false); transaction.ResponseArrived += delegate(object sender, LightSingleArgEventArgs<ThriftMessage> e) { InsertNewRowResponseMessage rspMsg = (InsertNewRowResponseMessage)e.Target; if (rspMsg.IOErrorMessage != null) ex = new IOErrorException(rspMsg.IOErrorMessage.Reason); else if (rspMsg.IllegalArgumentErrorMessage != null) ex = new IllegalArgumentException(rspMsg.IllegalArgumentErrorMessage.Reason); autoResetEvent.Set(); }; transaction.Timeout += delegate { ex = new CommunicationTimeoutException(transaction.SequenceId); autoResetEvent.Set(); }; transaction.Failed += delegate { ex = new CommunicationFailException(transaction.SequenceId); autoResetEvent.Set(); }; InsertNewRowRequestMessage reqMsg = new InsertNewRowRequestMessage { TableName = tableName, RowKey = rowkey }; reqMsg.Mutations = mutations; reqMsg.Attributes = attributes; transaction.SendRequest(reqMsg); autoResetEvent.WaitOne(); if (ex != null) throw ex; return true; }