.net OSS 表格存儲簡單分頁查詢

        /// <summary>
        /// 
        /// </summary>
        /// <param name="client"></param>
        /// <param name="tableName"></param>
        /// <param name="startKey"></param>
        /// <param name="endKey"></param>
        /// <param name="page">頁碼</param>
        /// <param name="limit">每頁顯示條數</param>
        /// <returns></returns>
        private KeyValuePair<List<Row>, PrimaryKey> ReadByPage(OTSClient client,string tableName,PrimaryKey startKey,PrimaryKey endKey,int page,int limit)
        {
            List<Row> rows = new List<Row>(limit);
            //需要跳過的數據條數
            int skip = (page-1)*limit;
            PrimaryKey nextStart = startKey;
            while (limit>0 && nextStart !=null )
            {
                // 構造GetRange的查詢參數。
                // 注意:startPrimaryKey需要設置爲上一次讀到的位點,從上一次未讀完的地方繼續往下讀,實現流式的範圍查詢。
                RangeRowQueryCriteria criteria = new RangeRowQueryCriteria(tableName);
                criteria.InclusiveStartPrimaryKey = nextStart;
                criteria.ExclusiveEndPrimaryKey = endKey;
                // 需要設置正確的limit,這裏期望讀出的數據行數最多爲完整的一頁數據以及需要過濾(offset)的數據
                criteria.Limit=(skip + limit);
                GetRangeRequest request = new GetRangeRequest(criteria);
                GetRangeResponse response = client.GetRange(request);
                foreach (var row in response.RowDataList)
                {                    
                    if (skip > 0)
                    {
                        skip--;
                    }
                    else
                    {
                        rows.Add(row);
                        //var ff = row.AttributeColumns["clo5"];
                        limit--;
                    }
                }
                nextStart = response.NextPrimaryKey;
            }
            return new KeyValuePair<List<Row>, PrimaryKey>(rows,nextStart);
        }
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章