Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

9/17/2020

Reading time:2 min

DataStax-Examples/object-mapper-csharp

by John Doe

The C# DataStax Driver comes with an object mapper that removes boilerplate of writing queries and lets you focus on your application objects. This example shows how to use the mapper to access Apache Cassandra™ in a C# application.Contributor(s): Dave Bechberger - derived from hereObjectivesTo demonstrate how to use the object mapper to insert dataTo demonstrate how to use the object mapper to retrieve dataTo demonstrate how to use the object mapper to update dataTo demonstrate how to use the object mapper to delete dataProject LayoutProgram.cs - The main application fileUser.cs - The POCO object that maps to the Cassandra tableHow this Sample WorksThis sample works by first making a connection to a Cassandra instance, this is defaulted to localhost. Once the Cluster object has been built the mapping between the User object and the users table is created using this code:MappingConfiguration.Global.Define( new Map<User>() .TableName("users") .PartitionKey(u => u.UserId) .Column(u => u.UserId, cm => cm.WithName("id")));One thing to notice with this code is that we are mapping the UserId property in the object to the id column in the table.Once we have made this can connected our session this sample contains four functions:InsertOperations - This contains the insert operations, including batch inserts, you can perform with the object mapperQueryOperations - This contains the query operations you can perform with the object mapperUpdateOperations - This contains the update operations you can perform with the object mapperDeleteOperations - This contains the delete operations, including batch inserts, you can perform with the object mapperThis sample shows the most common patterns used with the object mapper and is not an exhaustive example of the features and configuration options available within the driver. For a complete listing of the features and configuration options please check the documentation here.Setup and RunningPrerequisites.NET Core 2.1A Cassandra cluster is running and accessible through the contacts points and data centerNote This application defaults to connecting to a cluster on localhost. This can be changed in Program.cs by modifying the following to your settings.Cassandra.Cluster.Builder().AddContactPoint("127.0.0.1").Build();RunningTo run this application use the following command:dotnet runThis will produce output similar to the following:Retrieved 11 usersRetrieved 1 usersRetrieved 1 usersRetrieved UserId: 15b6cca1-c5ed-4bdc-9b4e-625897173065, Name: User 0, Age: 0Retrieved UserId: 15b6cca1-c5ed-4bdc-9b4e-625897173065, Name: User 0, Age: 0Retrieved UserId: 0f0dfe3f-4f2e-4638-808b-1bdc5352a7eb, Name: User 3, Age: 3Retrieved UserId: 0f0dfe3f-4f2e-4638-808b-1bdc5352a7eb, Name: User 3, Age: 3Retrieved UserId: 15b6cca1-c5ed-4bdc-9b4e-625897173065, Name: Update POCO, Age: 0Retrieved UserId: 15b6cca1-c5ed-4bdc-9b4e-625897173065, Name: Update CQL, Age: 0Retrieved 0 users

Illustration Image

The C# DataStax Driver comes with an object mapper that removes boilerplate of writing queries and lets you focus on your application objects. This example shows how to use the mapper to access Apache Cassandra™ in a C# application.

Contributor(s): Dave Bechberger - derived from here

Objectives

  • To demonstrate how to use the object mapper to insert data
  • To demonstrate how to use the object mapper to retrieve data
  • To demonstrate how to use the object mapper to update data
  • To demonstrate how to use the object mapper to delete data

Project Layout

  • Program.cs - The main application file
  • User.cs - The POCO object that maps to the Cassandra table

How this Sample Works

This sample works by first making a connection to a Cassandra instance, this is defaulted to localhost. Once the Cluster object has been built the mapping between the User object and the users table is created using this code:

MappingConfiguration.Global.Define(
    new Map<User>()
    .TableName("users")
    .PartitionKey(u => u.UserId)
    .Column(u => u.UserId, cm => cm.WithName("id")));

One thing to notice with this code is that we are mapping the UserId property in the object to the id column in the table.

Once we have made this can connected our session this sample contains four functions:

  • InsertOperations - This contains the insert operations, including batch inserts, you can perform with the object mapper
  • QueryOperations - This contains the query operations you can perform with the object mapper
  • UpdateOperations - This contains the update operations you can perform with the object mapper
  • DeleteOperations - This contains the delete operations, including batch inserts, you can perform with the object mapper

This sample shows the most common patterns used with the object mapper and is not an exhaustive example of the features and configuration options available within the driver. For a complete listing of the features and configuration options please check the documentation here.

Setup and Running

Prerequisites

  • .NET Core 2.1
  • A Cassandra cluster is running and accessible through the contacts points and data center

Note This application defaults to connecting to a cluster on localhost. This can be changed in Program.cs by modifying the following to your settings.

Cassandra.Cluster.Builder().AddContactPoint("127.0.0.1").Build();

Running

To run this application use the following command:

dotnet run

This will produce output similar to the following:

Retrieved 11 users
Retrieved 1 users
Retrieved 1 users
Retrieved UserId: 15b6cca1-c5ed-4bdc-9b4e-625897173065, Name: User 0, Age: 0
Retrieved UserId: 15b6cca1-c5ed-4bdc-9b4e-625897173065, Name: User 0, Age: 0
Retrieved UserId: 0f0dfe3f-4f2e-4638-808b-1bdc5352a7eb, Name: User 3, Age: 3
Retrieved UserId: 0f0dfe3f-4f2e-4638-808b-1bdc5352a7eb, Name: User 3, Age: 3
Retrieved UserId: 15b6cca1-c5ed-4bdc-9b4e-625897173065, Name: Update POCO, Age: 0
Retrieved UserId: 15b6cca1-c5ed-4bdc-9b4e-625897173065, Name: Update CQL, Age: 0
Retrieved 0 users

Related Articles

examples
cassandra
datastax

GitHub - datastaxdevs/workshop-betterreads: Clone of Good Reads using Spring and Cassandra

datastaxdevs

12/2/2023

Checkout Planet Cassandra

Claim Your Free Planet Cassandra Contributor T-shirt!

Make your contribution and score a FREE Planet Cassandra Contributor T-Shirt! 
We value our incredible Cassandra community, and we want to express our gratitude by sending an exclusive Planet Cassandra Contributor T-Shirt you can wear with pride.

Join Our Newsletter!

Sign up below to receive email updates and see what's going on with our company

Explore Related Topics

AllKafkaSparkScyllaSStableKubernetesApiGithubGraphQl

Explore Further

examples