Package com.privalia.qa.utils
Class CassandraUtils
- java.lang.Object
-
- com.privalia.qa.utils.CassandraUtils
-
public class CassandraUtils extends Object
Generic utilities for operations over Cassandra.
-
-
Constructor Summary
Constructors Constructor Description CassandraUtils()
Generic contructor of CassandraUtils.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
buildCluster()
Build a Cassandra cluster.void
connect()
Connect to Cassandra host.void
createKeyspace(String keyspace)
Create a keyspace in Cassandra.void
createTableWithData(String table, Map<String,String> colums, ArrayList<String> pk)
Create a table in Cassandra.void
disconnect()
Disconnect from Cassandra host.void
dropKeyspace(boolean ifExists, String keyspace)
Drop a keyspace in Cassandra.void
dropKeyspace(String keyspace)
Drop a keyspace in Cassandra.void
dropTable(String table)
Drop a table of a keyspace.void
executeQueriesList(List<String> queriesList)
Execute a list of queries over Cassandra.com.datastax.driver.core.ResultSet
executeQuery(String query)
Execute a query over Cassandra.boolean
existsKeyspace(String keyspace, boolean showLog)
Checks if a keyspace exists in Cassandra.boolean
existsTable(String keyspace, String table, boolean showLog)
Checks if a keyspace contains an especific table.List<String>
getKeyspaces()
Get a list of the existing keyspaces in Cassandra.com.datastax.driver.core.Metadata
getMetadata()
Get the metadata of the Cassandra Cluster.com.datastax.driver.core.Session
getSession()
Get the cassandra session.List<String>
getTables(String keyspace)
Get tables of a keyspace.void
insertData(String table, Map<String,Object> fields)
Insert data in a keyspace.static List<String>
loadScript(String path)
Load the lines of a CQL script containing one statement per line into a list.void
loadTestData(String keyspace, String path)
Load akeyspace
in Cassandra using the CQL sentences in the script path.void
reconnect()
Reconnect to Cassandra host.void
truncateTable(String table)
Truncate a table of a keyspace.void
useKeyspace(String keyspace)
Use a keyspace in Cassandra.
-
-
-
Method Detail
-
connect
public void connect()
Connect to Cassandra host.
-
executeQuery
public com.datastax.driver.core.ResultSet executeQuery(String query)
Execute a query over Cassandra.- Parameters:
query
- the query- Returns:
- ResultSet result set
-
executeQueriesList
public void executeQueriesList(List<String> queriesList)
Execute a list of queries over Cassandra.- Parameters:
queriesList
- the queries list
-
reconnect
public void reconnect()
Reconnect to Cassandra host.
-
disconnect
public void disconnect() throws DBException
Disconnect from Cassandra host.- Throws:
DBException
- DBException
-
getMetadata
public com.datastax.driver.core.Metadata getMetadata() throws DBException
Get the metadata of the Cassandra Cluster.- Returns:
- Metadata metadata
- Throws:
DBException
- the db exception
-
buildCluster
public void buildCluster()
Build a Cassandra cluster.
-
getSession
public com.datastax.driver.core.Session getSession()
Get the cassandra session.- Returns:
- Session
-
createKeyspace
public void createKeyspace(String keyspace)
Create a keyspace in Cassandra.- Parameters:
keyspace
- the keyspace
-
createTableWithData
public void createTableWithData(String table, Map<String,String> colums, ArrayList<String> pk)
Create a table in Cassandra.- Parameters:
table
- the table namecolums
- columnspk
- the pk
-
insertData
public void insertData(String table, Map<String,Object> fields)
Insert data in a keyspace.- Parameters:
table
- the tablefields
- the fields
-
existsKeyspace
public boolean existsKeyspace(String keyspace, boolean showLog)
Checks if a keyspace exists in Cassandra.- Parameters:
keyspace
- the keyspaceshowLog
- if log should be displayed- Returns:
- boolean boolean
-
getKeyspaces
public List<String> getKeyspaces()
Get a list of the existing keyspaces in Cassandra.- Returns:
List<String>
-
dropKeyspace
public void dropKeyspace(String keyspace)
Drop a keyspace in Cassandra.- Parameters:
keyspace
- the keyspace
-
dropKeyspace
public void dropKeyspace(boolean ifExists, String keyspace)
Drop a keyspace in Cassandra.- Parameters:
ifExists
- to verify if existskeyspace
- the keyspace
-
useKeyspace
public void useKeyspace(String keyspace)
Use a keyspace in Cassandra.- Parameters:
keyspace
- the keyspace
-
existsTable
public boolean existsTable(String keyspace, String table, boolean showLog)
Checks if a keyspace contains an especific table.- Parameters:
keyspace
- the keyspacetable
- the tableshowLog
- if show log- Returns:
- boolean boolean
-
getTables
public List<String> getTables(String keyspace)
Get tables of a keyspace.- Parameters:
keyspace
- the keyspace- Returns:
List<String>
-
dropTable
public void dropTable(String table)
Drop a table of a keyspace.- Parameters:
table
- the table name
-
truncateTable
public void truncateTable(String table)
Truncate a table of a keyspace.- Parameters:
table
- the table name
-
loadTestData
public void loadTestData(String keyspace, String path)
Load akeyspace
in Cassandra using the CQL sentences in the script path. The script is executed if the keyspace does not exists in Cassandra.- Parameters:
keyspace
- The name of the keyspace.path
- The path of the CQL script.
-
-