Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

punam.kumari_185226 avatar image
punam.kumari_185226 asked ·

What's the format for AddContactPoints() in C# for 3 Docker instances sharing the same IP?

I have configured 3 Cassandra database nodes in docker with port mapping 9043:9042, 9044:9042, and 9045:9042. I can work with a single node . How to pass other nodes in contact points.

Cluster.Builder()
    .WithSessionName("Cassandra")
    .AddContactPoints("127.0.0.1")
    .WithPort(9043)
drivercsharp
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

joao.reis avatar image
joao.reis answered ·

The answer posted by @smadhavan is correct, if you pass IPEndPoint objects to the Builder.AddContactPoints() method, the driver will use the correct ports.

However, Apache Cassandra has only recently (version 4, still in alpha at this time) introduced a feature where the server ports are exposed to the client. AFAIK, in this scenario with the current server version (3.11.x) the driver will only connect to the node that is used as the contact point.

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

smadhavan avatar image
smadhavan answered ·

@punam.kumari_185226, you could pass in multiple hosts via a String[] to Cluster.Builder().AddContactPoints(String[]). Refer to API documentation & driver documentation for additional details on the same.

2 comments Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

What should be in the string[]? I am getting unresolved host error when I am doing like this

AddContactPoints(new string[] {"127.0.0.1:9043", "127.0.0.1:9044"})

0 Likes 0 · ·
smadhavan avatar image smadhavan punam.kumari_185226 ·

@punam.kumari_185226, for your use case, you could try something like below,

System.Net.IPAddress ipAddress = System.Net.IPAddress.Parse("127.0.0.1");
IPEndPoint endPoint1 = new IPEndPoint(ipAddress, 9043);
IPEndPoint endPoint2 = new IPEndPoint(ipAddress, 9044);
IPEndPoint endPoint3 = new IPEndPoint(ipAddress, 9045);
Cluster.Builder().AddContactPoints(new IPEndPoint[]{endPoint1, endPoint2, endPoint3}).Build()

Disclaimer: I am not a C# developer so, I have made my best attempt above.

0 Likes 0 · ·