Пример #1
0
        public void LoadsOnLoad()
        {
            MockRepository mockRepository = new MockRepository();
            IHBaseConnection connection = mockRepository.Stub<IHBaseConnection>();
            IHBaseRowData rowData = mockRepository.Stub<IHBaseRowData>();
            IHBaseCellData cellData = mockRepository.Stub<IHBaseCellData>();

            byte[] tableName = Encoding.UTF8.GetBytes("t");
            byte[] rowKey = Encoding.UTF8.GetBytes("r");
            byte[] columnName = Encoding.UTF8.GetBytes("c");

            using (mockRepository.Record())
            {
                SetupResult.For(rowData.Key).Return(rowKey);
                SetupResult.For(rowData.Columns).Return(new Dictionary<byte[], IList<IHBaseCellData>> { { columnName, new List<IHBaseCellData> { cellData } } });
                SetupResult.For(connection.GetRow(tableName, rowKey)).Return(rowData);
            }

            using (mockRepository.Playback())
            {
                HBaseDatabase db = new HBaseDatabase(connection);
                HBaseRow row = new HBaseRow(Encoding.UTF8.GetBytes("r"), new HBaseTable(Encoding.UTF8.GetBytes("t"), db));
                row.Load();

                Assert.Equal(1, row.Columns.Keys.Count);
            }
        }
Пример #2
0
        public void RowSerialization()
        {
            var row = new HBaseRow
            {
                Key = "row key"
            };

            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family1", "column1"),
                Timestamp = 325,
                Data      = Encoding.UTF8.GetBytes("hello world")
            });
            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family2", "column2"),
                Timestamp = 1015,
                Data      = Encoding.UTF8.GetBytes("goodbye world")
            });

            var json        = HBaseSerializationHelper.SerializeObject(row);
            var compactJson = Regex.Replace(json, @"\s+", string.Empty);

            Assert.Equal("{\"key\":\"cm93IGtleQ==\",\"Cell\":[{\"column\":\"ZmFtaWx5MTpjb2x1bW4x\",\"timestamp\":325,\"$\":\"aGVsbG8gd29ybGQ=\"},{\"column\":\"ZmFtaWx5Mjpjb2x1bW4y\",\"timestamp\":1015,\"$\":\"Z29vZGJ5ZSB3b3JsZA==\"}]}",
                         compactJson);
        }
Пример #3
0
        /// <summary>
        /// 获取给定股票日统计数据所对应的 HBase 数据行。
        /// </summary>
        /// <param name="stockInfo">股票日统计数据</param>
        /// <param name="timestamp">获取数据时的时间戳</param>
        /// <returns></returns>
        private static HBaseRow GetRow(StockDailyStatisticsInfo stockInfo, DateTime timestamp)
        {
            var row = new HBaseRow {
                Key = stockInfo.Code
            };

            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("open", "open"),
                Timestamp = timestamp.Ticks,
                Data      = Encoding.UTF8.GetBytes(stockInfo.OpenPrice.ToString("G"))
            });

            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("close", "close"),
                Timestamp = timestamp.Ticks,
                Data      = Encoding.UTF8.GetBytes(stockInfo.ClosePrice.ToString("G"))
            });

            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("highest", "highest"),
                Timestamp = timestamp.Ticks,
                Data      = Encoding.UTF8.GetBytes(stockInfo.HighestPrice.ToString("G"))
            });

            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("lowest", "lowest"),
                Timestamp = timestamp.Ticks,
                Data      = Encoding.UTF8.GetBytes(stockInfo.LowestPrice.ToString("G"))
            });

            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("total", "total"),
                Timestamp = timestamp.Ticks,
                Data      = Encoding.UTF8.GetBytes(stockInfo.Volume.ToString("G"))
            });

            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("date", "date"),
                Timestamp = timestamp.Ticks,
                Data      = Encoding.UTF8.GetBytes(timestamp.ToString("yyyy-M-d hh:mm:ss"))
            });

            return(row);
        }
Пример #4
0
        public void RowCollectionSerialization()
        {
            var row1 = new HBaseRow
            {
                Key = "row key 1"
            };

            row1.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family1", "column1"),
                Timestamp = 325,
                Data      = Encoding.UTF8.GetBytes("hello world")
            });
            row1.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family2", "column2"),
                Timestamp = 1015,
                Data      = Encoding.UTF8.GetBytes("goodbye world")
            });

            var row2 = new HBaseRow
            {
                Key = "row key 2"
            };

            row2.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family3", "column3"),
                Timestamp = 76,
                Data      = Encoding.UTF8.GetBytes("你好世界")
            });
            row2.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family4", "column4"),
                Timestamp = 1111,
                Data      = Encoding.UTF8.GetBytes("再见世界")
            });

            var json        = HBaseSerializationHelper.SerializeObject(new[] { row1, row2 });
            var compactJson = Regex.Replace(json, @"\s+", string.Empty);

            Assert.Equal("[{\"key\":\"cm93IGtleSAx\",\"Cell\":[{\"column\":\"ZmFtaWx5MTpjb2x1bW4x\",\"timestamp\":325,\"$\":\"aGVsbG8gd29ybGQ=\"},{\"column\":\"ZmFtaWx5Mjpjb2x1bW4y\",\"timestamp\":1015,\"$\":\"Z29vZGJ5ZSB3b3JsZA==\"}]},{\"key\":\"cm93IGtleSAy\",\"Cell\":[{\"column\":\"ZmFtaWx5Mzpjb2x1bW4z\",\"timestamp\":76,\"$\":\"5L2g5aW95LiW55WM\"},{\"column\":\"ZmFtaWx5NDpjb2x1bW40\",\"timestamp\":1111,\"$\":\"5YaN6KeB5LiW55WM\"}]}]",
                         compactJson);
        }
Пример #5
0
        /// <summary>
        /// 从给定的 HBase 数据行创建 <see cref="StockListItem"/> 对象。
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"><paramref name="row"/>为null</exception>
        public static StockListItem FromHBaseRow(HBaseRow row)
        {
            if (row == null)
            {
                throw new ArgumentNullException(nameof(row));
            }

            var stock = new StockListItem {
                Code = row.Key
            };

            var nameCells = row.Cells.Get("name", "name").ToArray();

            if (nameCells.Length > 0)
            {
                stock.Name = Encoding.UTF8.GetString(nameCells[0].Data);
            }

            var priceCells = row.Cells.Get("price", "price").ToArray();

            if (priceCells.Length > 0)
            {
                stock.Price = double.Parse(Encoding.UTF8.GetString(priceCells[0].Data));
            }

            var priceChangeCells = row.Cells.Get("priceChange", "priceChange").ToArray();

            if (priceChangeCells.Length > 0)
            {
                stock.PriceRelativeChangePercent = double.Parse(Encoding.UTF8.GetString(priceChangeCells[0].Data));
            }

            var updateTimeCells = row.Cells.Get("date", "date").ToArray();

            if (updateTimeCells.Length > 0)
            {
                stock.UpdateTime = DateTime.Parse(Encoding.UTF8.GetString(updateTimeCells[0].Data));
            }

            return(stock);
        }
Пример #6
0
        public void HasColumns()
        {
            MockRepository mockRepository = new MockRepository();
            IHBaseConnection connection = mockRepository.Stub<IHBaseConnection>();
            IHBaseRowData rowData = mockRepository.Stub<IHBaseRowData>();
            HBaseDatabase db = new HBaseDatabase(connection);

            byte[] columnName = Encoding.UTF8.GetBytes("c");

            using (mockRepository.Record())
            {
                SetupResult.For(rowData.Key).Return(Encoding.UTF8.GetBytes("k"));
                SetupResult.For(rowData.Columns).Return(new Dictionary<byte[], IList<IHBaseCellData>>() { { columnName, new List<IHBaseCellData>() }});
            }

            using (mockRepository.Playback())
            {
                HBaseRow row = new HBaseRow(rowData, new HBaseTable(Encoding.UTF8.GetBytes("t"), db));
                Assert.Contains(columnName, row.Columns.Keys);
            }
        }
Пример #7
0
        public void RegardsFirstValueAsLatest()
        {
            MockRepository mockRepository = new MockRepository();
            IHBaseConnection connection = mockRepository.Stub<IHBaseConnection>();
            IHBaseCellData cellData1 = mockRepository.Stub<IHBaseCellData>();
            IHBaseCellData cellData2 = mockRepository.Stub<IHBaseCellData>();

            HBaseRow row = new HBaseRow(Encoding.UTF8.GetBytes("r"),
                                        new HBaseTable(Encoding.UTF8.GetBytes("t"), new HBaseDatabase(connection)));

            using (mockRepository.Record())
            {
                SetupResult.For(cellData1.Timestamp).Return(1);
                SetupResult.For(cellData1.Value).Return(Encoding.UTF8.GetBytes("old"));
                SetupResult.For(cellData2.Timestamp).Return(2);
                SetupResult.For(cellData2.Value).Return(Encoding.UTF8.GetBytes("new"));
            }

            using (mockRepository.Playback())
            {
                HBaseColumn column = new HBaseColumn(Encoding.UTF8.GetBytes("c"), row, new List<IHBaseCellData> { cellData2, cellData1 });
                Assert.Equal(Encoding.UTF8.GetBytes("new"), column.Value);
            }
        }
Пример #8
0
        public void RowDeserialization()
        {
            var json = "{\"key\":\"cm93IGtleQ==\",\"Cell\":[{\"column\":\"ZmFtaWx5MTpjb2x1bW4x\",\"timestamp\":325,\"$\":\"aGVsbG8gd29ybGQ=\"},{\"column\":\"ZmFtaWx5Mjpjb2x1bW4y\",\"timestamp\":1015,\"$\":\"Z29vZGJ5ZSB3b3JsZA==\"}]}";
            var row  = HBaseSerializationHelper.DeserializeObject <HBaseRow>(json);

            var expectedRow = new HBaseRow {
                Key = "row key"
            };

            expectedRow.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family1", "column1"),
                Timestamp = 325,
                Data      = Encoding.UTF8.GetBytes("hello world")
            });
            expectedRow.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family2", "column2"),
                Timestamp = 1015,
                Data      = Encoding.UTF8.GetBytes("goodbye world")
            });

            Assert.Equal(expectedRow, row, new HBaseRowEqualityComparer());
        }
Пример #9
0
        /// <summary>
        /// 从给定的股票实时数据对象创建 HBase 行。
        /// </summary>
        /// <param name="stockInfo"></param>
        /// <param name="timestamp">时间戳</param>
        /// <returns></returns>
        private static HBaseRow GetRow(StockRealtimeInfo stockInfo, DateTime timestamp)
        {
            var row = new HBaseRow {
                Key = stockInfo.Code
            };

            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("name", "name"),
                Timestamp = timestamp.Ticks,
                Data      = Encoding.UTF8.GetBytes(stockInfo.Name)
            });

            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("price", "price"),
                Timestamp = timestamp.Ticks,
                Data      = Encoding.UTF8.GetBytes(stockInfo.CurrentPrice.ToString("G"))
            });

            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("priceChange", "priceChange"),
                Timestamp = timestamp.Ticks,
                Data      = Encoding.UTF8.GetBytes(stockInfo.PriceRelativeChange.ToString("G"))
            });

            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("date", "date"),
                Timestamp = timestamp.Ticks,
                Data      = Encoding.UTF8.GetBytes(timestamp.ToString("yyyy-M-d hh:mm:ss"))
            });

            return(row);
        }