示例#1
0
        /// <summary>
        ///    创建一个HBase表
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="descriptors">表描述符</param>
        /// <returns>如果创建成功,则返回可以操作该表的实例</returns>
        /// <exception cref="AlreadyExistsException">表已经存在</exception>
        /// <exception cref="IOErrorException">IO错误</exception>
        /// <exception cref="IllegalArgumentException">参数错误</exception>
        /// <exception cref="ArgumentNullException">参数不能为空</exception>
        /// <exception cref="CommunicationTimeoutException">通信超时</exception>
        /// <exception cref="CommunicationFailException">通信失败</exception>
        /// <exception cref="NoConnectionException">内部无任何可用的远程连接异常,这通常代表无法连接到任何一台远程服务器</exception>
        public IHTable CreateTable(string tableName, params ColumnDescriptor[] descriptors)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException("tableName");
            }
            if (descriptors == null || descriptors.Length == 0)
            {
                throw new ArgumentNullException("descriptors");
            }
            IThriftConnectionAgent agent = _connectionPool.GetChannel(_regionServers[0], "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)
            {
                CreateTableResponseMessage rspMsg = (CreateTableResponseMessage)e.Target;
                if (rspMsg.IOErrorMessage != null)
                {
                    ex = new IOErrorException(rspMsg.IOErrorMessage.Reason);
                }
                else if (rspMsg.IllegalArgumentErrorMessage != null)
                {
                    ex = new IllegalArgumentException(rspMsg.IllegalArgumentErrorMessage.Reason);
                }
                else if (rspMsg.AlreadyExistsErrorMessage != null)
                {
                    ex = new AlreadyExistsException(rspMsg.AlreadyExistsErrorMessage.Reason);
                }
                autoResetEvent.Set();
            };
            transaction.Timeout += delegate
            {
                ex = new CommunicationTimeoutException(transaction.SequenceId);
                autoResetEvent.Set();
            };
            transaction.Failed += delegate
            {
                ex = new CommunicationFailException(transaction.SequenceId);
                autoResetEvent.Set();
            };
            CreateTableRequestMessage reqMsg = new CreateTableRequestMessage {
                TableName = tableName, ColumnFamilies = descriptors
            };

            transaction.SendRequest(reqMsg);
            autoResetEvent.WaitOne();
            if (ex != null)
            {
                throw ex;
            }
            return(new HTable(reqMsg.TableName, this, _hostMappingManager));
        }
示例#2
0
 /// <summary>
 ///    创建一个HBase表
 /// </summary>
 /// <param name="tableName">表名</param>
 /// <param name="descriptors">表描述符</param>
 /// <returns>如果创建成功,则返回可以操作该表的实例</returns>
 /// <exception cref="AlreadyExistsException">表已经存在</exception>
 /// <exception cref="IOErrorException">IO错误</exception>
 /// <exception cref="IllegalArgumentException">参数错误</exception>
 /// <exception cref="ArgumentNullException">参数不能为空</exception>
 /// <exception cref="CommunicationTimeoutException">通信超时</exception>
 /// <exception cref="CommunicationFailException">通信失败</exception>
 /// <exception cref="NoConnectionException">内部无任何可用的远程连接异常,这通常代表无法连接到任何一台远程服务器</exception>
 public IHTable CreateTable(string tableName, params ColumnDescriptor[] descriptors)
 {
     if (string.IsNullOrEmpty(tableName)) throw new ArgumentNullException("tableName");
     if (descriptors == null || descriptors.Length == 0) throw new ArgumentNullException("descriptors");
     IThriftConnectionAgent agent = _connectionPool.GetChannel(_regionServers[0], "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)
     {
         CreateTableResponseMessage rspMsg = (CreateTableResponseMessage) e.Target;
         if (rspMsg.IOErrorMessage != null) ex = new IOErrorException(rspMsg.IOErrorMessage.Reason);
         else if (rspMsg.IllegalArgumentErrorMessage != null) ex = new IllegalArgumentException(rspMsg.IllegalArgumentErrorMessage.Reason);
         else if (rspMsg.AlreadyExistsErrorMessage != null) ex = new AlreadyExistsException(rspMsg.AlreadyExistsErrorMessage.Reason);
         autoResetEvent.Set();
     };
     transaction.Timeout += delegate
     {
         ex = new CommunicationTimeoutException(transaction.SequenceId);
         autoResetEvent.Set();
     };
     transaction.Failed += delegate
     {
         ex = new CommunicationFailException(transaction.SequenceId);
         autoResetEvent.Set();
     };
     CreateTableRequestMessage reqMsg = new CreateTableRequestMessage {TableName = tableName, ColumnFamilies = descriptors};
     transaction.SendRequest(reqMsg);
     autoResetEvent.WaitOne();
     if (ex != null) throw ex;
     return new HTable(reqMsg.TableName, this, _hostMappingManager);
 }