مشاركة عبر


إنشاء مساحة مفتاح وجدول ل Azure Cosmos DB - API ل Cassandra

ينطبق على: كاساندرا

إشعار

نوصي باستخدام الوحدة النمطية Azure Az PowerShell للتفاعل مع Azure. للبدء، راجع تثبيت Azure PowerShell. لمعرفة كيفية الترحيل إلى الوحدة النمطية Az PowerShell، راجع ترحيل Azure PowerShell من AzureRM إلى Az.

يتطلب هذا النموذج Azure PowerShell Az 5.4.0 أو أحدث. اضغط Get-Module -ListAvailable Az لمعرفة الإصدارات المثبتة. إذا كنت بحاجة إلى التثبيت، راجع نموذجInstall Azure PowerShell.

اضغط Connect-AzAccount لتسجيل الدخول إلى Azure.

نموذج البرنامج النصي

# Reference: Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Create Cosmos Cassandra API account with automatic failover,
# a keyspace, and a table with defined schema, dedicated throughput, and
# conflict resolution policy with last writer wins and custom resolver path.
# --------------------------------------------------
Function New-RandomString{Param ([Int]$Length = 10) return $(-join ((97..122) + (48..57) | Get-Random -Count $Length | ForEach-Object {[char]$_}))}
# --------------------------------------------------
$uniqueId = New-RandomString -Length 7 # Random alphanumeric string for unique resource names
$apiKind = "Cassandra"
# --------------------------------------------------
# Variables - ***** SUBSTITUTE YOUR VALUES *****
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "East Us" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "West Us" -FailoverPriority 1 -IsZoneRedundant 0

$resourceGroupName = "myResourceGroup" # Resource Group must already exist
$accountName = "cosmos-$uniqueId" # Must be all lower case
$consistencyLevel = "BoundedStaleness"
$maxStalenessInterval = 300
$maxStalenessPrefix = 100000
$tags = @{Tag1 = "MyTag1"; Tag2 = "MyTag2"; Tag3 = "MyTag3"}
$keyspaceName = "mykeyspace"
$tableName = "mytable"
$tableRUs = 400
$partitionKeys = @("machine", "cpu", "mtime")
$clusterKeys = @( 
    @{ name = "loadid"; orderBy = "Asc" };
    @{ name = "duration"; orderBy = "Desc" }
)
$columns = @(
    @{ name = "loadid"; type = "uuid" };
    @{ name = "machine"; type = "uuid" };
    @{ name = "cpu"; type = "int" };
    @{ name = "mtime"; type = "int" };
    @{ name = "load"; type = "float" };
    @{ name = "duration"; type = "float" }
)
# --------------------------------------------------
Write-Host "Creating account $accountName"
$account = New-AzCosmosDBAccount -ResourceGroupName $resourceGroupName `
    -LocationObject $locations -Name $accountName -ApiKind $apiKind -Tag $tags `
    -DefaultConsistencyLevel $consistencyLevel `
    -MaxStalenessIntervalInSeconds $maxStalenessInterval `
    -MaxStalenessPrefix $maxStalenessPrefix `
    -EnableAutomaticFailover:$true

Write-Host "Creating keyspace $keyspaceName"
$keyspace = New-AzCosmosDBCassandraKeyspace -ParentObject $account `
    -Name $keyspaceName

# Table Schema
$psClusterKeys = @()
ForEach ($clusterKey in $clusterKeys) {
    $psClusterKeys += New-AzCosmosDBCassandraClusterKey -Name $clusterKey.name -OrderBy $clusterKey.orderBy
}

$psColumns = @()
ForEach ($column in $columns) {
    $psColumns += New-AzCosmosDBCassandraColumn -Name $column.name -Type $column.type
}

$schema = New-AzCosmosDBCassandraSchema `
    -PartitionKey $partitionKeys `
    -ClusterKey $psClusterKeys `
    -Column $psColumns

Write-Host "Creating table $tableName"
$table = New-AzCosmosDBCassandraTable -ParentObject $keyspace `
    -Name $tableName -Schema $schema -Throughput $tableRUs 

تنظيف النشر

بعد تشغيل نموذج البرنامج النصي، يمكن استخدام الأمر التالي لإزالة مجموعة الموارد وجميع الموارد المرتبطة بها.

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

شرح السيناريو

يستخدم هذا البرنامج النصي الأوامر التالية. يرتبط كل أمر في الجدول بأمر وثائق معينة.

الأمر ملاحظات
Azure Cosmos DB
New-AzCosmosDBAccount إنشاء حساب Azure Cosmos DB.
مساحة مفاتيح AzCosmosDBCassandra جديدة إنشاء Azure Cosmos DB ل Apache Cassandra Keyspace.
مفتاح كتلة AzCosmosDBCassandra جديد إنشاء Azure Cosmos DB ل Apache Cassandra Cluster Key.
عمود-AzCosmosDBCassandra جديد إنشاء Azure Cosmos DB لعمود Apache Cassandra.
مخطط AzCosmosDBCassandra جديد إنشاء Azure Cosmos DB لمخطط Apache Cassandra.
جدول AzCosmosDBCassandra جديد إنشاء Azure Cosmos DB ل Apache Cassandra Table.
مجموعات موارد Azure
Remove-AzResourceGroup يحذف مجموعة الموارد بما في ذلك جميع الموارد المتداخلة.

الخطوات التالية

للحصول على مزيدٍ من المعلومات عن Azure PowerShell، راجع وثائق Azure PowerShell.