Skip to content

Commit 9ae47e9

Browse files
Merge pull request #302297 from wchigit/wc/fix-java-cassandra
[Service Connector] Fix java+cassandra sample code
2 parents f4527a0 + 4a2dd18 commit 9ae47e9

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

articles/service-connector/includes/code-cosmoscassandra-me-id.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ ms.author: wchi
125125
</dependency>
126126
```
127127
128-
1. Get an access token for the managed identity or service principal using `azure-identity`. Use the access token and `AZURE_COSMOS_LISTKEYURL` to get the password. Get the connection information from the environment variables added by Service Connector and connect to Azure Cosmos DB for Cassandra. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
128+
1. Get an access token for the managed identity or service principal using `azure-identity`. Use the access token and `AZURE_COSMOS_LISTKEYURL` to get the password. Get the connection information from the environment variables added by Service Connector and connect to Azure Cosmos DB for Cassandra. When using the code below, uncomment the part of the code snippet for the authentication type you want to use. Replace `<AZURE_COSMOS_DB_ACCOUNT_LOCATION>` with the location of your Azure Cosmos DB account.
129129
130130
```java
131131
import com.datastax.oss.driver.api.core.CqlSession;
@@ -164,7 +164,9 @@ ms.author: wchi
164164
String token = accessToken.getToken();
165165
166166
// Get the password.
167-
HttpClient client = HttpClient.newBuilder().build();
167+
HttpClient client = HttpClient.newBuilder()
168+
.version(HttpClient.Version.HTTP_1_1)
169+
.build();
168170
HttpRequest request = HttpRequest.newBuilder()
169171
.uri(new URI(listKeyUrl))
170172
.header("Authorization", "Bearer " + token)
@@ -177,9 +179,14 @@ ms.author: wchi
177179
178180
// Connect to Azure Cosmos DB for Cassandra
179181
final SSLContext sc = SSLContext.getInstance("TLSv1.2");
180-
CqlSession session = CqlSession.builder().withSslContext(sc)
182+
sc.init(null, null, null);
183+
CqlSession session = CqlSession.builder()
184+
.withSslContext(sc)
181185
.addContactPoint(new InetSocketAddress(cassandraHost, cassandraPort)).withLocalDatacenter('datacenter1')
182-
.withAuthCredentials(cassandraUsername, cassandraPassword).build();
186+
.withLocalDatacenter("<AZURE_COSMOS_DB_ACCOUNT_LOCATION>") // Use the same location as your Azure Cosmos DB account
187+
.withKeyspace(cassandraKeyspace)
188+
.withAuthCredentials(cassandraUsername, cassandraPassword)
189+
.build();
183190
```
184191
185192
### [SpringBoot](#tab/springBoot)

0 commit comments

Comments
 (0)