แชร์ผ่าน


เลขหน้า

การแบ่งหน้าหมายถึงแนวทางปฏิบัติของการแบ่งชุดข้อมูลขนาดใหญ่เป็นกลุ่มหรือหน้าเล็กกว่าที่สามารถจัดการได้เมื่อส่งข้อมูลไปยังแอปพลิเคชันไคลเอ็นต์ ซึ่งเป็นเทคนิคทั่วไปที่ใช้ในการปรับปรุงประสิทธิภาพและประสิทธิภาพของคําขอ API โดยเฉพาะอย่างยิ่งเมื่อจัดการกับข้อมูลจํานวนมาก นอกจากนี้ ยังมีการใช้การแบ่งหน้าเพื่อป้องกันการสูญหายของข้อมูลเมื่อมีข้อมูลมากเกินไปที่จะแสดงเป็นกลุ่มเดียว

ฉันจะทราบได้อย่างไรว่ามีการแบ่งหน้า API หรือไม่

API ที่มีการแบ่งหน้าของ Microsoft Fabric ประกอบด้วยพารามิเตอร์เหล่านี้

  • continuationUri

  • continuationToken

ฉันจะค้นหาพารามิเตอร์แบบแบ่งหน้าได้ที่ไหน

โครงสร้างของการตอบสนอง API แบบแบ่งหน้าประกอบด้วยพารามิเตอร์ continuationUri และ continuationToken และมีลักษณะดังนี้:

{
  "value": [
    {
      "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",  
      "displayName": "Lakehouse",
      "description": "A lakehouse used by the analytics team.",
      "type": "Lakehouse",
      "workspaceId": "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" 
    },
    {
      "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",  
      "displayName": "Notebook",
      "description": "A notebook for refining medical data analysis through machine learning algorithms.",
      "type": "Notebook",
      "workspaceId": "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" 
    }
  ],
  "continuationToken": "ABCsMTAwMDAwLDA%3D",
  "continuationUri": "https://api.fabric.microsoft.com/v1/workspaces/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/items?continuationToken=ABCsMTAwMDAwLDA%3D"
}

วิธีการใช้การแบ่งหน้าใน Fabric REST API

เมื่อคุณทําการร้องขอ API ที่มีการแบ่งหน้า คุณจะได้รับชุดของระเบียน โดยปกติจะอยู่ภายใต้ค่า คุณสมบัติ ระเบียนมีพารามิเตอร์ continuationUri และ continuationToken ด้วยพารามิเตอร์เหล่านี้ คุณสามารถเรียกใช้ชุดถัดไปของระเบียนโดยใช้หนึ่งในวิธีเหล่านี้:

  • ใช้ continuationUri เพื่อรับคําขอถัดไปของคุณ

  • ใช้ continuationToken เป็นพารามิเตอร์คิวรีเพื่อสร้างคําขอถัดไปของคุณ

เมื่อดึงบันทึกทั้งหมดแล้ว พารามิเตอร์ continuationUri และ continuationToken จะถูกลบออกจากการตอบสนองหรือปรากฏเป็น null

ตัวอย่างโค้ด

ในตัวอย่างนี้ คุณสร้างไคลเอ็นต์และเรียกใช้พื้นที่ทํางานรายการ API พารามิเตอร์ continuationToken ถูกใช้เพื่อรับกลุ่มพื้นที่ทํางานที่มีการแบ่งหน้าถัดไปจนกว่าจะส่งกลับค่าว่างหรือ null

using (HttpClient client = new HttpClient()) 
{ 
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "<Your token>"); 
    string continuationToken = null; 
    var workspaces = new List<Workspace>(); 
    do 
    { 
        var requestUrl = "https://api.fabric.microsoft.com/v1/workspaces"; 
        if (!string.IsNullOrEmpty(continuationToken)) 
        { 
            requestUrl += $"?continuationToken={continuationToken}"; 
        } 
        HttpResponseMessage response = await client.GetAsync(requestUrl); 
        if (response.IsSuccessStatusCode) 
        { 

            // Parse the response JSON   
            var responseData = await response.Content.ReadAsStringAsync(); 
            var paginatedResponse = JsonConvert.DeserializeObject<PaginatedResponse<Workspace>>(responseData); 

            // Append the list of workspaces in the current retrieved page 
            workspaces.AddRange(paginatedResponse.Value); 

            // Check if there are more records to retrieve 
            continuationToken = paginatedResponse.ContinuationToken; 
        } 
        else 
        { 
            Console.WriteLine($"Error: {response.StatusCode}"); 
            break; 
        } 
    } while (!string.IsNullOrEmpty(continuationToken)); 
}