Ejemplo n.º 1
0
        public ActionResult SearchLocation()
        {
            OTSClient  _oTSClient = OTSHelper.GetOTSClientLocation(_tableStoreModel);
            PrimaryKey pk         = new PrimaryKey();

            pk.Add("d", new ColumnValue(Convert.ToInt64(Request.Form["d"])));
            pk.Add("t", new ColumnValue(TimeHelper.ConvertDateTimeToInt(Convert.ToDateTime(Request.Form["t"]))));
            GetRowRequest  getRowRequest      = new GetRowRequest("L_100000000", pk);
            GetRowResponse response           = _oTSClient.GetRow(getRowRequest);
            StringBuilder  sbAttributeColumns = new StringBuilder();

            foreach (var item in response.Attribute)
            {
                if (item.Key == "l")
                {
                    byte[] lbyte = item.Value.BinaryValue;
                    Dictionary <string, int> dictionary = ByteIntHelper.GetLocationByByte(lbyte);
                    foreach (var dic in dictionary)
                    {
                        sbAttributeColumns.Append(dic.Key + ":" + dic.Value + "; ");
                    }
                }
            }
            ViewData["pk"]  = "设备:" + Request.Form["d"] + " 时间:" + Request.Form["t"];
            ViewData["att"] = sbAttributeColumns.ToString();
            return(View("Search", ViewBag));
        }
        public void TestIntegerValueInBoundary()
        {
            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue("ABC"));
            primaryKey.Add("PK1", new ColumnValue("DEF"));
            primaryKey.Add("PK2", new ColumnValue(Int64.MaxValue));
            primaryKey.Add("PK3", new ColumnValue(456));
            SetTestConext(primaryKey: primaryKey);
            TestAllDataAPI();

            var attribute = new AttributeColumns();

            attribute.Add("Col0", new ColumnValue(Int64.MaxValue));
            SetTestConext(attribute: attribute);
            TestAllDataAPI();

            primaryKey = new PrimaryKey();
            primaryKey.Add("PK0", new ColumnValue("ABC"));
            primaryKey.Add("PK1", new ColumnValue("DEF"));
            primaryKey.Add("PK2", new ColumnValue(Int64.MinValue));
            primaryKey.Add("PK3", new ColumnValue(456));
            SetTestConext(primaryKey: primaryKey);
            TestAllDataAPI();

            attribute = new AttributeColumns();
            attribute.Add("Col0", new ColumnValue(Int64.MinValue));
            SetTestConext(attribute: attribute);
            TestAllDataAPI();
        }
Ejemplo n.º 3
0
        public static void GetRow()
        {
            Console.WriteLine("Start get row...");
            PrepareTable();
            OTSClient otsClient = Config.GetClient();

            // 定义行的主键,必须与创建表时的TableMeta中定义的一致
            PrimaryKey primaryKey = new PrimaryKey();

            primaryKey.Add("pk0", new ColumnValue(0));
            primaryKey.Add("pk1", new ColumnValue("abc"));

            GetRowRequest    request        = new GetRowRequest(TableName, primaryKey); // 未指定读哪列,默认读整行
            GetRowResponse   response       = otsClient.GetRow(request);
            PrimaryKey       primaryKeyRead = response.PrimaryKey;
            AttributeColumns attributesRead = response.Attribute;

            Console.WriteLine("Primary key read: ");
            foreach (KeyValuePair <string, ColumnValue> entry in primaryKeyRead)
            {
                Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
            }

            Console.WriteLine("Attributes read: ");
            foreach (KeyValuePair <string, ColumnValue> entry in attributesRead)
            {
                Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
            }

            Console.WriteLine("Get row succeed.");
        }
Ejemplo n.º 4
0
        public void UpdateRowTest()
        {
            CreateTable();
            var otsClient  = OTSClient;
            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue("ABC"));
            primaryKey.Add("PK1", new ColumnValue(123));
            var updateOfAttribute = new UpdateOfAttribute();

            updateOfAttribute.AddAttributeColumnToPut("NewColumn", new ColumnValue(123));
            updateOfAttribute.AddAttributeColumnToDelete("IntAttr0");

            var updateRowRequest = new UpdateRowRequest(
                "SampleTable",
                new Condition(RowExistenceExpectation.IGNORE),
                primaryKey,
                updateOfAttribute);
            var updateRowResponse = otsClient.UpdateRow(updateRowRequest);

            System.Console.WriteLine("UpdateRow CU Consumed: Read {0} Write {0}",
                                     updateRowResponse.ConsumedCapacityUnit.Read,
                                     updateRowResponse.ConsumedCapacityUnit.Write);
            DeleteTable();
        }
Ejemplo n.º 5
0
        public void GetRangeTest()
        {
            CreateTable();
            var otsClient = OTSClient;
            // 指定范围读取数据
            var startPrimaryKey = new PrimaryKey();

            startPrimaryKey.Add("PK0", new ColumnValue("TestData"));
            startPrimaryKey.Add("PK1", ColumnValue.INF_MIN);

            var endPrimaryKey = new PrimaryKey();

            endPrimaryKey.Add("PK0", new ColumnValue("TestData"));
            endPrimaryKey.Add("PK1", ColumnValue.INF_MAX);

            var consumed = new CapacityUnit(0, 0);

            var request = new GetIteratorRequest("SampleTable", GetRangeDirection.Forward,
                                                 startPrimaryKey, endPrimaryKey,
                                                 consumed);
            var iterator = OTSClient.GetRangeIterator(request);

            foreach (var rowData in iterator)
            {
                // 处理每一行数据
            }
            DeleteTable();
        }
Ejemplo n.º 6
0
        public void TestDuplicateDeleteInUpdateRow()
        {
            var pk = new PrimaryKey();

            pk.Add("PK0", new ColumnValue("123"));
            pk.Add("PK1", new ColumnValue(123));


            var updateOfAttribute = new UpdateOfAttribute();

            updateOfAttribute.AddAttributeColumnToDelete("Col0");
            updateOfAttribute.AddAttributeColumnToDelete("Col0");

            var request = new UpdateRowRequest(
                TestTableName, new Condition(RowExistenceExpectation.IGNORE), pk, updateOfAttribute);

            try {
                var response = OTSClient.UpdateRow(request);
                Assert.Fail();
            } catch (OTSServerException e) {
                AssertOTSServerException(e, new OTSServerException(
                                             "/UpdateRow",
                                             HttpStatusCode.BadRequest,
                                             "OTSParameterInvalid",
                                             "Duplicated attribute column name: 'Col0' while updating row."
                                             ));
            }
        }
Ejemplo n.º 7
0
        public void Test1000PutAnd1000DeleteInUpdateRow()
        {
            var pk = new PrimaryKey();

            pk.Add("PK0", new ColumnValue("123"));
            pk.Add("PK1", new ColumnValue(123));


            var updateOfAttribute = new UpdateOfAttribute();

            for (int i = 0; i < 1000; i++)
            {
                updateOfAttribute.AddAttributeColumnToPut("Put" + i, new ColumnValue("blah"));
                updateOfAttribute.AddAttributeColumnToDelete("Delete" + i);
            }

            var request = new UpdateRowRequest(
                TestTableName, new Condition(RowExistenceExpectation.IGNORE), pk, updateOfAttribute);

            try {
                var response = OTSClient.UpdateRow(request);
            } catch (OTSServerException e) {
                AssertOTSServerException(e, new OTSServerException(
                                             "/UpdateRow",
                                             HttpStatusCode.BadRequest,
                                             "OTSParameterInvalid",
                                             "The number of columns from the request exceeded the limit."
                                             ));
            }
        }
Ejemplo n.º 8
0
        public void TestGetRowWithFilterOneRowAndReturnCol1()
        {
            CreateTestTableWith4PK();

            var pk = new PrimaryKey();

            pk.Add("PK0", new ColumnValue("a"));
            pk.Add("PK1", new ColumnValue("aff"));
            pk.Add("PK2", new ColumnValue(10));
            pk.Add("PK3", new ColumnValue(20));

            var attribute = new AttributeColumns();

            attribute.Add("Col0", new ColumnValue(10));
            attribute.Add("Col1", new ColumnValue(2));
            attribute.Add("Col2", new ColumnValue(3));

            PutSingleRow(TestTableName, pk, attribute);

            var columnsToGet = new HashSet <string>();

            columnsToGet.Add("Col0");
            columnsToGet.Add("Col1");
            var filter = new RelationalCondition("Col0", RelationalCondition.CompareOperator.LESS_THAN, new ColumnValue(5));

            CheckSingleRow(TestTableName, pk, attribute, new CapacityUnit(1, 0), columnsToGet, isEmpty: true, condition: filter);
        }
Ejemplo n.º 9
0
        public void TestGetRowWithFilterAndLogicOrAllNotHit()
        {
            CreateTestTableWith4PK();

            var pk = new PrimaryKey();

            pk.Add("PK0", new ColumnValue("a"));
            pk.Add("PK1", new ColumnValue("a"));
            pk.Add("PK2", new ColumnValue(10));
            pk.Add("PK3", new ColumnValue(20));

            var attribute = new AttributeColumns();

            attribute.Add("Col0", new ColumnValue(10));
            attribute.Add("Col1", new ColumnValue(2));
            attribute.Add("Col2", new ColumnValue(3));

            PutSingleRow(TestTableName, pk, attribute);

            var columnsToGet = new HashSet <string>();
            var filter1      = new RelationalCondition("Col0", RelationalCondition.CompareOperator.LESS_EQUAL, new ColumnValue(5));
            var filter2      = new RelationalCondition("Col1", RelationalCondition.CompareOperator.NOT_EQUAL, new ColumnValue(2));
            var filter       = new CompositeCondition(CompositeCondition.LogicOperator.OR);

            filter.AddCondition(filter1);
            filter.AddCondition(filter2);

            CheckSingleRow(TestTableName, pk, attribute, new CapacityUnit(1, 0), columnsToGet, isEmpty: true, condition: filter);
        }
Ejemplo n.º 10
0
        public void TestString()
        {
            CreateTestTable();

            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue("ABC"));
            primaryKey.Add("PK1", new ColumnValue(123));

            var attribute = new AttributeColumns();

            attribute.Add("Col0", new ColumnValue("abcdefghijklnm"));
            var request1 = new PutRowRequest(
                "SampleTableName",
                new Condition(RowExistenceExpectation.IGNORE),
                primaryKey,
                attribute
                );

            var response1 = OTSClient.PutRow(request1);

            Assert.AreEqual(0, response1.ConsumedCapacityUnit.Read);
            Assert.AreEqual(1, response1.ConsumedCapacityUnit.Write);

            var request2 = new GetRowRequest(
                "SampleTableName",
                primaryKey
                );
            var response2 = OTSClient.GetRow(request2);

            Assert.AreEqual(1, response2.ConsumedCapacityUnit.Read);
            Assert.AreEqual(0, response2.ConsumedCapacityUnit.Write);
            AssertColumns(primaryKey, response2.PrimaryKey);
            AssertColumns(attribute, response2.Attribute);
        }
Ejemplo n.º 11
0
        public void BatchGetRowTest()
        {
            var otsClient = OTSClient;

            var primaryKey1 = new PrimaryKey();

            primaryKey1.Add("PK0", new ColumnValue("TestData"));
            primaryKey1.Add("PK1", new ColumnValue(0));

            var primaryKey2 = new PrimaryKey();

            primaryKey2.Add("PK0", new ColumnValue("TestData"));
            primaryKey2.Add("PK1", new ColumnValue(1));

            var batchGetRowRequest = new BatchGetRowRequest();
            var primaryKeys        = new List <PrimaryKey>()
            {
                primaryKey1,
                primaryKey2
            };

            batchGetRowRequest.Add("SampleTableName", primaryKeys);
            var batchGetRowRespnse = otsClient.BatchGetRow(batchGetRowRequest);

            foreach (var responseForOneTable in batchGetRowRespnse.RowDataGroupByTable)
            {
                foreach (var row in responseForOneTable.Value)
                {
                    // 处理每一行的返回
                }
            }
        }
        private void PutRow(OTSClient client, String tableName)
        {
            var primaryKey = new PrimaryKey();

            primaryKey.Add(COLUMN_GID_NAME, new ColumnValue(1));
            primaryKey.Add(COLUMN_UID_NAME, new ColumnValue(101));

            var attribute = new AttributeColumns();

            attribute.Add(COLUMN_NAME_NAME, new ColumnValue("张三"));
            attribute.Add(COLUMN_MOBILE_NAME, new ColumnValue(111111111));
            attribute.Add(COLUMN_ADDRESS_NAME, new ColumnValue("中国A地"));
            attribute.Add(COLUMN_AGE_NAME, new ColumnValue(20));

            Condition cond    = new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST);
            var       request = new PutRowRequest(tableName, cond, primaryKey, attribute);

            try
            {
                client.PutRow(request);
                Console.WriteLine("PutRow success");
            }
            catch (OTSServerException e)
            {
                Console.WriteLine("PutRow fail: {0}", e.ErrorMessage);
            }
        }
Ejemplo n.º 13
0
        public void PutRowTest()
        {
            CreateTable();
            var otsClient  = OTSClient;
            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue("ABC"));
            primaryKey.Add("PK1", new ColumnValue(123));

            var attribute = new AttributeColumns();

            attribute.Add("IntAttr0", new ColumnValue(12345));
            attribute.Add("StringAttr1", new ColumnValue("ABC"));
            attribute.Add("DoubleAttr2", new ColumnValue(3.14));
            attribute.Add("BooleanAttr3", new ColumnValue(true));

            var putRowRequest = new PutRowRequest(
                "SampleTable",
                new Condition(RowExistenceExpectation.IGNORE),
                primaryKey,
                attribute
                );

            var putRowResponse = otsClient.PutRow(putRowRequest);

            System.Console.WriteLine("PutRow CU Consumed: Read {0} Write {0}",
                                     putRowResponse.ConsumedCapacityUnit.Read,
                                     putRowResponse.ConsumedCapacityUnit.Write);
            DeleteTable();
        }
        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);
                }
            }
        }
Ejemplo n.º 15
0
Archivo: Route.cs Proyecto: hyd309/BIDO
        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);
            }
        }
Ejemplo n.º 16
0
        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);
            }
        }
        public static void ConditionDeleteRow()
        {
            Console.WriteLine("Start delete row...");

            PrepareTable();
            OTSClient otsClient = Config.GetClient();

            // 定义行的主键,必须与创建表时的TableMeta中定义的一致
            PrimaryKey primaryKey = new PrimaryKey();

            primaryKey.Add("pk0", new ColumnValue(2));
            primaryKey.Add("pk1", new ColumnValue("abc"));

            // 定义要写入改行的属性列
            AttributeColumns attribute = new AttributeColumns();

            attribute.Add("col0", new ColumnValue(2));
            attribute.Add("col1", new ColumnValue("a"));
            attribute.Add("col2", new ColumnValue(true));

            PutRowRequest putRequest = new PutRowRequest(tableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

            // 新创建一行数据
            try
            {
                otsClient.PutRow(putRequest);

                Console.WriteLine("Put row succeeded.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Put row failed. error:{0}", ex.Message);
            }

            // 当col2列的值等于true的时候,允许删除
            try
            {
                // 构造条件语句:col2列的值等于true
                var condition = new Condition(RowExistenceExpectation.EXPECT_EXIST);
                condition.ColumnCondition = new RelationalCondition("col2",
                                                                    CompareOperator.EQUAL,
                                                                    new ColumnValue(true));

                // 构造删除请求
                var deleteRequest = new DeleteRowRequest(tableName, condition, primaryKey);

                // 删除满足条件的特定行
                otsClient.DeleteRow(deleteRequest);

                Console.WriteLine("Delete row succeeded.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Delete row failed. error:{0}", ex.Message);
            }

            Console.WriteLine("Delete row succeeded.");
        }
Ejemplo n.º 18
0
        public void TestConditionExpectNotExist()
        {
            CreateTestTable();

            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue("ABC"));
            primaryKey.Add("PK1", new ColumnValue(123));

            var attribute = new AttributeColumns();

            attribute.Add("Col0", new ColumnValue(new byte[] { 0x20, 0x21, 0x23, 0x24 }));
            {
                var request1 = new PutRowRequest(
                    "SampleTableName",
                    new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST),
                    primaryKey,
                    attribute
                    );

                var response1 = OTSClient.PutRow(request1);
                Assert.AreEqual(1, response1.ConsumedCapacityUnit.Read);
                Assert.AreEqual(1, response1.ConsumedCapacityUnit.Write);

                var request2 = new GetRowRequest(
                    "SampleTableName",
                    primaryKey
                    );
                var response2 = OTSClient.GetRow(request2);
                Assert.AreEqual(1, response2.ConsumedCapacityUnit.Read);
                Assert.AreEqual(0, response2.ConsumedCapacityUnit.Write);
                AssertColumns(primaryKey, response2.PrimaryKey);
                AssertColumns(attribute, response2.Attribute);
            }
            {
                var request1 = new PutRowRequest(
                    "SampleTableName",
                    new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST),
                    primaryKey,
                    attribute
                    );

                try
                {
                    OTSClient.PutRow(request1);
                    Assert.Fail();
                }
                catch (OTSServerException e)
                {
                    Assert.AreEqual("/PutRow", e.APIName);
                    Assert.AreEqual(403, (int)e.HttpStatusCode);
                    Assert.AreEqual("OTSConditionCheckFail", e.ErrorCode);
                    Assert.AreEqual("Condition check failed.", e.ErrorMessage);
                    Assert.NotNull(e.RequestID);
                }
            }
        }
        public PrimaryKey GetPredefinedPrimaryKeyWith4PK(int index)
        {
            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue("ABC" + index));
            primaryKey.Add("PK1", new ColumnValue("DEF" + index));
            primaryKey.Add("PK2", new ColumnValue(123 + index));
            primaryKey.Add("PK3", new ColumnValue(456 + index));
            return(primaryKey);
        }
Ejemplo n.º 20
0
        public void GetRowTest()
        {
            CreateTable();
            var otsClient = OTSClient;

            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue("ABC"));
            primaryKey.Add("PK1", new ColumnValue(123));

            var attribute = new AttributeColumns();

            attribute.Add("IntAttr0", new ColumnValue(12345));
            attribute.Add("StringAttr1", new ColumnValue("ABC"));
            attribute.Add("DoubleAttr2", new ColumnValue(3.14));
            attribute.Add("BooleanAttr3", new ColumnValue(true));
            var putRowRequest = new PutRowRequest(
                "SampleTable",
                new Condition(RowExistenceExpectation.IGNORE),
                primaryKey,
                attribute
                );
            var putRowResponse = otsClient.PutRow(putRowRequest);

            var getRowRequest = new GetRowRequest(
                "SampleTable",
                primaryKey
                );
            var getRowResponse = otsClient.GetRow(getRowRequest);

            System.Console.WriteLine("GetRow CU Consumed: Read {0} Write {0}",
                                     getRowResponse.ConsumedCapacityUnit.Read,
                                     getRowResponse.ConsumedCapacityUnit.Write);

            var pk0 = getRowResponse.PrimaryKey["PK0"];

            System.Console.WriteLine("PrimaryKey PK0 Value {0}", pk0.StringValue);
            var pk1 = getRowResponse.PrimaryKey["PK1"];

            System.Console.WriteLine("PrimaryKey PK1 Value {0}", pk1.IntegerValue);
            var attr0 = getRowResponse.Attribute["IntAttr0"];

            System.Console.WriteLine("Attribute IntAttr0 Value {0}", attr0.IntegerValue);
            var attr1 = getRowResponse.Attribute["StringAttr1"];

            System.Console.WriteLine("Attribute StringAttr1 Value {0}", attr1.StringValue);
            var attr2 = getRowResponse.Attribute["DoubleAttr2"];

            System.Console.WriteLine("Attribute DoubleAttr2 Value {0}", attr2.DoubleValue);
            var attr3 = getRowResponse.Attribute["BooleanAttr3"];

            System.Console.WriteLine("Attribute BooleanAttr3 Value {0}", attr2.BooleanValue);
            DeleteTable();
        }
        public static void BatchGetRowWithFilter()
        {
            Console.WriteLine("Start batch get row with filter ...");
            PrepareTable();
            PrepareData();
            OTSClient otsClient = Config.GetClient();

            // 批量一次读10行
            BatchGetRowRequest request     = new BatchGetRowRequest();
            List <PrimaryKey>  primaryKeys = new List <PrimaryKey>();

            for (int i = 0; i < 10; i++)
            {
                PrimaryKey primaryKey = new PrimaryKey();
                primaryKey.Add("pk0", new ColumnValue(i));
                primaryKey.Add("pk1", new ColumnValue("abc"));
                primaryKeys.Add(primaryKey);
            }

            var condition = new RelationalCondition("col2",
                                                    RelationalCondition.CompareOperator.EQUAL,
                                                    new ColumnValue(false));

            request.Add(TableName, primaryKeys, null, condition);

            BatchGetRowResponse response = otsClient.BatchGetRow(request);
            var tableRows = response.RowDataGroupByTable;
            var rows      = tableRows[TableName];

            foreach (var row in rows)
            {
                // 注意:batch操作可能部分成功部分失败,需要为每行检查状态
                if (row.IsOK)
                {
                    Console.WriteLine("-----------------");
                    foreach (KeyValuePair <string, ColumnValue> entry in row.PrimaryKey)
                    {
                        Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
                    }
                    foreach (KeyValuePair <string, ColumnValue> entry in row.Attribute)
                    {
                        Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
                    }
                    Console.WriteLine("-----------------");
                }
                else
                {
                    Console.WriteLine("Read row with filter failed: " + row.ErrorMessage);
                }
            }

            Console.WriteLine("RowsCount with filter");
        }
Ejemplo n.º 22
0
        public static void GetRowWithFilter()
        {
            Console.WriteLine("Start get row with filter ...");
            PrepareTable();
            OTSClient otsClient = Config.GetClient();

            // 定义行的主键,必须与创建表时的TableMeta中定义的一致
            PrimaryKey primaryKey = new PrimaryKey();

            primaryKey.Add("pk0", new ColumnValue(0));
            primaryKey.Add("pk1", new ColumnValue("abc"));

            var rowQueryCriteria = new SingleRowQueryCriteria(TableName);

            rowQueryCriteria.RowPrimaryKey = primaryKey;

            // 只返回col0的值等于5的行或者col1不等于ff的行
            var filter1 = new RelationalCondition("col0",
                                                  RelationalCondition.CompareOperator.EQUAL,
                                                  new ColumnValue(5));

            var filter2 = new RelationalCondition("col1", RelationalCondition.CompareOperator.NOT_EQUAL, new ColumnValue("ff"));

            var filter = new CompositeCondition(CompositeCondition.LogicOperator.OR);

            filter.AddCondition(filter1);
            filter.AddCondition(filter2);

            rowQueryCriteria.Filter = filter;
            rowQueryCriteria.AddColumnsToGet("col0");
            rowQueryCriteria.AddColumnsToGet("col1");

            GetRowRequest request = new GetRowRequest(rowQueryCriteria);

            // 查询
            GetRowResponse   response       = otsClient.GetRow(request);
            PrimaryKey       primaryKeyRead = response.PrimaryKey;
            AttributeColumns attributesRead = response.Attribute;

            Console.WriteLine("Primary key read: ");
            foreach (KeyValuePair <string, ColumnValue> entry in primaryKeyRead)
            {
                Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
            }

            Console.WriteLine("Attributes read: ");
            foreach (KeyValuePair <string, ColumnValue> entry in attributesRead)
            {
                Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
            }

            Console.WriteLine("Get row with filter succeed.");
        }
        private void DeleteRow(OTSClient client, String tableName)
        {
            var primaryKey = new PrimaryKey();

            primaryKey.Add(COLUMN_GID_NAME, new ColumnValue(1));
            primaryKey.Add(COLUMN_UID_NAME, new ColumnValue(101));
            Condition        condition = new Condition(RowExistenceExpectation.IGNORE);
            DeleteRowRequest req       = new DeleteRowRequest(tableName, condition, primaryKey);

            client.DeleteRow(req);
            Console.WriteLine("DeleteRow success");
        }
        public static void BatchWriteRow()
        {
            Console.WriteLine("Start batch write row...");
            PrepareTable();
            PrepareData();
            OTSClient otsClient = Config.GetClient();

            // 一次批量导入100行数据
            var request    = new BatchWriteRowRequest();
            var rowChanges = new RowChanges();

            for (int i = 0; i < 100; i++)
            {
                PrimaryKey primaryKey = new PrimaryKey();
                primaryKey.Add("pk0", new ColumnValue(i));
                primaryKey.Add("pk1", new ColumnValue("abc"));

                // 定义要写入改行的属性列
                UpdateOfAttribute attribute = new UpdateOfAttribute();
                attribute.AddAttributeColumnToPut("col0", new ColumnValue(0));
                attribute.AddAttributeColumnToPut("col1", new ColumnValue("a"));
                attribute.AddAttributeColumnToPut("col2", new ColumnValue(true));

                rowChanges.AddUpdate(new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);
            }

            request.Add(TableName, rowChanges);

            var response  = otsClient.BatchWriteRow(request);
            var tableRows = response.TableRespones;
            var rows      = tableRows[TableName];

            int succeedRows = 0;
            int failedRows  = 0;

            foreach (var row in rows.UpdateResponses)
            {
                // 注意:batch操作可能部分成功部分失败,需要为每行检查状态
                if (row.IsOK)
                {
                    succeedRows++;
                }
                else
                {
                    Console.WriteLine("Read row failed: " + row.ErrorMessage);
                    failedRows++;
                }
            }

            Console.WriteLine("SucceedRows: " + succeedRows);
            Console.WriteLine("FailedRows: " + failedRows);
        }
        public static void GetRange()
        {
            Console.WriteLine("Start get range...");

            PrepareTable();
            PrepareData();

            OTSClient otsClient = Config.GetClient();
            // 读取 (0, INF_MIN)到(100, INF_MAX)这个范围内的所有行
            PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey();

            inclusiveStartPrimaryKey.Add("pk0", new ColumnValue(0));
            inclusiveStartPrimaryKey.Add("pk1", ColumnValue.INF_MIN);

            PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey();

            exclusiveEndPrimaryKey.Add("pk0", new ColumnValue(100));
            exclusiveEndPrimaryKey.Add("pk1", ColumnValue.INF_MAX);
            GetRangeRequest request = new GetRangeRequest(TableName, GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey);

            GetRangeResponse            response = otsClient.GetRange(request);
            IList <RowDataFromGetRange> rows     = response.RowDataList;
            PrimaryKey nextStartPrimaryKey       = response.NextPrimaryKey;

            while (nextStartPrimaryKey != null)
            {
                request             = new GetRangeRequest(TableName, GetRangeDirection.Forward, nextStartPrimaryKey, exclusiveEndPrimaryKey);
                response            = otsClient.GetRange(request);
                nextStartPrimaryKey = response.NextPrimaryKey;
                foreach (RowDataFromGetRange row in response.RowDataList)
                {
                    rows.Add(row);
                }
            }

            foreach (RowDataFromGetRange row in rows)
            {
                Console.WriteLine("-----------------");
                foreach (KeyValuePair <string, ColumnValue> entry in row.PrimaryKey)
                {
                    Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
                }
                foreach (KeyValuePair <string, ColumnValue> entry in row.Attribute)
                {
                    Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
                }
                Console.WriteLine("-----------------");
            }

            Console.WriteLine("TotalRowsRead: " + rows.Count);
        }
Ejemplo n.º 26
0
        public ActionResult SearchRoute()
        {
            OTSClient  _oTSClient = OTSHelper.GetOTSClientRoute(_tableStoreModel);
            PrimaryKey pk         = new PrimaryKey();

            pk.Add("d", new ColumnValue(Convert.ToInt64(Request.Form["d"])));
            pk.Add("s", new ColumnValue(TimeHelper.ConvertDateTimeToInt(Convert.ToDateTime(Request.Form["s"]))));
            GetRowRequest  getRowRequest      = new GetRowRequest("Route", pk);
            GetRowResponse response           = _oTSClient.GetRow(getRowRequest);
            StringBuilder  sbAttributeColumns = new StringBuilder();

            foreach (var item in response.Attribute)
            {
                switch (item.Key)
                {
                case "e":
                    sbAttributeColumns.Append(item.Key + ":" + item.Value.IntegerValue + "【" + TimeHelper.ConvertStringToDateTime(item.Value.IntegerValue.ToString()).ToString("yyyy-MM-dd HH:mm:ss fff") + "】;");
                    break;

                case "r":
                    byte[] lbyte = item.Value.BinaryValue;
                    Dictionary <string, int> dictionary = ByteIntHelper.GetRouteByByte(lbyte);
                    foreach (var dic in dictionary)
                    {
                        sbAttributeColumns.Append(dic.Key + ":" + dic.Value + "; ");
                    }
                    break;

                case "ds":
                    byte[] ds = item.Value.BinaryValue;
                    Dictionary <string, int> dsDic = ByteIntHelper.GetDurationstatsByByte(ds);
                    foreach (var dic in dsDic)
                    {
                        sbAttributeColumns.Append(dic.Key + ":" + dic.Value + "; ");
                    }
                    break;

                case "es":
                    byte[] es = item.Value.BinaryValue;
                    Dictionary <string, int> esDic = ByteIntHelper.GetEventStatsByByte(es);
                    foreach (var dic in esDic)
                    {
                        sbAttributeColumns.Append(dic.Key + ":" + dic.Value + "; ");
                    }
                    break;
                }
            }
            ViewData["pk"]  = "设备:" + Request.Form["d"] + " 开始时间:" + Request.Form["s"];
            ViewData["att"] = sbAttributeColumns.ToString();
            return(View("Search", ViewBag));
        }
Ejemplo n.º 27
0
        public void TestColumnsToGet()
        {
            CreateTestTable();

            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue("ABC"));
            primaryKey.Add("PK1", new ColumnValue(123));

            var attribute = new AttributeColumns();

            attribute.Add("Col0", new ColumnValue(123));
            attribute.Add("Col1", new ColumnValue("ABC"));
            attribute.Add("Col2", new ColumnValue(new byte[] { 0x20, 0x21, 0x23, 0x24 }));
            var request1 = new PutRowRequest(
                "SampleTableName",
                new Condition(RowExistenceExpectation.IGNORE),
                primaryKey,
                attribute
                );

            var response1 = OTSClient.PutRow(request1);

            Assert.AreEqual(0, response1.ConsumedCapacityUnit.Read);
            Assert.AreEqual(1, response1.ConsumedCapacityUnit.Write);

            var request2 = new GetRowRequest(
                "SampleTableName",
                primaryKey,
                new HashSet <string>()
            {
                "Col0", "Col2"
            }
                );
            var response2 = OTSClient.GetRow(request2);

            Assert.AreEqual(1, response2.ConsumedCapacityUnit.Read);
            Assert.AreEqual(0, response2.ConsumedCapacityUnit.Write);
            AssertColumns(new PrimaryKey(), response2.PrimaryKey);


            var attributeToExpect = new AttributeColumns();

            attributeToExpect.Add("Col0", new ColumnValue(123));
            attributeToExpect.Add("Col2", new ColumnValue(new byte[] { 0x20, 0x21, 0x23, 0x24 }));

            AssertColumns(attributeToExpect, response2.Attribute);
        }
        private void Run()
        {
            for (int i = 0; i < round; ++i)
            {
                var primaryKey = new PrimaryKey();
                primaryKey.Add("PK0", new ColumnValue(pk));
                var                 request     = new GetRowRequest(tableName, primaryKey);
                var                 response    = OTSClient.GetRow(request);
                var                 attr        = response.Attribute["Col1"];
                long                oldIntValue = attr.IntegerValue;
                ColumnValue         oldValue    = new ColumnValue(oldIntValue);
                ColumnValue         newValue    = new ColumnValue(oldIntValue + 1);
                RelationalCondition cc          = new RelationalCondition("Col1", RelationalCondition.CompareOperator.EQUAL, oldValue);
                Condition           cond        = new Condition(RowExistenceExpectation.IGNORE);

                cond.ColumnCondition = cc;
                UpdateOfAttribute updateOfAttributeForPut = new UpdateOfAttribute();
                updateOfAttributeForPut.AddAttributeColumnToPut("Col1", newValue);
                UpdateRowRequest updateReq = new UpdateRowRequest(tableName, cond, primaryKey, updateOfAttributeForPut);
                bool             success   = true;
                try
                {
                    OTSClient.UpdateRow(updateReq);
                }
                catch (OTSServerException)
                {
                    success = false;
                }
                if (success)
                {
                    ++count;
                }
            }
        }
Ejemplo n.º 29
0
        private void GetFieldList(Type type)
        {
            foreach (MemberInfo mi in type.FindMembers(MemberTypes.Property | MemberTypes.Field, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null))
            {
                string field_name = "";
                foreach (DBPrimaryKeyAttribute pk in mi.GetCustomAttributes(typeof(DBPrimaryKeyAttribute), false))
                {
                    //如果没有指定字段名称,使用MemberInfo的Name作为字段名称
                    field_name = string.IsNullOrEmpty(pk.FieldName) ? mi.Name : pk.FieldName;

                    PrimaryKey.Add(new DBPrimaryKey(mi, field_name, pk.DataType, pk.NotNull, pk.Default, pk.DBPrimaryType, string.Format("SEQ_{0}", this.AliasName)));
                }

                //如果是其他普通字段,只需要处理自己内部的属性
                if (mi.DeclaringType != type)
                {
                    continue;
                }
                foreach (DBColumnAttribute ca in mi.GetCustomAttributes(typeof(DBColumnAttribute), false))
                {
                    if (field_name == "")
                    {
                        //如果没有指定字段名称,使用MemberInfo的Name作为字段名称
                        field_name = string.IsNullOrEmpty(ca.FieldName) ? mi.Name : ca.FieldName;
                        ColumnList.Add(new DBColumn(mi, field_name, ca.DataType, ca.NotNull, ca.Default, ca.DBColumnOpType));
                    }
                }
            }
        }
        public void TestOneColumnInPK()
        {
            var primaryKeySchema = new PrimaryKeySchema();

            primaryKeySchema.Add("PK0", ColumnValueType.Integer);

            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue(123));

            var startPrimaryKey = new PrimaryKey();

            startPrimaryKey.Add("PK0", ColumnValue.INF_MIN);

            var endPrimaryKey = new PrimaryKey();

            endPrimaryKey.Add("PK0", ColumnValue.INF_MAX);

            SetTestConext(pkSchema: primaryKeySchema, primaryKey: primaryKey,
                          startPrimaryKey: startPrimaryKey, endPrimaryKey: endPrimaryKey);

            TestSingleAPI("CreateTable");
            TestSingleAPI("DescribeTable");
            WaitForTableReady();
            TestAllDataAPI(createTable: false);
        }