Exemplo n.º 1
0
        public async Task <ActionResult <IEnumerable <StockDailyInfo> > > GetStockDailyInfo(
            string code,
            [FromQuery][BindRequired] DateTime startDate,
            [FromQuery][BindRequired] DateTime endDate)
        {
            if (startDate > endDate)
            {
                return(BadRequest());
            }

            var hbaseClient = _hbaseClientFactory.Create();

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

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

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

            var options = new HBaseFindOptions
            {
                NumberOfVersions = 1000
            };

            var dayRow = await hbaseClient.Find(DailyDataTableName, code, options);

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

            var dayCells = dayRow.Cells.GetBetween(startDate, endDate, "date");

            var predictRow = await hbaseClient.Find(PredictionDataTableName, code);

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

            var predictCells = predictRow.Cells.GetBetween(startDate, endDate, "");

            var dailyInfo = new List <StockDailyInfo>();

            foreach (var stock in StockDailyInfo.FromHBaseRowCellCollection(dayCells, predictCells))
            {
                var info = stock.Value;
                info.Code = code;
                info.Name = name;
                dailyInfo.Add(info);
            }

            return(new ActionResult <IEnumerable <StockDailyInfo> >(dailyInfo));
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public async Task <HBaseRow> Find(string tableName, string rowKey, HBaseFindOptions options = null)
        {
            if (tableName == null)
            {
                throw new ArgumentNullException(nameof(tableName));
            }
            if (rowKey == null)
            {
                throw new ArgumentNullException(nameof(rowKey));
            }

            var urlBuilder = new StringBuilder($"{tableName}/{rowKey}");

            if (options?.Column != null)
            {
                urlBuilder.AppendFormat("/{0}", options.Column);
            }

            if (options?.Timestamp != null)
            {
                urlBuilder.AppendFormat("/{0}", options.Timestamp);
            }

            if (options?.NumberOfVersions != null)
            {
                urlBuilder.AppendFormat("?v={0}", options.NumberOfVersions);
            }

            var url = urlBuilder.ToString();

            HttpResponseMessage response;

            using (var httpClient = CreateHttpClient())
            {
                response = await httpClient.GetAsync(url);
            }

            if (!response.IsSuccessStatusCode)
            {
                throw new HBaseException("HBase REST API 返回了表示错误的 HTTP 状态码:" + response.StatusCode);
            }

            var responseBody = await response.Content.ReadAsStringAsync();

            var data = HBaseSerializationHelper.DeserializeObject <HBaseRowWrapper>(responseBody);

            if (data.Rows == null || data.Rows.Count == 0)
            {
                return(null);
            }

            return(data.Rows[0]);
        }
Exemplo n.º 3
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));
        }