From dba3977c7ee72a580d8c30fa04e3669283f22559 Mon Sep 17 00:00:00 2001 From: SoftwareObservatorium Date: Tue, 23 May 2023 17:40:46 +0200 Subject: [PATCH] cassandra beispiel --- cassandra/.gitignore | 38 +++++++++++++++++++ cassandra/pom.xml | 32 ++++++++++++++++ .../src/main/java/org/example/QueryApp.java | 30 +++++++++++++++ .../src/main/java/org/example/QueryApp.java | 2 +- 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 cassandra/.gitignore create mode 100644 cassandra/pom.xml create mode 100644 cassandra/src/main/java/org/example/QueryApp.java diff --git a/cassandra/.gitignore b/cassandra/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/cassandra/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/cassandra/pom.xml b/cassandra/pom.xml new file mode 100644 index 0000000..27ba51c --- /dev/null +++ b/cassandra/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + de.hsma + cassandra + 1.0-SNAPSHOT + jar + + cassandra + + + UTF-8 + 11 + 11 + + + + + com.datastax.oss + java-driver-core + 4.15.0 + + + + org.junit.jupiter + junit-jupiter-engine + 5.9.2 + test + + + diff --git a/cassandra/src/main/java/org/example/QueryApp.java b/cassandra/src/main/java/org/example/QueryApp.java new file mode 100644 index 0000000..fded234 --- /dev/null +++ b/cassandra/src/main/java/org/example/QueryApp.java @@ -0,0 +1,30 @@ +package org.example; + +import com.datastax.oss.driver.api.core.CqlSession; +import com.datastax.oss.driver.api.core.cql.ResultSet; +import com.datastax.oss.driver.api.core.cql.Row; + +import java.net.InetSocketAddress; + +public class QueryApp { + + /** + * + * @param args + * + * @see Manual + */ + public static void main(String[] args) { + try (CqlSession session = CqlSession.builder() + .addContactPoint(new InetSocketAddress("localhost", 9042)) // = CqlSession.builder().build() + .withLocalDatacenter("datacenter1") // benötigt wenn contact point explizit angegeben + .withKeyspace("test") // unser keyspace (z.B. use test;) + .build()) { + ResultSet rs = session.execute("select * from professoren"); + for (Row row : rs) { + System.out.println(row.getString("name")); + //System.out.println(row.getFormattedContents()); + } + } + } +} \ No newline at end of file diff --git a/mongodb/src/main/java/org/example/QueryApp.java b/mongodb/src/main/java/org/example/QueryApp.java index 2061478..2766f2b 100644 --- a/mongodb/src/main/java/org/example/QueryApp.java +++ b/mongodb/src/main/java/org/example/QueryApp.java @@ -12,7 +12,7 @@ public class QueryApp { // Replace the placeholder with your MongoDB deployment's connection string String uri = "mongodb://localhost:27017"; try (MongoClient mongoClient = MongoClients.create(uri)) { - MongoDatabase database = mongoClient.getDatabase("mytest"); + MongoDatabase database = mongoClient.getDatabase("hs"); MongoCollection collection = database.getCollection("professoren"); Document doc = collection.find(eq("Name", "Sokrates")).first(); if (doc != null) {