import urllib.request
import json
with urllib.request.urlopen("https://cosmosnotebooksdata.blob.core.chinacloudapi.cn/notebookdata/websiteData.json") as url:
docs = json.loads(url.read().decode())
for doc in docs:
container.upsert_item(doc)
运行该单元。 这需要 45 秒到 1 分钟才能运行。
添加一个新代码单元格。
在代码单元格中,创建一个新的 C# 类来表示容器中的项。 运行该单元。
public class Record
{
public string id { get; set; }
public int CartID { get; set; }
public string Action { get; set; }
public decimal Price { get; set; }
public string Country { get; set; }
public string Item { get; set; }
}
using System.Net.Http;
using System.Text.Json;
using System.IO;
var dataURL = "https://cosmosnotebooksdata.blob.core.chinacloudapi.cn/notebookdata/websiteData.json";
var jsonData = new HttpClient().GetStringAsync(dataURL).Result;
Record[] result = JsonSerializer.Deserialize<Record[]>(jsonData);
foreach (Record record in result) {
await container.UpsertItemAsync<Record>(record, new PartitionKey(record.CartID)); //43 seconds
}
using System.Collections.Generic;
var query = new QueryDefinition(
query: "SELECT c.Action, c.Price, c.Country, c.Item FROM c"
);
FeedIterator<Record> feed = container.GetItemQueryIterator<Record>(
queryDefinition: query
);
var results = new List<Record>();
while (feed.HasMoreResults)
{
FeedResponse<Record> response = await feed.ReadNextAsync();
foreach (Record result in response)
{
results.Add(result);
}
}