Class CassandraUtils


  • public class CassandraUtils
    extends Object
    Generic utilities for operations over Cassandra.
    • Constructor Detail

      • CassandraUtils

        public CassandraUtils()
        Generic contructor of CassandraUtils.
    • 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 name
        colums - columns
        pk - the pk
      • insertData

        public void insertData​(String table,
                               Map<String,​Object> fields)
        Insert data in a keyspace.
        Parameters:
        table - the table
        fields - the fields
      • existsKeyspace

        public boolean existsKeyspace​(String keyspace,
                                      boolean showLog)
        Checks if a keyspace exists in Cassandra.
        Parameters:
        keyspace - the keyspace
        showLog - 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 exists
        keyspace - 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 keyspace
        table - the table
        showLog - 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 a keyspace 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.
      • loadScript

        public static List<String> loadScript​(String path)
        Load the lines of a CQL script containing one statement per line into a list. l
        Parameters:
        path - The path of the CQL script.
        Returns:
        The contents of the script.