/// <summary> /// This request is used to execute a batch of updates against a PreparedStatement. /// </summary> public async Task <ExecuteBatchResponse> ExecuteBatchRequestAsync(string connectionId, uint statementId, pbc::RepeatedField <UpdateBatch> updates) { ExecuteBatchRequest req = new ExecuteBatchRequest { ConnectionId = connectionId, StatementId = statementId }; req.Updates.AddRange(updates); WireMessage msg = new WireMessage { Name = Constants.WireMessagePrefix + "ExecuteBatchRequest", WrappedMessage = req.ToByteString() }; using (Response webResponse = await PostRequestAsync(msg.ToByteArray())) { if (webResponse.WebResponse.StatusCode != HttpStatusCode.OK) { WireMessage output = WireMessage.Parser.ParseFrom(webResponse.WebResponse.GetResponseStream()); ErrorResponse res = ErrorResponse.Parser.ParseFrom(output.WrappedMessage); throw new WebException( string.Format( "ExecuteBatchRequestAsync failed! connectionId: {0}, Response code was: {1}, Response body was: {2}", connectionId, webResponse.WebResponse.StatusCode, res.ToString())); } else { WireMessage output = WireMessage.Parser.ParseFrom(webResponse.WebResponse.GetResponseStream()); ExecuteBatchResponse res = ExecuteBatchResponse.Parser.ParseFrom(output.WrappedMessage); return(res); } } }
public void ManyRowBatchInsertTest() { var client = new PhoenixClient(_credentials); string connId = GenerateRandomConnId(); RequestOptions options = RequestOptions.GetGatewayDefaultOptions(); // In gateway mode, url format will be https://<cluster dns name>.azurehdinsight.net/hbasephoenix<N>/ // Requests sent to hbasephoenix0/ will be forwarded to PQS on workernode0 options.AlternativeEndpoint = "hbasephoenix0/"; string tableName = "Persons" + connId; OpenConnectionResponse openConnResponse = null; CreateStatementResponse createStatementResponse = null; try { // Opening connection pbc::MapField <string, string> info = new pbc::MapField <string, string>(); openConnResponse = client.OpenConnectionRequestAsync(connId, info, options).Result; // Syncing connection ConnectionProperties connProperties = new ConnectionProperties { HasAutoCommit = true, AutoCommit = true, HasReadOnly = true, ReadOnly = false, TransactionIsolation = 0, Catalog = "", Schema = "", IsDirty = true }; client.ConnectionSyncRequestAsync(connId, connProperties, options).Wait(); // Creating statement createStatementResponse = client.CreateStatementRequestAsync(connId, options).Result; // Running query 1 string sql1 = "CREATE TABLE " + tableName + " (LastName varchar(255) PRIMARY KEY,FirstName varchar(255))"; ExecuteResponse execResponse1 = client.PrepareAndExecuteRequestAsync(connId, sql1, createStatementResponse.StatementId, long.MaxValue, int.MaxValue, options).Result; // Running query 2 // Batching two sqls in a single HTTP request pbc::RepeatedField <string> list = new pbc.RepeatedField <string>(); list.Add("UPSERT INTO " + tableName + " VALUES('d1','x1')"); list.Add("UPSERT INTO " + tableName + " VALUES('d2','x2')"); ExecuteBatchResponse execResponse2 = client.PrepareAndExecuteBatchRequestAsync(connId, createStatementResponse.StatementId, list, options).Result; // Running query 3 string sql3 = "select count(*) from " + tableName; ExecuteResponse execResponse3 = client.PrepareAndExecuteRequestAsync(connId, sql3, createStatementResponse.StatementId, long.MaxValue, int.MaxValue, options).Result; long count3 = execResponse3.Results[0].FirstFrame.Rows[0].Value[0].ScalarValue.NumberValue; Assert.AreEqual(2, count3); // Running query 4 // Batching two sqls in a single HTTP request // Creating statement 2 string sql4 = "UPSERT INTO " + tableName + " VALUES (?,?)"; PrepareResponse prepareResponse = client.PrepareRequestAsync(connId, sql4, int.MaxValue, options).Result; StatementHandle statementHandle = prepareResponse.Statement; pbc::RepeatedField <UpdateBatch> updates = new pbc.RepeatedField <UpdateBatch>(); for (int i = 3; i < 10; i++) { pbc::RepeatedField <TypedValue> parameterValues = new pbc.RepeatedField <TypedValue>(); TypedValue v1 = new TypedValue { StringValue = "d" + i, Type = Rep.String }; TypedValue v2 = new TypedValue { StringValue = "x" + i, Type = Rep.String }; parameterValues.Add(v1); parameterValues.Add(v2); UpdateBatch batch = new UpdateBatch { ParameterValues = parameterValues }; updates.Add(batch); } ExecuteBatchResponse execResponse4 = client.ExecuteBatchRequestAsync(connId, statementHandle.Id, updates, options).Result; // Running query 5 string sql5 = "select count(*) from " + tableName; ExecuteResponse execResponse5 = client.PrepareAndExecuteRequestAsync(connId, sql5, createStatementResponse.StatementId, long.MaxValue, int.MaxValue, options).Result; long count5 = execResponse5.Results[0].FirstFrame.Rows[0].Value[0].ScalarValue.NumberValue; Assert.AreEqual(9, count5); // Running query 5 string sql6 = "DROP TABLE " + tableName; client.PrepareAndExecuteRequestAsync(connId, sql6, createStatementResponse.StatementId, long.MaxValue, int.MaxValue, options).Wait(); } catch (Exception ex) { Assert.Fail(ex.Message); } finally { if (createStatementResponse != null) { client.CloseStatementRequestAsync(connId, createStatementResponse.StatementId, options).Wait(); createStatementResponse = null; } if (openConnResponse != null) { client.CloseConnectionRequestAsync(connId, options).Wait(); openConnResponse = null; } } }