forked from GoogleCloudPlatform/DataflowTemplates
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...rcedb/src/main/java/com/google/cloud/teleport/v2/templates/utils/CassandraConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.google.cloud.teleport.v2.templates.utils; | ||
import com.datastax.oss.driver.api.core.CqlSession; | ||
import com.datastax.oss.driver.api.core.auth.ProgrammaticPlainTextAuthProvider; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
public class CassandraConnection { | ||
private static CassandraConnection instance; | ||
private CqlSession session; | ||
|
||
// Private constructor to enforce Singleton | ||
private CassandraConnection(String host, int port, String datacenter, String username, String password) { | ||
try { | ||
session = CqlSession.builder() | ||
.addContactPoint(new InetSocketAddress(host, port)) | ||
.withLocalDatacenter(datacenter) | ||
// .withAuthProvider(new ProgrammaticPlainTextAuthProvider(username, password)) // Use if authentication is enabled | ||
.build(); | ||
session.execute("USE mykeyspace;"); | ||
|
||
System.out.println("Connected to Cassandra!"); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to connect to Cassandra", e); | ||
} | ||
} | ||
public static synchronized CassandraConnection getInstance(String host, int port, String datacenter, String username, String password) { | ||
if (instance == null) { | ||
instance = new CassandraConnection(host, port, datacenter, username, password); | ||
} | ||
return instance; | ||
} | ||
|
||
// Method to get the CqlSession | ||
public CqlSession getSession() { | ||
return session; | ||
} | ||
|
||
// Close the session when done | ||
public void close() { | ||
if (session != null) { | ||
session.close(); | ||
System.out.println("Cassandra connection closed."); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...-to-sourcedb/src/main/java/com/google/cloud/teleport/v2/templates/utils/CassandraDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.google.cloud.teleport.v2.templates.utils; | ||
|
||
import java.io.Serializable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import com.datastax.oss.driver.api.core.CqlSession; | ||
/** Writes data to MySQL. */ | ||
public class CassandraDao implements Serializable { | ||
private static final Logger LOG = LoggerFactory.getLogger(CassandraDao.class); | ||
String cassandraHost; | ||
String cassandraUser; | ||
String cassandraPasswd; | ||
int port; | ||
String datacenter; | ||
|
||
public CassandraDao(String cassandraHost, int cassandraPort, String dataCenter, String cassandraUser, String cassandraPasswd) { | ||
this.cassandraHost = cassandraHost; | ||
this.port = cassandraPort; | ||
this.cassandraUser = cassandraUser; | ||
this.cassandraPasswd = cassandraPasswd; | ||
this.datacenter = dataCenter; | ||
} | ||
|
||
// writes to database | ||
public void write(String cassandraStatement) { | ||
CassandraConnection cassandraConnection = null; | ||
CqlSession session = null; | ||
|
||
try { | ||
cassandraConnection = CassandraConnection.getInstance(this.cassandraHost , this.port, this.datacenter, this.cassandraUser,this.cassandraPasswd); | ||
session = cassandraConnection.getSession(); | ||
session.execute(cassandraStatement); | ||
} catch (Exception e) { | ||
System.err.println("An error occurred while interacting with Cassandra:"); | ||
e.printStackTrace(); | ||
} finally { | ||
// Ensure the connection is closed in the finally block | ||
if (cassandraConnection != null) { | ||
cassandraConnection.close(); | ||
} | ||
} | ||
} | ||
} | ||
|