Hyper-Converged Database (HCD) quickstart for collections (C#)
|
If your data is fully structured and you want to use a fixed schema, see the quickstart for tables instead. |
This quickstart demonstrates how to create a collection, insert data with vector embeddings to the collection, and perform a vector search to find similar data.
To learn more about vector databases and vector search, see About vector databases and What is Vector Search.
Store your endpoint
The Data API endpoint for your database has the form: http://CLUSTER_HOST:GATEWAY_PORT
-
Replace CLUSTER_HOST with the external IP address of any node in your cluster. To find this, run
kubectl get nodes -o wideand use any of the values listed under "EXTERNAL-IP" in the output. -
Replace GATEWAY_PORT with the port number for your API gateway service. To find this, run
kubectl get svcand look for the "PORT(S)" value that corresponds toNodePort.
For this quickstart, store the endpoint in an environment variable:
-
Linux or macOS
-
Windows
export API_ENDPOINT=API_ENDPOINT
set API_ENDPOINT=API_ENDPOINT
Store your username and password
You set a username and password when you create a cluster.
If you didn’t provide superuser credentials when you created your cluster, they were generated automatically and saved in a superuser secret named CLUSTER_NAME-superuser.
The CLUSTER_NAME-superuser secret contains both the username and the password.
For this quickstart, store the username and password in environment variables:
-
Linux or macOS
-
Windows
export USERNAME=USERNAME
export PASSWORD=PASSWORD
set USERNAME=USERNAME
set PASSWORD=PASSWORD
Install a client
Install one of the Data API clients to facilitate interactions with the Data API.
-
Update to one of the following:
-
.NET version 8 or later
-
.NET Framework 4.6.2 or later
-
.NET Standard 2.1 or later
-
-
Install the latest version of the astra-db-csharp package.
dotnet add package DataStax.AstraDB.DataApi
Connect to your database
The following function will connect to your database.
Copy the file into your project. You don’t need to execute the function now; the subsequent code examples will import and use this function.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Quickstart
{
public class QuickstartConnect
{
public static Database ConnectToDatabase()
{
string? endpoint = Environment.GetEnvironmentVariable(
"API_ENDPOINT"
); (1)
string? username = Environment.GetEnvironmentVariable("USERNAME");
string? password = Environment.GetEnvironmentVariable("PASSWORD");
if (
string.IsNullOrEmpty(endpoint)
|| string.IsNullOrEmpty(username)
|| string.IsNullOrEmpty(password)
)
{
throw new InvalidOperationException(
"Environment variables API_ENDPOINT, USERNAME, and PASSWORD must be defined"
);
}
// Create an instance of the `DataAPIClient` class
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
// Get the database specified by your endpoint and provide the token
var database = client.GetDatabase(
endpoint,
DataAPIClient.UsernamePasswordTokenProvider(username, password)
);
Console.WriteLine("Connected to database.");
return database;
}
}
}
| 1 | Store your database’s endpoint, username, and password in environment variables named API_ENDPOINT, USERNAME, and PASSWORD, as instructed in Store your endpoint and Store your username and password. |
Create a keyspace
The following code will create a new keyspace in your database.
-
Copy the code into your project.
-
If needed, update the import path to the "connect to database" function from the previous section.
-
Execute the code.
For information about executing code, refer to the documentation for your programming language.
Once the code completes, you should see a printed message confirming keyspace creation.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Quickstart
{
public class QuickstartCreateKeyspace
{
public static async Task Main()
{
var database = QuickstartConnect.ConnectToDatabase(); (1)
// Get an admin object
var databaseAdmin = database.GetAdmin();
// Create a keyspace
await databaseAdmin.CreateKeyspaceAsync("quickstart_keyspace"); (2)
Console.WriteLine("Created keyspace.");
}
}
}
| 1 | This is the ConnectToDatabase function from the previous section.
To use the function, ensure you stored your database’s endpoint, username, and password in environment variables as instructed in Store your endpoint and Store your username and password. |
| 2 | This code creates a keyspace named quickstart_keyspace.
If you want to use a different name, change the name before running the code. |
Create a collection
The following code will create an empty collection in your database.
-
Copy the code into your project.
-
If needed, update the import path to the "connect to database" function from the previous section.
-
Execute the code.
For information about executing code, refer to the documentation for your programming language.
Once the code completes, you should see a printed message confirming the collection creation.
|
This example creates an untyped collection, but you can define a client-side type for your collection to help statically catch errors. For examples, see Create a collection (C#) and Custom typing for collections. |
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
namespace Quickstart
{
public class QuickstartCreateCollection
{
public static async Task Main()
{
var database = QuickstartConnect.ConnectToDatabase(); (1)
await database.CreateCollectionAsync<Document>(
"quickstart_collection", (2)
new CollectionDefinition
{
Vector = new VectorOptions (3)
{
Dimension = 5,
Metric = SimilarityMetric.Cosine,
},
},
new CreateCollectionOptions()
{
Keyspace = "quickstart_keyspace", (4)
}
);
Console.WriteLine("Created collection.");
}
}
}
| 1 | This is the ConnectToDatabase function from the previous section.
To use the function, ensure you stored your database’s endpoint, username, and password in environment variables as instructed in Store your endpoint and Store your username and password. |
| 2 | This code creates a collection named quickstart_collection.
If you want to use a different name, change the name before running the code. |
| 3 | This collection will store 5-dimensional vector data and use the cosine similarity metric for comparisons. |
| 4 | This code expects that you have a keyspace named quickstart_keyspace.
If you used a different keyspace name in the previous section, update it here. |
Insert data to your collection
The following code will insert data from a JSON file to your collection.
-
Copy the code into your project.
-
Download the quickstart_dataset.json sample dataset (76 kB). This dataset is a JSON array describing library books.
-
Replace
PATH_TO_DATA_FILEin the code with the path to the dataset. -
If needed, update the import path to the "connect to database" function from the previous section.
-
Execute the code.
For information about executing code, refer to the documentation for your programming language.
Once the code completes, you should see a printed message confirming the insertion of 100 documents.
using System.Text.Json;
using System.Text.Json.Nodes;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
namespace Quickstart
{
public class QuickstartInsertToCollection
{
public static async Task Main()
{
var database = QuickstartConnect.ConnectToDatabase(); (1)
var collection = database.GetCollection(
"quickstart_collection",
new GetCollectionOptions() { Keyspace = "quickstart_keyspace" }
); (2)
var dataFilePath = "PATH_TO_DATA_FILE"; (3)
// Read the JSON file and parse it into a JSON array
string rawData = await File.ReadAllTextAsync(dataFilePath);
JsonArray jsonArray =
JsonNode.Parse(rawData)?.AsArray() ?? new JsonArray();
// Assemble the documents to insert
var documents = new List<Document>();
foreach (var node in jsonArray)
{
if (node is JsonObject obj)
{
var document = new Document();
foreach (var prop in obj)
{
document[prop.Key] = prop.Value switch
{
JsonValue val => val.GetValue<object>(),
JsonArray arr => arr.Deserialize<List<object>>()!,
JsonObject subObj => subObj.Deserialize<
Dictionary<string, object>
>(),
_ => prop.Value?.ToString(),
};
}
// Populate the reserved $vector field
document["$vector"] = obj["summary_genres_vector"];
documents.Add(document);
}
}
// Insert the data
var result = await collection.InsertManyAsync(documents);
Console.WriteLine(
$"Inserted {result.InsertedIds.Count} documents."
);
}
}
}
| 1 | This is the ConnectToDatabase function from the previous section.
To use the function, ensure you stored your database’s endpoint, username, and password in environment variables as instructed in Store your endpoint and Store your username and password. |
| 2 | This code expects that you have a collection named quickstart_collection in a keyspace named quickstart_keyspace.
If you used a different keyspace or collection name in the previous sections, update it here. |
| 3 | Replace PATH_TO_DATA_FILE with the path to the JSON data file. |
Find data in your collection
After you insert data to your collection, you can search the data. In addition to traditional database filtering, you can perform a vector search to find data that is most similar to a search vector.
The following code performs three searches on the sample data that you loaded in Insert data to your collection.
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;
namespace Quickstart
{
public class QuickstartFind
{
public static async Task Main()
{
var database = QuickstartConnect.ConnectToDatabase(); (1)
var collection = database.GetCollection(
"quickstart_collection",
new GetCollectionOptions() { Keyspace = "quickstart_keyspace" }
); (2)
// Find documents that match a filter
Console.WriteLine(
"\nFinding books with rating greater than 4.7..."
);
var filter = Builders<Document>.CollectionFilter.Gt("rating", 4.7);
var ratingCursor = collection.Find(
filter,
new CollectionFindOptions<Document>() { Limit = 10 }
);
foreach (var document in ratingCursor)
{
Console.WriteLine(
$"{document["title"]} is rated {document["rating"]}"
);
}
// Perform a vector search to find the closest match to a given vector
Console.WriteLine("\nUsing vector search to find a book...");
var singleVectorMatch = await collection.FindOneAsync(
new CollectionFindOneOptions<Document>()
{
Sort = Builders<Document>.CollectionSort.Vector(
new float[]
{
0.016326904f,
-0.031677246f,
0.04815674f,
0.0033435822f,
0.01876831f,
}
),
}
);
if (singleVectorMatch != null)
{
Console.WriteLine(
$"{singleVectorMatch["title"]} is the best match"
);
}
// Combine a filter, vector search, and projection
// to find the 3 books with more than 400 pages that are
// the closest matches to a given vector,
// and just return the title and author
Console.WriteLine(
"\nUsing filters and vector search to find 3 books with more than 400 pages, returning just the title and author..."
);
var filter3 = Builders<Document>.CollectionFilter.Gt(
"number_of_pages",
400
);
var vectorCursor = collection.Find(
filter3,
new CollectionFindOptions<Document>()
{
Limit = 3,
Projection = Builders<Document>
.Projection.Include("title")
.Include("author"),
Sort = Builders<Document>.CollectionSort.Vector(
new float[]
{
0.016326904f,
-0.031677246f,
0.04815674f,
0.0033435822f,
0.01876831f,
}
),
}
);
foreach (var document in vectorCursor)
{
Console.WriteLine($"{document["title"]} by {document["author"]}");
}
}
}
}
| 1 | This is the ConnectToDatabase function from the previous section.
To use the function, ensure you stored your database’s endpoint, username, and password in environment variables as instructed in Store your endpoint and Store your username and password. |
| 2 | This code expects that you have a collection named quickstart_collection in a keyspace named quickstart_keyspace.
If you used a different keyspace or collection name in the previous sections, update it here. |
Next steps
For more practice, you can continue building with the collection that you created here. For example, try inserting more data to the collection, or try different searches. The Data API reference provides code examples for various operations.
- Insert data from different sources
-
This quickstart demonstrated how to insert data from a JSON file, but you can insert data from many sources, including CSV and PDF files.
This quickstart also demonstrated how to insert data to a collection, which uses a flexible schema. If your data is structured and you want to use a fixed schema, you can use a table instead of a collection. See the quickstart for tables.
- Perform more complex searches
-
This quickstart demonstrated how to find data using filters and vector search. To learn more about the searches you can perform, see Find a document (C#), Find documents (C#), and Find data with vector search.