示例#1
0
        public async Task <ActionResult <StockRealtimeInfo> > GetStockRealtimeInfo(
            string code,
            [FromQuery] DateTime?date)
        {
            if (date == null)
            {
                date = DateTime.Now;
            }

            var hbaseClient = _hbaseClientFactory.Create();

            var options = new HBaseFindOptions
            {
                NumberOfVersions = 1000
            };

            var realtimeRow = await hbaseClient.Find(RealtimeDataTableName, code, options);

            if (realtimeRow == null)
            {
                return(NotFound());
            }

            //获取股票名称
            var name = Encoding.UTF8.GetString(realtimeRow.Cells.Get("name", "name").ToArray()[0].Data);

            // 获取timestamp在当天的所有cell
            var realtimeCells = realtimeRow.Cells.GetAt(date.Value, "date");

            // 合并真实值和预测值
            var realtimeInfo = StockRealtimeInfo.FromHBaseRowCellCollection(realtimeCells);

            realtimeInfo.Code = code;
            realtimeInfo.Name = name;

            return(new ActionResult <StockRealtimeInfo>(realtimeInfo));
        }
示例#2
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);
        }