我打算为了自己的目的保存来自CoinMarketCap API的记录。我想每15分钟在前100个加密硬币上保存价格信息,我想将它保存在Azure Cosmos DB中。
由于我是文档db的整个概念的新手,我需要一些帮助建模文档。
首先我开始使用这个模型。
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "573.137",
"price_btc": "1.0",
"24h_volume_usd": "72855700.0",
"market_cap_usd": "9080883500.0",
"available_supply": "15844176.0",
"total_supply": "15844176.0",
"percent_change_1h": "0.04",
"percent_change_24h": "-0.3",
"percent_change_7d": "-0.57",
"last_updated": "1472762067"
},
{
"id": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_usd": "12.1844",
"price_btc": "0.021262",
"24h_volume_usd": "24085900.0",
"market_cap_usd": "1018098455.0",
"available_supply": "83557537.0",
"total_supply": "83557537.0",
"percent_change_1h": "-0.58",
"percent_change_24h": "6.34",
"percent_change_7d": "8.59",
"last_updated": "1472762062"
},
...
]
但由于每次写入数据库时id都没有改变,因此记录只是更新,而不是聚合。我想这是预期的。
因此,为了确保记录汇总,我将模型重写为此。
[
{
"id": <timestamp>_bitcoin
"identifier": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "573.137",
"price_btc": "1.0",
"24h_volume_usd": "72855700.0",
"market_cap_usd": "9080883500.0",
"available_supply": "15844176.0",
"total_supply": "15844176.0",
"percent_change_1h": "0.04",
"percent_change_24h": "-0.3",
"percent_change_7d": "-0.57",
"last_updated": "1472762067"
},
{
"id": <timestamp>_ethereum
"identifier": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_usd": "12.1844",
"price_btc": "0.021262",
"24h_volume_usd": "24085900.0",
"market_cap_usd": "1018098455.0",
"available_supply": "83557537.0",
"total_supply": "83557537.0",
"percent_change_1h": "-0.58",
"percent_change_24h": "6.34",
"percent_change_7d": "8.59",
"last_updated": "1472762062"
},
...
]
在这里,我添加了一个单独的id
,其中包含时间戳和对旧ID的引用,以使其独一无二。
这工作正常,但我认为这是一些重复的数据(例如名称和符号),我认为看起来很糟糕。但也许这就是db world文档中的情况?
我也想过这样的模型。
[
{
"id": <timestamp>_bitcoin
"identifier": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_history": [{
"price_usd": "573.137",
"price_btc": "1.0",
"24h_volume_usd": "72855700.0",
"market_cap_usd": "9080883500.0",
"available_supply": "15844176.0",
"total_supply": "15844176.0",
"percent_change_1h": "0.04",
"percent_change_24h": "-0.3",
"percent_change_7d": "-0.57",
"last_updated": "1472762067"
]}
},
{
"id": <timestamp>_ethereum
"identifier": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_history": [{
"price_usd": "12.1844",
"price_btc": "0.021262",
"24h_volume_usd": "24085900.0",
"market_cap_usd": "1018098455.0",
"available_supply": "83557537.0",
"total_supply": "83557537.0",
"percent_change_1h": "-0.58",
"percent_change_24h": "6.34",
"percent_change_7d": "8.59",
"last_updated": "1472762062"
]}
},
...
]
但由于没有选择将新记录添加到price_history
而不重写整个文档,这不是一个好主意。此外,由于price_history
中的记录数量可能会无限增长,因此本文档将变得非常庞大且难以处理。
接下来我想到分成单独的文档,但不确定这是否也是要走的路。所以此刻我有点失落。
有什么建议?
这是事实部分:
现在对于完全意见部分:
Ryan CrawCour和David Makogon在第9频道的There's a great video处理Cosmos DB中的建模数据,我发现这个数据非常有用。
这是一篇很好的文章,总结了非关系世界https://docs.microsoft.com/en-us/azure/cosmos-db/modeling-data中数据建模的最佳实践
HTH