Skip to content

Commit 770fdc6

Browse files
committed
Archetype for quickstart
1 parent 00c0a24 commit 770fdc6

File tree

19 files changed

+707
-2
lines changed

19 files changed

+707
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.datastax.astra</groupId>
5+
<artifactId>astra-sdk-quickstart</artifactId>
6+
<packaging>maven-archetype</packaging>
7+
<name>+ archetype::astra-sdk-quickstart</name>
8+
9+
<parent>
10+
<groupId>com.datastax.astra</groupId>
11+
<artifactId>astra-sdk-archetypes</artifactId>
12+
<version>0.6.1-SNAPSHOT</version>
13+
</parent>
14+
15+
<build>
16+
<extensions>
17+
<extension>
18+
<groupId>org.apache.maven.archetype</groupId>
19+
<artifactId>archetype-packaging</artifactId>
20+
<version>3.2.1</version>
21+
</extension>
22+
</extensions>
23+
24+
<pluginManagement>
25+
<plugins>
26+
<plugin>
27+
<artifactId>maven-archetype-plugin</artifactId>
28+
<version>3.2.1</version>
29+
</plugin>
30+
</plugins>
31+
</pluginManagement>
32+
</build>
33+
34+
35+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<archetype-descriptor xsi:schemaLocation="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0 http://maven.apache.org/xsd/archetype-descriptor-1.1.0.xsd" name="sample-spring-boot-2x"
3+
xmlns="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<fileSets>
6+
<fileSet filtered="true" packaged="true" encoding="UTF-8">
7+
<directory>src/test/java</directory>
8+
<includes>
9+
<include>**/*.java</include>
10+
</includes>
11+
</fileSet>
12+
<fileSet encoding="UTF-8">
13+
<directory>src/test/resources</directory>
14+
<includes>
15+
<include>**/*.xml</include>
16+
</includes>
17+
</fileSet>
18+
</fileSets>
19+
</archetype-descriptor>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>${artifactId}</artifactId>
7+
<name>${artifactId}</name>
8+
<groupId>${groupId}</groupId>
9+
<version>${version}</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>com.datastax.astra</groupId>
14+
<artifactId>astra-sdk</artifactId>
15+
<version>0.6.1</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.junit.jupiter</groupId>
19+
<artifactId>junit-jupiter-engine</artifactId>
20+
<version>5.9.3</version>
21+
<scope>test</scope>
22+
</dependency>
23+
</dependencies>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
<version>3.11.0</version>
31+
<configuration>
32+
<source>17</source>
33+
<target>17</target>
34+
<showWarnings>false</showWarnings>
35+
</configuration>
36+
</plugin>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-resources-plugin</artifactId>
40+
<version>3.3.1</version>
41+
<configuration>
42+
<escapeString>\</escapeString>
43+
<encoding>UTF-8</encoding>
44+
</configuration>
45+
</plugin>
46+
</plugins>
47+
48+
</build>
49+
50+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#set( $symbol_pound = '#' )
2+
#set( $symbol_dollar = '$' )
3+
#set( $symbol_escape = '\' )
4+
package ${package};
5+
6+
import org.junit.jupiter.api.BeforeAll;
7+
8+
public abstract class AbstractSdkTest {
9+
10+
protected static String ASTRA_DB_APPLICATION_TOKEN;
11+
protected static String ASTRA_DB_ID;
12+
protected static String ASTRA_DB_REGION;
13+
protected static String ASTRA_DB_KEYSPACE;
14+
15+
public static void loadRequiredEnvironmentVariables() {
16+
ASTRA_DB_APPLICATION_TOKEN = System.getenv("ASTRA_DB_APPLICATION_TOKEN");
17+
if (ASTRA_DB_APPLICATION_TOKEN == null) {
18+
throw new IllegalArgumentException("ASTRA_DB_APPLICATION_TOKEN variables is not defined");
19+
}
20+
ASTRA_DB_ID = System.getenv("ASTRA_DB_ID");
21+
if (ASTRA_DB_ID == null) {
22+
throw new IllegalArgumentException("ASTRA_DB_ID variables is not defined");
23+
}
24+
ASTRA_DB_REGION = System.getenv("ASTRA_DB_REGION");
25+
if (ASTRA_DB_REGION == null) {
26+
throw new IllegalArgumentException("ASTRA_DB_REGION variables is not defined");
27+
}
28+
ASTRA_DB_KEYSPACE = System.getenv("ASTRA_DB_KEYSPACE");
29+
if (ASTRA_DB_KEYSPACE == null) {
30+
throw new IllegalArgumentException("ASTRA_DB_KEYSPACE variables is not defined");
31+
}
32+
}
33+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#set( $symbol_pound = '#' )
2+
#set( $symbol_dollar = '$' )
3+
#set( $symbol_escape = '\' )
4+
package ${package}.db;
5+
6+
import com.datastax.astra.sdk.AstraClient;
7+
import ${package}.AbstractSdkTest;
8+
import com.datastax.oss.driver.api.core.CqlSession;
9+
import com.datastax.oss.driver.api.core.cql.ResultSet;
10+
import com.datastax.oss.driver.api.core.cql.Row;
11+
import org.junit.jupiter.api.Assertions;
12+
import org.junit.jupiter.api.BeforeAll;
13+
import org.junit.jupiter.api.Test;
14+
15+
/**
16+
* Test Class to work with SDK and CQL.
17+
* Variables are defined and loading in Super class @see {@link AbstractSdkTest}
18+
*/
19+
public class AstraCqlApiTest extends AbstractSdkTest {
20+
21+
@BeforeAll
22+
public static void init() {
23+
loadRequiredEnvironmentVariables();
24+
}
25+
26+
@Test
27+
public void shouldConnectWithCql() {
28+
29+
try (AstraClient astraClient = AstraClient.builder()
30+
.withToken(ASTRA_DB_APPLICATION_TOKEN) // credentials are mandatory
31+
.withDatabaseId(ASTRA_DB_ID) // identifier of the database
32+
.withDatabaseRegion(ASTRA_DB_REGION) // connection is different for each dc
33+
.enableCql() // as stateful, connection is not always establish
34+
.enableDownloadSecureConnectBundle() // secure connect bundles can be downloaded
35+
.withCqlKeyspace(ASTRA_DB_KEYSPACE) // target keyspace
36+
.build()) {
37+
38+
// Session is already opened for you
39+
CqlSession cqlSession = astraClient.cqlSession();
40+
41+
// Sample Usage
42+
Row row = cqlSession.execute("SELECT cql_version from system.local").one();
43+
Assertions.assertNotNull(row);
44+
Assertions.assertNotNull(row.getString("cql_version"));
45+
System.out.println("Your db is running on " + row.getString("cql_version"));
46+
}
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#set( $symbol_pound = '#' )
2+
#set( $symbol_dollar = '$' )
3+
#set( $symbol_escape = '\' )
4+
package ${package}.db;
5+
6+
import com.datastax.astra.sdk.AstraClient;
7+
import ${package}.AbstractSdkTest;
8+
import io.stargate.sdk.core.domain.Page;
9+
import io.stargate.sdk.doc.CollectionClient;
10+
import io.stargate.sdk.doc.Document;
11+
import io.stargate.sdk.doc.StargateDocumentApiClient;
12+
import org.junit.jupiter.api.BeforeAll;
13+
import org.junit.jupiter.api.Test;
14+
15+
import java.util.stream.Collectors;
16+
17+
public class AstraDocumentApiTest extends AbstractSdkTest {
18+
19+
@BeforeAll
20+
public static void init() {
21+
loadRequiredEnvironmentVariables();
22+
}
23+
24+
@Test
25+
public void shouldConnectWithDocument() {
26+
27+
try (AstraClient astraClient = AstraClient.builder()
28+
.withToken(ASTRA_DB_APPLICATION_TOKEN) // credentials are mandatory
29+
.withDatabaseId(ASTRA_DB_ID) // identifier of the database
30+
.withDatabaseRegion(ASTRA_DB_REGION) // endpoint contains region
31+
.build()) {
32+
33+
// Each Api got a dedicated specialized client
34+
StargateDocumentApiClient apiDoc = astraClient.apiStargateDocument();
35+
36+
// list namespaces
37+
System.out.println("- List namespaces : " +
38+
apiDoc.namespaceNames().collect(Collectors.toList()));
39+
40+
// List Collection in a Keyspace
41+
System.out.println("- List collections : " +
42+
apiDoc.namespace(ASTRA_DB_KEYSPACE).collectionNames().collect(Collectors.toList()));
43+
44+
// Working with collections
45+
CollectionClient cc = apiDoc.namespace(ASTRA_DB_KEYSPACE).collection("user");
46+
if (!cc.exist())
47+
cc.create();
48+
49+
// Working with documents
50+
51+
// Creation => id can be generated by astra
52+
System.out.println(cc.create(new User("John", "Connor")));
53+
// Creation => id can also be enforced like here
54+
cc.document("sara").upsert(new User("Sarah", "Connor"));
55+
56+
// List Documents
57+
Page<Document<User>> page1 = cc.findPage(User.class);
58+
for (Document<User> doc : page1.getResults()) {
59+
System.out.println("Document " + doc.getDocumentId()
60+
+ " with user " + doc.getDocument().getFirstname()
61+
+ " " + doc.getDocument().getLastname());
62+
}
63+
64+
/* Go for page 2
65+
PageableQuery query = PageableQuery.builder().build();
66+
query.setPageState(page1.getPageState().get());
67+
Page<Document<String>> page2 = cc.findPage(query);
68+
*/
69+
}
70+
}
71+
72+
public static final class User {
73+
String firstname;
74+
String lastname;
75+
public User() {}
76+
public User(String firstname, String lastname) {
77+
this.firstname = firstname;
78+
this.lastname = lastname;
79+
}
80+
public String getFirstname() { return firstname; }
81+
public String getLastname() { return lastname; }
82+
public void setFirstname(String firstname) { this.firstname = firstname; }
83+
public void setLastname(String lastname) { this.lastname = lastname; }
84+
}
85+
86+
}
87+
88+
89+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#set( $symbol_pound = '#' )
2+
#set( $symbol_dollar = '$' )
3+
#set( $symbol_escape = '\' )
4+
package ${package}.db;
5+
6+
import com.datastax.astra.sdk.AstraClient;
7+
import ${package}.AbstractSdkTest;
8+
import io.stargate.sdk.core.Ordering;
9+
import io.stargate.sdk.rest.StargateRestApiClient;
10+
import io.stargate.sdk.rest.domain.CreateTable;
11+
import io.stargate.sdk.rest.domain.SearchTableQuery;
12+
import org.junit.jupiter.api.BeforeAll;
13+
import org.junit.jupiter.api.Test;
14+
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
import java.util.stream.Collectors;
18+
19+
public class AstraGraphQLApiTest extends AbstractSdkTest {
20+
21+
@BeforeAll
22+
public static void init() {
23+
loadRequiredEnvironmentVariables();
24+
}
25+
26+
@Test
27+
public void shouldConnectWithGraphQL() {
28+
29+
try (AstraClient astraClient = AstraClient.builder()
30+
.withToken(ASTRA_DB_APPLICATION_TOKEN) // credentials are mandatory
31+
.withDatabaseId(ASTRA_DB_ID) // identifier of the database
32+
.withDatabaseRegion(ASTRA_DB_REGION) // endpoint contains region
33+
.build()) {
34+
35+
// List Keyspaces
36+
System.out.println("Keyspaces:" + astraClient
37+
.apiStargateGraphQL()
38+
.keyspaceDDL()
39+
.keyspaces());
40+
41+
// List Tables
42+
String getTables = "query GetTables {\n"
43+
+ " keyspace(name: \"" + ASTRA_DB_KEYSPACE + "\") {\n"
44+
+ " name\n"
45+
+ " tables {\n"
46+
+ " name\n"
47+
+ " columns {\n"
48+
+ " name\n"
49+
+ " kind\n"
50+
+ " type {\n"
51+
+ " basic\n"
52+
+ " info {\n"
53+
+ " name\n"
54+
+ " }\n"
55+
+ " }\n"
56+
+ " }\n"
57+
+ " }\n"
58+
+ " }\n"
59+
+ "}";
60+
61+
System.out.println("Tables : " + astraClient
62+
.apiStargateGraphQL()
63+
.keyspaceDDL()
64+
.execute(getTables));
65+
}
66+
}
67+
}
68+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#set( $symbol_pound = '#' )
2+
#set( $symbol_dollar = '$' )
3+
#set( $symbol_escape = '\' )
4+
package ${package}.db;
5+
6+
import com.datastax.astra.sdk.AstraClient;
7+
import ${package}.AbstractSdkTest;
8+
import org.junit.jupiter.api.BeforeAll;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class AstraGrpcApiTest extends AbstractSdkTest {
12+
13+
@BeforeAll
14+
public static void init() {
15+
loadRequiredEnvironmentVariables();
16+
}
17+
18+
@Test
19+
public void shouldConnectWithGraphQL() {
20+
21+
try (AstraClient astraClient = AstraClient.builder()
22+
.withToken(ASTRA_DB_APPLICATION_TOKEN) // credentials are mandatory
23+
.withDatabaseId(ASTRA_DB_ID) // identifier of the database
24+
.withDatabaseRegion(ASTRA_DB_REGION) // endpoint contains region
25+
.enableGrpc() // Import to start the grpc Channel
26+
.build()) {
27+
28+
System.out.println("+ Cql Version (grpc) : " + astraClient
29+
.apiStargateGrpc()
30+
.execute("SELECT cql_version from system.local")
31+
.one().getString("cql_version"));
32+
}
33+
}
34+
}
35+

0 commit comments

Comments
 (0)