public void TestForError() { var schema = new PrimaryKeySchema(); schema.Add("pk1", ColumnValueType.String); CreateTestTable(TestTableName, schema, new CapacityUnit(0, 0)); var attr1 = new AttributeColumns(); attr1.Add("attr", new ColumnValue("attr_value1")); var attr2 = new AttributeColumns(); attr2.Add("attr", new ColumnValue("attr_value2")); var attr3 = new AttributeColumns(); attr3.Add("attr", new ColumnValue("attr_value3")); { var pk1 = new PrimaryKey(); pk1.Add("pk1", new ColumnValue("1")); pk1.Add("pk2", new ColumnValue("1")); var pk2 = new PrimaryKey(); pk2.Add("pk1", new ColumnValue("2")); pk2.Add("pk2", new ColumnValue("2")); var pk3 = new PrimaryKey(); pk3.Add("pk1", new ColumnValue("3")); pk3.Add("pk2", new ColumnValue("3")); { var request = new BatchWriteRowRequest(); var change = new RowChanges(); change.AddPut(new Condition(RowExistenceExpectation.IGNORE), pk1, attr1); change.AddPut(new Condition(RowExistenceExpectation.IGNORE), pk2, attr2); change.AddPut(new Condition(RowExistenceExpectation.IGNORE), pk3, attr3); request.Add(TestTableName, change); var response = OTSClient.BatchWriteRow(request); var tables = response.TableRespones; Assert.AreEqual(1, tables.Count); var rows = tables[TestTableName]; Assert.AreEqual(3, rows.PutResponses.Count); Assert.AreEqual("OTSInvalidPK", rows.PutResponses[0].ErrorCode); Assert.AreEqual("OTSInvalidPK", rows.PutResponses[1].ErrorCode); Assert.AreEqual("OTSInvalidPK", rows.PutResponses[2].ErrorCode); Assert.AreEqual("Validate PK size fail. Input: 2, Meta: 1.", rows.PutResponses[0].ErrorMessage); Assert.AreEqual("Validate PK size fail. Input: 2, Meta: 1.", rows.PutResponses[1].ErrorMessage); Assert.AreEqual("Validate PK size fail. Input: 2, Meta: 1.", rows.PutResponses[2].ErrorMessage); } } }
public void TableStoreAddLocation(DataRowCollection drs) { int batchCount = 0; int i; List <string> list = new List <string>();//存储批量上传的主键字符串,用于判断批量上传数据中是否有重复 try { RowChanges rowChanges = new RowChanges(); for (i = 0; i < drs.Count; i++) { DataRow dr = drs[i]; i++; var primaryKey = new PrimaryKey(); primaryKey.Add("d", new ColumnValue(Convert.ToInt64(dr["device_code"]))); primaryKey.Add("s", new ColumnValue(TimeHelper.ConvertDateTimeToInt(Convert.ToDateTime(dr["startTime"])))); string primarykey = dr["device_code"].ToString() + ";" + dr["startTime"].ToString(); if (list.Contains(primarykey)) { log.Info(i + "发现重复记录" + primarykey + "数据库记录:"); continue; } list.Add(primarykey); var attribute = new AttributeColumns(); attribute.Add("e", new ColumnValue(TimeHelper.ConvertDateTimeToInt(Convert.ToDateTime(dr["endTime"])))); //行驶数据 attribute.Add("r", new ColumnValue(ByteIntHelper.GetRouteByte(dr["startLongitude"], dr["startLatitude"], dr["endLongitude"], dr["endLatitude"], dr["drivingTime"], dr["mileage"], dr["topSpeed"]))); //行驶时长统计 attribute.Add("ds", new ColumnValue(ByteIntHelper.GetDurationstatsByte(dr["speedingTime"], dr["highSpeedTime"], dr["mediumSpeedTime"], dr["lowSpeedTime"], dr["idleTime"]))); //事件次数统计 attribute.Add("es", new ColumnValue(ByteIntHelper.GetEventStatsByte(dr["rapidAccelerationTimes"], dr["rapidDecelerationTimes"], dr["sharpTurnTimes"]))); rowChanges.AddPut(new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute); batchCount++; if (batchCount == 200)//200行数据进行批量提交一次 { batchRequest.Add(TableName, rowChanges); BatchWriteRowResponse batchWriteRowResponse = oTSClient.BatchWriteRow(batchRequest); BatchWriteResult(batchWriteRowResponse, i, rowChanges); rowChanges = new RowChanges(); batchRequest = new BatchWriteRowRequest(); batchCount = 0; list = new List <string>(); } } if (rowChanges.PutOperations.Count > 0) { batchRequest.Add(TableName, rowChanges); BatchWriteRowResponse batchWriteRowResponse = oTSClient.BatchWriteRow(batchRequest); BatchWriteResult(batchWriteRowResponse, i, rowChanges); } batchRequest = new BatchWriteRowRequest(); } catch (Exception ex) { log.Error(ex.Message); } }
public void TableStoreAddLocation(DataRowCollection drs) { if (drs.Count == 0) { return; } int batchCount = 0; int i = 0; List <string> list = new List <string>();//存储批量上传的主键字符串,用于判断批量上传数据中是否有重复 try { RowChanges rowChanges = new RowChanges(); foreach (DataRow dr in drs) { i++; var primaryKey = new PrimaryKey(); primaryKey.Add("d", new ColumnValue(Convert.ToInt64(dr["device_code"]))); primaryKey.Add("t", new ColumnValue(TimeHelper.ConvertDateTimeToInt(Convert.ToDateTime(dr["gps_time"])))); string primarykey = dr["device_code"].ToString() + ";" + dr["gps_time"].ToString(); if (list.Contains(primarykey)) { log.Info(i + "发现重复记录" + primarykey + "数据库记录:id = " + dr["id"]); continue; } list.Add(primarykey); var attribute = new AttributeColumns(); //定位数据 attribute.Add("l", new ColumnValue(ByteIntHelper.GetLocationByte(dr["latitude"], dr["longitude"], Convert.ToInt32(dr["speed"]), Convert.ToInt32(dr["direct"]), 0))); rowChanges.AddPut(new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute); batchCount++; if (batchCount == 200)//200行数据进行批量提交一次 { batchRequest.Add(TableName, rowChanges); BatchWriteRowResponse bwResponse = oTSClient.BatchWriteRow(batchRequest); BatchWriteResult(bwResponse, i, rowChanges); rowChanges = new RowChanges(); batchRequest = new BatchWriteRowRequest(); batchCount = 0; list = new List <string>(); } } if (rowChanges.PutOperations.Count > 0) { batchRequest.Add(TableName, rowChanges); BatchWriteRowResponse batchWriteRowResponse = oTSClient.BatchWriteRow(batchRequest); BatchWriteResult(batchWriteRowResponse, i, rowChanges); } batchRequest = new BatchWriteRowRequest(); } catch (Exception ex) { log.Error(ex.Message); } }
private void BatchWriteRow() { var otsClient = OTSClient; var primaryKey1 = new PrimaryKey { { "PK0", new ColumnValue("TestData") }, { "PK1", new ColumnValue(0) } }; var attribute1 = new AttributeColumns { { "Col0", new ColumnValue("Hangzhou") } }; var primaryKey2 = new PrimaryKey { { "PK0", new ColumnValue("TestData") }, { "PK1", new ColumnValue(1) } }; var attribute2 = new AttributeColumns { { "Col0", new ColumnValue("Shanghai") } }; var rowChanges = new RowChanges(TestTableName); rowChanges.AddPut(new Condition(RowExistenceExpectation.IGNORE), primaryKey1, attribute1); rowChanges.AddPut(new Condition(RowExistenceExpectation.IGNORE), primaryKey2, attribute2); var batchWriteRowRequest = new BatchWriteRowRequest(); batchWriteRowRequest.Add(TestTableName, rowChanges); var batchWriteRowResponse = otsClient.BatchWriteRow(batchWriteRowRequest); foreach (var responseForOneTable in batchWriteRowResponse.TableRespones) { foreach (var row in responseForOneTable.Value.Responses) { Console.WriteLine(row); } } }
public void BatchWriteRowTest() { var otsClient = OTSClient; var primaryKey1 = new PrimaryKey(); primaryKey1.Add("PK0", new ColumnValue("TestData")); primaryKey1.Add("PK1", new ColumnValue(0)); var attribute1 = new AttributeColumns(); attribute1.Add("Col0", new ColumnValue("Hangzhou")); var primaryKey2 = new PrimaryKey(); primaryKey2.Add("PK0", new ColumnValue("TestData")); primaryKey2.Add("PK1", new ColumnValue(1)); var attribute2 = new AttributeColumns(); attribute2.Add("Col0", new ColumnValue("Shanghai")); var rowChanges = new RowChanges(); rowChanges.AddPut(new Condition(RowExistenceExpectation.IGNORE), primaryKey1, attribute1); rowChanges.AddPut(new Condition(RowExistenceExpectation.IGNORE), primaryKey2, attribute2); var batchWriteRowRequest = new BatchWriteRowRequest(); batchWriteRowRequest.Add("SampleTableName", rowChanges); var batchWriteRowResponse = otsClient.BatchWriteRow(batchWriteRowRequest); foreach (var responseForOneTable in batchWriteRowResponse.TableRespones) { foreach (var row in responseForOneTable.Value.PutResponses) { // 处理每一行的返回 } } }
public void PutPredefinedRows(int count) { int index = 0; while (count > 0) { var rowChanges = new RowChanges(); for (int i = 0; i < (count > 100 ? 100 : count); i++) { rowChanges.AddPut(new Condition(RowExistenceExpectation.IGNORE), GetPredefinedPrimaryKeyWith4PK(index + i), GetPredefinedAttributeWith5PK(index + i) ); } var request = new BatchWriteRowRequest(); request.Add(TestTableName, rowChanges); OTSClient.BatchWriteRow(request); count -= 100; index += 100; } }
public void TestAPIWithParameter(string apiName) { var tableName = TestContext.tableName; var pkSchema = TestContext.pkSchema; var reservedThroughput = TestContext.reservedThroughput; var primaryKey = TestContext.primaryKey; var attribute = TestContext.attribute; var condition = TestContext.condition; var startPrimaryKey = TestContext.startPrimaryKey; var endPrimaryKey = TestContext.endPrimaryKey; var putRowConsumed = TestContext.putRowConsumed; var getRowConsumed = TestContext.getRowConsumed; var updateRowConsumed = TestContext.updateRowConsumed; var deleteRowConsumed = TestContext.deleteRowConsumed; var getRangeConsumed = TestContext.getRangeConsumed; var updateOfAttributeForPut = TestContext.updateOfAttributeForPut; var updateOfAttributeForDelete = TestContext.updateOfAttributeForDelete; var columnsToGet = TestContext.columnsToGet; var limit = TestContext.limit; var direction = TestContext.direction; var tableMeta = new TableMeta(tableName, pkSchema); switch (apiName) { case "CreateTable": var request0 = new CreateTableRequest(tableMeta, reservedThroughput); OTSClient.CreateTable(request0); return; case "ListTable": var request1 = new ListTableRequest(); var response1 = OTSClient.ListTable(request1); Assert.AreEqual(new List <string>() { tableName }, response1.TableNames); return; case "UpdateTable": var request2 = new UpdateTableRequest(tableName, reservedThroughput); var response2 = OTSClient.UpdateTable(request2); if (reservedThroughput.Read.HasValue && reservedThroughput.Write.HasValue) { AssertCapacityUnit( reservedThroughput, response2.ReservedThroughputDetails.CapacityUnit); } Assert.IsTrue(response2.ReservedThroughputDetails.LastDecreaseTime >= 0); Assert.IsTrue(response2.ReservedThroughputDetails.LastIncreaseTime >= 0); Assert.IsTrue(response2.ReservedThroughputDetails.NumberOfDecreasesToday >= 0); return; case "DeleteTable": var request3 = new DeleteTableRequest(tableName); OTSClient.DeleteTable(request3); var request31 = new ListTableRequest(); var response31 = OTSClient.ListTable(request31); Assert.AreEqual(new List <string>() { }, response31.TableNames); return; case "DescribeTable": var request4 = new DescribeTableRequest(tableName); var response4 = OTSClient.DescribeTable(request4); Assert.AreEqual(tableName, response4.TableMeta.TableName); AssertPrimaryKeySchema(pkSchema, response4.TableMeta.PrimaryKeySchema); AssertCapacityUnit(reservedThroughput, response4.ReservedThroughputDetails.CapacityUnit); Assert.IsTrue(response4.ReservedThroughputDetails.LastDecreaseTime >= 0); Assert.IsTrue(response4.ReservedThroughputDetails.LastIncreaseTime >= 0); Assert.IsTrue(response4.ReservedThroughputDetails.NumberOfDecreasesToday >= 0); return; case "PutRow": var request5 = new PutRowRequest(tableName, condition, primaryKey, attribute); var response5 = OTSClient.PutRow(request5); AssertCapacityUnit(putRowConsumed, response5.ConsumedCapacityUnit); return; case "GetRow": var request6 = new GetRowRequest(tableName, primaryKey, columnsToGet); var response6 = OTSClient.GetRow(request6); AssertPrimaryKey(primaryKey, response6.PrimaryKey, columnsToGet); AssertAttribute(attribute, response6.Attribute, columnsToGet); AssertCapacityUnit(getRowConsumed, response6.ConsumedCapacityUnit); return; case "DeleteRow": var request7 = new DeleteRowRequest(tableName, condition, primaryKey); var response7 = OTSClient.DeleteRow(request7); AssertCapacityUnit(deleteRowConsumed, response7.ConsumedCapacityUnit); var request71 = new GetRowRequest(tableName, primaryKey); var response71 = OTSClient.GetRow(request71); AssertPrimaryKey(new PrimaryKey(), response71.PrimaryKey); AssertAttribute(new AttributeColumns(), response71.Attribute); return; case "UpdateRow_Put": var request8 = new UpdateRowRequest(tableName, condition, primaryKey, updateOfAttributeForPut); var response8 = OTSClient.UpdateRow(request8); AssertCapacityUnit(updateRowConsumed, response8.ConsumedCapacityUnit); var request81 = new GetRowRequest(tableName, primaryKey); var response81 = OTSClient.GetRow(request81); AssertPrimaryKey(primaryKey, response81.PrimaryKey); AssertAttribute(attribute, response81.Attribute); AssertCapacityUnit(getRowConsumed, response81.ConsumedCapacityUnit); return; case "UpdateRow_Delete": var request9 = new UpdateRowRequest(tableName, condition, primaryKey, updateOfAttributeForDelete); var response9 = OTSClient.UpdateRow(request9); AssertCapacityUnit(deleteRowConsumed, response9.ConsumedCapacityUnit); var request91 = new GetRowRequest(tableName, primaryKey); var response91 = OTSClient.GetRow(request91); // Don't assert primary key AssertAttribute(new AttributeColumns(), response91.Attribute); return; case "BatchGetRow": var request11 = new BatchGetRowRequest(); request11.Add(tableName, new List <PrimaryKey>() { primaryKey }, columnsToGet); var response11 = OTSClient.BatchGetRow(request11); Assert.AreEqual(1, response11.RowDataGroupByTable.Count); Assert.IsTrue(response11.RowDataGroupByTable.ContainsKey(tableName)); Assert.AreEqual(1, response11.RowDataGroupByTable[tableName].Count); if (!response11.RowDataGroupByTable[tableName][0].IsOK) { throw new OTSServerException(apiName, HttpStatusCode.OK, response11.RowDataGroupByTable[tableName][0].ErrorCode, response11.RowDataGroupByTable[tableName][0].ErrorMessage); } AssertPrimaryKey(primaryKey, response11.RowDataGroupByTable[tableName][0].PrimaryKey); AssertAttribute(attribute, response11.RowDataGroupByTable[tableName][0].Attribute); AssertCapacityUnit(getRowConsumed, response11.RowDataGroupByTable[tableName][0].Consumed); return; case "BatchWriteRow_Put": var request12 = new BatchWriteRowRequest(); var rowChanges = new RowChanges(); rowChanges.AddPut(condition, primaryKey, attribute); request12.Add(tableName, rowChanges); var response12 = OTSClient.BatchWriteRow(request12); Assert.AreEqual(1, response12.TableRespones.Count); Assert.IsTrue(response12.TableRespones.ContainsKey(tableName)); Assert.AreEqual(1, response12.TableRespones[tableName].PutResponses.Count); Assert.AreEqual(0, response12.TableRespones[tableName].UpdateResponses.Count); Assert.AreEqual(0, response12.TableRespones[tableName].DeleteResponses.Count); if (response12.TableRespones[tableName].PutResponses[0].IsOK) { AssertCapacityUnit(putRowConsumed, response12.TableRespones[tableName].PutResponses[0].Consumed); } else { throw new OTSServerException("/BatchWriteRow", HttpStatusCode.OK, response12.TableRespones[tableName].PutResponses[0].ErrorCode, response12.TableRespones[tableName].PutResponses[0].ErrorMessage); } var request121 = new GetRowRequest(tableName, primaryKey); var response121 = OTSClient.GetRow(request121); AssertPrimaryKey(primaryKey, response121.PrimaryKey); AssertAttribute(attribute, response121.Attribute); AssertCapacityUnit(getRowConsumed, response121.ConsumedCapacityUnit); return; case "BatchWriteRow_Update": var request13 = new BatchWriteRowRequest(); var rowChanges2 = new RowChanges(); rowChanges2.AddUpdate(condition, primaryKey, updateOfAttributeForPut); request13.Add(tableName, rowChanges2); var response13 = OTSClient.BatchWriteRow(request13); Assert.AreEqual(1, response13.TableRespones.Count); Assert.IsTrue(response13.TableRespones.ContainsKey(tableName)); Assert.AreEqual(0, response13.TableRespones[tableName].PutResponses.Count); Assert.AreEqual(1, response13.TableRespones[tableName].UpdateResponses.Count); Assert.AreEqual(0, response13.TableRespones[tableName].DeleteResponses.Count); if (response13.TableRespones[tableName].UpdateResponses[0].IsOK) { AssertCapacityUnit(updateRowConsumed, response13.TableRespones[tableName].UpdateResponses[0].Consumed); } else { throw new OTSServerException("/BatchWriteRow", HttpStatusCode.OK, response13.TableRespones[tableName].UpdateResponses[0].ErrorCode, response13.TableRespones[tableName].UpdateResponses[0].ErrorMessage); } var request131 = new GetRowRequest(tableName, primaryKey); var response131 = OTSClient.GetRow(request131); AssertPrimaryKey(primaryKey, response131.PrimaryKey); AssertAttribute(attribute, response131.Attribute); AssertCapacityUnit(getRowConsumed, response131.ConsumedCapacityUnit); return; case "BatchWriteRow_Delete": var request14 = new BatchWriteRowRequest(); var rowChanges3 = new RowChanges(); rowChanges3.AddDelete(condition, primaryKey); request14.Add(tableName, rowChanges3); var response14 = OTSClient.BatchWriteRow(request14); Assert.AreEqual(1, response14.TableRespones.Count); Assert.IsTrue(response14.TableRespones.ContainsKey(tableName)); Assert.AreEqual(0, response14.TableRespones[tableName].PutResponses.Count); Assert.AreEqual(0, response14.TableRespones[tableName].UpdateResponses.Count); Assert.AreEqual(1, response14.TableRespones[tableName].DeleteResponses.Count); if (response14.TableRespones[tableName].DeleteResponses[0].IsOK) { AssertCapacityUnit(deleteRowConsumed, response14.TableRespones[tableName].DeleteResponses[0].Consumed); } else { throw new OTSServerException("/BatchWriteRow", HttpStatusCode.OK, response14.TableRespones[tableName].DeleteResponses[0].ErrorCode, response14.TableRespones[tableName].DeleteResponses[0].ErrorMessage); } var request141 = new GetRowRequest(tableName, primaryKey); var response141 = OTSClient.GetRow(request141); AssertPrimaryKey(new PrimaryKey(), response141.PrimaryKey); AssertAttribute(new AttributeColumns(), response141.Attribute); return; case "GetRange": var request15 = new GetRangeRequest(tableName, direction, startPrimaryKey, endPrimaryKey, columnsToGet, limit); var response15 = OTSClient.GetRange(request15); Assert.AreEqual(1, response15.RowDataList.Count); Assert.AreEqual(null, response15.NextPrimaryKey); AssertCapacityUnit(getRangeConsumed, response15.ConsumedCapacityUnit); AssertPrimaryKey(primaryKey, response15.RowDataList[0].PrimaryKey, columnsToGet); AssertAttribute(attribute, response15.RowDataList[0].Attribute, columnsToGet); return; default: throw new Exception(String.Format("invalid api name: {0}", apiName)); } }
public void TestBatchWriteRow() { String tableName = "condition_update_test_table"; CreateTable(tableName); bool success = PutRow(tableName, 20, "Col1", new ColumnValue("Value20"), null); Assert.IsTrue(success); success = PutRow(tableName, 21, "Col1", new ColumnValue("Value21"), null); Assert.IsTrue(success); success = PutRow(tableName, 22, "Col1", new ColumnValue("Value22"), null); Assert.IsTrue(success); var primaryKey = new PrimaryKey { { "PK0", new ColumnValue(20) } }; var attribute = new AttributeColumns { { "Col2", new ColumnValue("Value2") } }; Condition cond1 = new Condition(RowExistenceExpectation.IGNORE) { ColumnCondition = new RelationalCondition( "Col1", CompareOperator.NOT_EQUAL, new ColumnValue("Value20")) }; var updatePrimaryKey = new PrimaryKey { { "PK0", new ColumnValue(21) } }; UpdateOfAttribute update2 = new UpdateOfAttribute(); update2.AddAttributeColumnToPut("Col3", new ColumnValue("Value3")); Condition cond2 = new Condition(RowExistenceExpectation.IGNORE) { ColumnCondition = new RelationalCondition( "Col1", CompareOperator.EQUAL, new ColumnValue("Value21")) }; var deletePrimaryKey = new PrimaryKey { { "PK0", new ColumnValue(22) } }; Condition cond3 = new Condition(RowExistenceExpectation.IGNORE) { ColumnCondition = new RelationalCondition( "Col1", CompareOperator.GREATER_THAN, new ColumnValue("Value22")) }; var batchWriteRequest = new BatchWriteRowRequest(); RowChanges changes = new RowChanges(tableName); changes.AddPut(cond1, primaryKey, attribute); changes.AddUpdate(cond2, updatePrimaryKey, update2); changes.AddDelete(cond3, deletePrimaryKey); batchWriteRequest.Add(tableName, changes); var response = OTSClient.BatchWriteRow(batchWriteRequest); var tables = response.TableRespones; Assert.AreEqual(1, tables.Count); var rows = tables[tableName]; Assert.IsFalse(rows.Responses[0].IsOK); }
public void TestForNormal() { var schema = new PrimaryKeySchema(); schema.Add("pk1", ColumnValueType.String); CreateTestTable(TestTableName, schema, new CapacityUnit(0, 0)); var attr1 = new AttributeColumns(); attr1.Add("attr", new ColumnValue("attr_value1")); var attr2 = new AttributeColumns(); attr2.Add("attr", new ColumnValue("attr_value2")); var attr3 = new AttributeColumns(); attr3.Add("attr", new ColumnValue("attr_value3")); { List <PrimaryKey> primaryKeys = new List <PrimaryKey>(); var pk1 = new PrimaryKey(); pk1.Add("pk1", new ColumnValue("1")); var pk2 = new PrimaryKey(); pk2.Add("pk1", new ColumnValue("2")); var pk3 = new PrimaryKey(); pk3.Add("pk1", new ColumnValue("3")); primaryKeys.Add(pk1); primaryKeys.Add(pk2); primaryKeys.Add(pk3); { var request = new BatchWriteRowRequest(); var change = new RowChanges(); change.AddPut(new Condition(RowExistenceExpectation.IGNORE), pk1, attr1); change.AddPut(new Condition(RowExistenceExpectation.IGNORE), pk2, attr2); change.AddPut(new Condition(RowExistenceExpectation.IGNORE), pk3, attr3); request.Add(TestTableName, change); var response = OTSClient.BatchWriteRow(request); } { var request = new BatchGetRowRequest(); request.Add(TestTableName, primaryKeys); var response = OTSClient.BatchGetRow(request); var tables = response.RowDataGroupByTable; Assert.AreEqual(1, tables.Count); var rows = tables[TestTableName]; Assert.AreEqual(3, rows.Count); AssertPrimaryKey(pk1, rows[0].PrimaryKey); AssertPrimaryKey(pk2, rows[1].PrimaryKey); AssertPrimaryKey(pk3, rows[2].PrimaryKey); AssertAttribute(attr1, rows[0].Attribute); AssertAttribute(attr2, rows[1].Attribute); AssertAttribute(attr3, rows[2].Attribute); } } }
public void TableStoreAddLocation(DataRowCollection drs) { int batchCount = 0; int i = 0; List <string> list = new List <string>();//存储批量上传的主键字符串,用于判断批量上传数据中是否有重复 try { RowChanges rowChanges = new RowChanges(); foreach (DataRow dr in drs) { i++; int alarm_no = dr["alarm_no"] == null ? -1 : Convert.ToInt32(dr["alarm_no"]); if (alarm_no == 202 && dr["alarm_parameter"].ToString() == "0") { //【逻辑判断】当事件id=202且参数=0则设置ID=212 alarm_no = 212; } else if (alarm_no == 200) { if (dr["alarm_parameter"].ToString() == "3") { //【逻辑判断】当事件id=200且参数=3则设置ID=25 alarm_no = 25; } else { //【逻辑判断】当事件id=200且参数=4则设置ID=26 alarm_no = 26; } } string primarykey = dr["device_code"].ToString() + ";" + dr["alarm_time"].ToString() + ";" + alarm_no; if (list.Contains(primarykey)) { log.Info(i + "发现重复记录" + primarykey + "数据库记录:id=" + dr["id"]); continue; } list.Add(primarykey); var primaryKey = new PrimaryKey(); primaryKey.Add("d", new ColumnValue(Convert.ToInt64(dr["device_code"]))); primaryKey.Add("et", new ColumnValue(TimeHelper.ConvertDateTimeToInt(Convert.ToDateTime(dr["alarm_time"])))); //事件事件(毫秒) primaryKey.Add("ei", new ColumnValue(ByteIntHelper.intToBytes2(alarm_no, 1))); //事件ID var attribute = new AttributeColumns(); #region 事件参数暂时不传递,默认空 //if (dr["alarm_parameter"] != null)//事件参数 不为空才添加 //{ // byte[] temp= ByteIntHelper.intToBytes(Convert.ToInt32(dr["alarm_parameter"]), 4); // int startIndex = 0; // foreach (var item in temp) // { // if (Convert.ToInt32(item) == 0) // { // startIndex++; // continue; // } // else { // break; // } // } // byte[] byteAlarmNo = new byte[4-startIndex]; // for (int index = 0; index < byteAlarmNo.Length; index++) // { // byteAlarmNo[index] = temp[startIndex+index]; // } // attribute.Add("ep", new ColumnValue(byteAlarmNo)); //事件参数 //} #endregion //定位时间 attribute.Add("t", new ColumnValue(TimeHelper.ConvertDateTimeToInt(Convert.ToDateTime(dr["create_time"])))); //定位数据 attribute.Add("l", new ColumnValue(ByteIntHelper.GetLocationByte(dr["latitude"], dr["longitude"], Convert.ToInt32(dr["speed"]), Convert.ToInt32(dr["direct"]), 0))); rowChanges.AddPut(new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute); batchCount++; if (batchCount == 200)//200行数据进行批量提交一次 { batchRequest.Add(TableName, rowChanges); BatchWriteRowResponse bwResponse = oTSClient.BatchWriteRow(batchRequest); BatchWriteResult(bwResponse, i, rowChanges); rowChanges = new RowChanges(); batchRequest = new BatchWriteRowRequest(); batchCount = 0; list = new List <string>(); } } if (rowChanges.PutOperations.Count > 0) { batchRequest.Add(TableName, rowChanges); BatchWriteRowResponse batchWriteRowResponse = oTSClient.BatchWriteRow(batchRequest); BatchWriteResult(batchWriteRowResponse, i, rowChanges); } batchRequest = new BatchWriteRowRequest(); } catch (Exception ex) { log.Error(ex.Message); } }
public static void ConditionBatchWriteRow() { Console.WriteLine("Start batch write row..."); PrepareTable(); OTSClient otsClient = Config.GetClient(); // 定义行的主键,必须与创建表时的TableMeta中定义的一致 PrimaryKey primaryKey = new PrimaryKey(); primaryKey.Add("pk0", new ColumnValue(3)); primaryKey.Add("pk1", new ColumnValue("abc")); // 定义要写入改行的属性列 AttributeColumns attribute = new AttributeColumns(); attribute.Add("col0", new ColumnValue(0)); attribute.Add("col1", new ColumnValue("a")); attribute.Add("col2", new ColumnValue(true)); PutRowRequest request = new PutRowRequest(tableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute); // 新创建一行数据 try { otsClient.PutRow(request); Console.WriteLine("Put row succeeded."); } catch (Exception ex) { Console.WriteLine("Put row failed. error:{0}", ex.Message); } // 当col0列的值不等于5的时候,允许再次put row,覆盖掉原值,预期成功 try { // 构造条件语句:col0列的值不等于5 var condition = new Condition(RowExistenceExpectation.IGNORE); condition.ColumnCondition = new RelationalCondition("col0", CompareOperator.NOT_EQUAL, new ColumnValue(5)); // 构造col2列的值 var attr1 = new AttributeColumns(); attr1.Add("col2", new ColumnValue(false)); // 构造批量写请求 var rowChange = new RowChanges(tableName); rowChange.AddPut(condition, primaryKey, attr1); var batchWriteRequest = new BatchWriteRowRequest(); batchWriteRequest.Add(tableName, rowChange); // 批量写数据 otsClient.BatchWriteRow(batchWriteRequest); Console.WriteLine("Batch write row succeeded."); } catch (Exception ex) { Console.WriteLine("Batch write row failed. error:{0}", ex.Message); } }
public void TableStoreAddLocation(DataRowCollection drs) { if (drs.Count == 0) { return; } int batchCount = 0; int i = 0; List <string> list = new List <string>();//存储批量上传的主键字符串,用于判断批量上传数据中是否有重复 try { RowChanges rowChanges = new RowChanges(); foreach (DataRow dr in drs) { var primaryKey = new PrimaryKey(); primaryKey.Add("d", new ColumnValue(Convert.ToInt64(dr["device_code"]))); primaryKey.Add("t", new ColumnValue(TimeHelper.ConvertDateTimeToInt(Convert.ToDateTime(dr["gps_time"])))); string primarykey = dr["device_code"].ToString() + ";" + dr["gps_time"].ToString(); if (list.Contains(primarykey)) { log.Info("发现重复记录" + primarykey + "数据库记录:"); continue; } list.Add(primarykey); var attribute = new AttributeColumns(); //定位数据 attribute.Add("l", new ColumnValue(ByteIntHelper.GetLocationByte(dr["latitude"], dr["longitude"], Convert.ToInt32(dr["speed"]), Convert.ToInt32(dr["direct"]), 0))); rowChanges.AddPut(new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute); batchCount++; i++; if (batchCount == 200)//200行数据进行批量提交一次 { batchRequest.Add(TableName, rowChanges); BatchWriteRowResponse bwResponse = oTSClient.BatchWriteRow(batchRequest); if (bwResponse.IsAllSucceed) { //所有数据插入成功 log.Info(i + "批量提交成功"); } else { log.Error(i + "批量提交有出错记录"); //把批量提交的数据,赋值给新的list,进行异常插入处理方法 //分析具体是哪个index行数据出错 var tableRows = bwResponse.TableRespones; var rows = tableRows[TableName]; for (int j = 0; j < rows.PutResponses.Count; j++) { if (rows.PutResponses[j].IsOK) { } else { log.Error("批量提交 问题数据在第【" + j + "】行"); Task task = new Task(() => { var errorRow = rowChanges.PutOperations[j]; //异步处理错误数据行 PutErrorHandle putError = new PutErrorLocation(); putError.HandlePutError(errorRow, TableName, log); }); task.Start(); } } } rowChanges = new RowChanges(); batchRequest = new BatchWriteRowRequest(); batchCount = 0; list = new List <string>(); } } if (rowChanges.PutOperations.Count > 0) { batchRequest.Add("L_100000000", rowChanges); oTSClient.BatchWriteRow(batchRequest); log.Info(i + "批量提交成功"); } batchRequest = new BatchWriteRowRequest(); } catch (Exception ex) { log.Error(ex.Message); } }