Skip to content

Commit 7928d73

Browse files
authored
JAVA-1964: Complete remaining "Coming Soon" sections in docs (apache#1167)
* Authentication * Control Connection * Reconnection * Statements * UDTs
1 parent a0bbb52 commit 7928d73

6 files changed

Lines changed: 217 additions & 21 deletions

File tree

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [improvement] JAVA-2002: Reimplement TypeCodec.accepts to improve performance.
1515
- [documentation] JAVA-2041: Deprecate cross-DC failover in DCAwareRoundRobinPolicy.
1616
- [documentation] JAVA-1159: Document workaround for using tuple with udt field in Mapper.
17+
- [documentation] JAVA-1964: Complete remaining "Coming Soon" sections in docs.
1718

1819

1920
### 3.6.0

manual/auth/README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
## Authentication
22

3-
*Coming soon... In the meantime, see the javadoc for [AuthProvider].*
3+
Cassandra’s binary protocol supports [SASL]-based authentication. To enable it, use
4+
[Cluster.Builder.withCredentials] when building your `Cluster` instance to provide the credentials
5+
you wish to authenticate with:
46

5-
[AuthProvider]: http://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/AuthProvider.html
7+
```java
8+
Cluster.builder()
9+
.withCredentials("bob", "mypassword")
10+
.build();
11+
```
12+
13+
This is a shortcut for using [PlainTextAuthProvider] for simple username/password authentication
14+
(intended to work with the server-side `PasswordAuthenticator`). This may alternatively be
15+
provided using the [Cluster.Builder.withAuthProvider] method:
16+
17+
18+
```java
19+
Cluster.builder()
20+
.withAuthProvider(new PlainTextAuthProvider("bob", "mypassword"))
21+
.build();
22+
```
23+
24+
Authentication must be configured before opening a session, it cannot be changed at runtime.
25+
26+
You can also write your own provider; it must implement [AuthProvider].
27+
28+
29+
[SASL]: https://en.wikipedia.org/wiki/Simple_Authentication_and_Security_Layer
30+
31+
[Cluster.Builder.withCredentials]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/Cluster.Builder.html#withCredentials-java.lang.String-java.lang.String-
32+
[AuthProvider]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/AuthProvider.html
33+
[Cluster.Builder.withAuthProvider]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/Cluster.Builder.html#withAuthProvider-com.datastax.driver.core.AuthProvider-
34+
[PlainTextAuthProvider]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/PlainTextAuthProvider.html
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
## Control connection
22

3-
*Coming soon...*
4-
5-
<!--
6-
TODO cover:
7-
- what the control connection is (link to init sequence in ../README.md)
8-
- server-sent events (gossip + schema changes)
9-
- maybe event debouncing (although it sometimes applies to other types of events)
10-
-->
3+
The control connection is a dedicated connection used for administrative tasks:
4+
5+
* querying system tables to learn about the cluster's topology and
6+
[schema](../metadata/#schema-metadata);
7+
* checking [schema agreement](../metadata/#schema-agreement);
8+
* reacting to server events, which are used to notify the driver of external topology or schema
9+
changes.
10+
11+
When the driver starts, the control connection is established to the first contacted node. If that
12+
node goes down, a [reconnection](../reconnection/) is started to find another node; it is governed
13+
by the same policy as regular connections and tries the nodes according to a query plan from the
14+
[load balancing policy](../load_balancing/).
15+
16+
The control connection is managed independently from [regular pooled connections](../pooling/), and
17+
used exclusively for administrative requests. It is included in [Session.State.getOpenConnections],
18+
as well as the `open-connections` [metric](../metrics); for example, if you've configured a pool
19+
size of 2, the control node will have 3 connections.
20+
21+
[Session.State.getOpenConnections]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/Session.State.html#getOpenConnections-com.datastax.driver.core.Host-

manual/reconnection/README.md

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
## Reconnection
22

3-
*Coming soon... In the meantime, see the javadoc for [ReconnectionPolicy].*
3+
If the driver loses a connection to a node, it tries to re-establish it according to a configurable
4+
policy. This is used in two places:
45

5-
<!--
6-
TODO cover:
7-
- reconnection policy
8-
- scheduled reconnections vs. gossip events
9-
-->
6+
* [connection pools](../pooling/): for each node, a session has a fixed-size pool of connections to
7+
execute user requests. If a node is detected as down, a reconnection is started.
8+
* [control connection](../control_connection/): a session uses a single connection to an arbitrary
9+
node for administrative requests. If that connection goes down, a reconnection gets started; each
10+
attempt iterates through all active nodes until one of them accepts a connection. This goes on
11+
until we have a control node again.
1012

11-
[ReconnectionPolicy]: http://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/policies/ReconnectionPolicy.html
13+
[ReconnectionPolicy] controls the interval between each attempt. The policy to use may be
14+
provided using [Cluster.Builder.withReconnectionPolicy]. For example, the following configures
15+
an [ExponentialReconnectionPolicy] with a base delay of 1 second, and a max delay of 10 minutes
16+
(this is the default behavior).
17+
18+
```java
19+
Cluster.builder()
20+
.withReconnectionPolicy(new ExponentialReconnectionPolicy(1000, 10 * 60 * 1000))
21+
.build();
22+
```
23+
24+
[ConstantReconnectionPolicy] uses the same delay every time, regardless of the
25+
previous number of attempts.
26+
27+
You can also write your own policy; it must implement [ReconnectionPolicy].
28+
29+
For best results, use reasonable values: very low values (for example a constant delay of 10
30+
milliseconds) will quickly saturate your system.
31+
32+
[ReconnectionPolicy]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/policies/ReconnectionPolicy.html
33+
[Cluster.Builder.withReconnectionPolicy]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/Cluster.Builder.html#withReconnectionPolicy-com.datastax.driver.core.policies.ReconnectionPolicy-
34+
[ExponentialReconnectionPolicy]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/policies/ExponentialReconnectionPolicy.html
35+
[ConstantReconnectionPolicy]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/policies/ConstantReconnectionPolicy.html

manual/statements/batch/README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
## Batch statements
22

3-
*Coming soon... In the meantime, see the javadoc for [BatchStatement].*
3+
Use [BatchStatement] to execute a set of queries as a single operation (refer to
4+
[Batching inserts, updates and deletes][batch_dse] to understand how to use batching effectively):
45

5-
[BatchStatement]: http://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/BatchStatement.html
6+
```java
7+
PreparedStatement preparedInsertExpense =
8+
session.prepare(
9+
"INSERT INTO cyclist_expenses (cyclist_name, expense_id, amount, description, paid) "
10+
+ "VALUES (:name, :id, :amount, :description, :paid)");
11+
SimpleStatement simpleInsertBalance =
12+
new SimpleStatement("INSERT INTO cyclist_expenses (cyclist_name, balance) VALUES (?, 0) IF NOT EXISTS",
13+
"Vera ADRIAN");
14+
15+
BatchStatement batch = new BatchStatement(BatchStatement.Type.UNLOGGED)
16+
.add(simpleInsertBalance)
17+
.add(preparedInsertExpense.bind("Vera ADRIAN", 1, 7.95f, "Breakfast", false));
18+
19+
session.execute(batch);
20+
```
21+
22+
As shown in the examples above, batches can contain any combination of simple statements and bound
23+
statements. A given batch can contain at most 65536 statements. Past this limit, addition methods
24+
throw an `IllegalStateException`.
25+
26+
By default, batches are configured as [LOGGED]. This ensures that if any statement in the batch
27+
succeeds, all will eventually succeed. Ensuring all queries in a batch succeed has a
28+
performance cost. Consider using [UNLOGGED] as shown above if you do not need this capability.
29+
30+
Please note that the size of a batch is subject to the [batch_size_fail_threshold] configuration
31+
option on the server.
32+
33+
In addition, simple statements with named parameters are currently not supported in batches (this is
34+
due to a [protocol limitation][CASSANDRA-10246] that will be fixed in a future version). If you try
35+
to execute such a batch, an `IllegalArgumentException` is thrown.
36+
37+
[BatchStatement]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/BatchStatement.html
38+
[batch_dse]: http://docs.datastax.com/en/dse/5.1/cql/cql/cql_using/useBatch.html
39+
[LOGGED]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/BatchStatement.Type.html#LOGGED
40+
[UNLOGGED]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/BatchStatement.Type.html#UNLOGGED
41+
[batch_size_fail_threshold]: https://docs.datastax.com/en/cassandra/3.x/cassandra/configuration/configCassandra_yaml.html#configCassandra_yaml__batch_size_fail_threshold_in_kb
42+
[CASSANDRA-10246]: https://issues.apache.org/jira/browse/CASSANDRA-10246

manual/udts/README.md

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,99 @@
11
## User-defined types
22

3-
*Coming soon... In the meantime, see the javadoc for [UserType].*
3+
[CQL user-defined types][cql_doc] are ordered sets of named, typed fields. They must be defined in a
4+
keyspace:
45

5-
[UserType]: http://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/UserType.html
6+
```
7+
CREATE TYPE ks.type1 (
8+
a int,
9+
b text,
10+
c float);
11+
```
12+
13+
And can then be used as a column type in tables, or a field type in other user-defined types in that
14+
keyspace:
15+
16+
```
17+
CREATE TABLE ks.collect_things (
18+
pk int,
19+
ck1 text,
20+
ck2 text,
21+
v frozen<type1>,
22+
PRIMARY KEY (pk, ck1, ck2)
23+
);
24+
25+
CREATE TYPE ks.type2 (v frozen<type1>);
26+
```
27+
28+
### Fetching UDTs from results
29+
30+
The driver maps UDT columns to the [UDTValue] class, which exposes getters and setters to access
31+
individual fields by index or name:
32+
33+
```java
34+
Row row = session.execute("SELECT v FROM ks.collect_things WHERE pk = 1").one();
35+
36+
UDTValue udtValue = row.getUDTValue("v");
37+
int a = udtValue.getInt(0);
38+
String b = udtValue.getString("b");
39+
Float c = udtValue.getFloat(2);
40+
```
41+
42+
### Using UDTs as parameters
43+
44+
Statements may contain UDTs as bound values:
45+
46+
```java
47+
PreparedStatement ps =
48+
session.prepare(
49+
"INSERT INTO ks.collect_things (pk, ck1, ck2, v) VALUES (:pk, :ck1, :ck2, :v)");
50+
```
51+
52+
To create a new UDT value, you must first have a reference to its [UserType]. There are
53+
various ways to get it:
54+
55+
* from the statement's metadata
56+
57+
```java
58+
UserType udt = (UserType) ps.getVariables().getType("v");
59+
```
60+
61+
* from the driver's [schema metadata](../metadata/#schema-metadata):
62+
63+
```java
64+
UserType udt = session.getMetadata().getKeyspace("ks").getUserType("type1");
65+
```
66+
67+
* from another UDT value:
68+
69+
```java
70+
UserType udt = udtValue.getType();
71+
```
72+
73+
Note that the driver's official API does not expose a way to build [UserType] instances manually.
74+
This is because the type's internal definition must precisely match the database schema;
75+
if it doesn't (for example if the fields are not in the same order), you run the risk of inserting
76+
corrupt data, that you won't be able to read back.
77+
78+
Once you have the type, call `newValue()` and set the fields:
79+
80+
```java
81+
UDTValue udtValue = udt.newValue().setInt(0, 1).setString(1, "hello").setFloat(2, 2.3f);
82+
```
83+
84+
And bind your UDT value like any other type:
85+
86+
```java
87+
BoundStatement bs =
88+
ps.bind()
89+
.setInt("pk", 1)
90+
.setString("ck1", "1")
91+
.setString("ck2", "1")
92+
.setUDTValue("v", udtValue);
93+
session.execute(bs);
94+
```
95+
96+
[cql_doc]: https://docs.datastax.com/en/cql/3.3/cql/cql_reference/cqlRefUDType.html
97+
98+
[UDTValue]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/UDTValue.html
99+
[UserType]: https://docs.datastax.com/en/drivers/java/3.6/com/datastax/driver/core/UserType.html

0 commit comments

Comments
 (0)