Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration with spring boot #212

Open
dzasti opened this issue Sep 26, 2023 · 5 comments
Open

Integration with spring boot #212

dzasti opened this issue Sep 26, 2023 · 5 comments

Comments

@dzasti
Copy link

dzasti commented Sep 26, 2023

Hello, is there a possibility to run liquibase cassandra extension via spring boot? Can’t find any example online. Also I’ve done some research: spring-projects/spring-boot#29991 (comment) , #4 (comment). Seems confusing, could someone show me an example, if its possible ?

thanks

@maximevw
Copy link
Contributor

maximevw commented Sep 30, 2023

Hello @dzasti,

It should be possible to use Spring Liquibase with Cassandra as I successfully ran it with the example below.

First of all, I used a slightly modified version of liquibase-cassandra using cassandra-jdbc-wrapper (4.9.1 or above). For further details, see here: #148 (comment)
But, I guess it should also work with Simba JDBC driver (if you have an appropriate licence).

The dependencies to include in your POM file are:

<!-- Using Spring Boot 3.1.4 and Java 17 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
	<groupId>org.liquibase</groupId>
	<artifactId>liquibase-core</artifactId>
</dependency>
<!-- Include cassandra-jdbc-wrapper if you don't use Simba JDBC, otherwise include a dependency to Simba driver -->
<dependency>
	<groupId>com.ing.data</groupId>
	<artifactId>cassandra-jdbc-wrapper</artifactId>
	<version>4.10.0</version>
</dependency>
<dependency>
	<groupId>org.liquibase.ext</groupId>
	<artifactId>liquibase-cassandra</artifactId>
	<!-- Use here a version modified to use cassandra-jdbc-wrapper instead of Simba if necessary-->
	<version>4.23.2</version>
</dependency>

<!-- Required to avoid runtime exceptions due to a conflict of version between Spring Boot dependencies and cassandra-jdbc-wrapper -->
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>com.datastax.oss</groupId>
			<artifactId>java-driver-core</artifactId>
			<version>4.17.0</version>
		</dependency>
	</dependencies>
</dependencyManagement>

Assuming you have a Cassandra instance running on localhost, port 9042, with a keyspace named test_keyspace.

In your application.properties file, you should have this:

# Liquibase configuration
spring.liquibase.url=jdbc:cassandra://localhost:9042/test_keyspace?localdatacenter=datacenter1&user=cassandra&password=cassandra&compliancemode=Liquibase
spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.yaml
spring.liquibase.driver-class-name=com.ing.data.cassandra.jdbc.CassandraDriver

# Spring datasource configuration
spring.datasource.name=cassandra_datasource
spring.datasource.url=jdbc:cassandra://localhost:9042/test_keyspace?localdatacenter=datacenter1&user=cassandra&password=cassandra
spring.datasource.driver-class-name=com.ing.data.cassandra.jdbc.CassandraDriver
spring.datasource.hikari.minimum-idle=1

In your project resources, you should have the following files:

  • db/changelog/db.changelog-master.yaml
databaseChangeLog:
  - include:
      file: classpath:/db/changelog/init.sql
  • db/changelog/init.sql. Note the extension MUST be "sql" otherwise Liquibase will fail to parse the script.
USE test_keyspace;
CREATE TABLE IF NOT EXISTS tbl_test (
keyname text PRIMARY KEY,
bool_col boolean,
int_col int);

INSERT INTO tbl_test(keyname, bool_col, int_col) VALUES ('key1', true, 1) IF NOT EXISTS;
INSERT INTO tbl_test(keyname, bool_col, int_col) VALUES ('key2', false, 2) IF NOT EXISTS;

Finally, your Spring Boot application:

@SpringBootApplication
public class SpringLiquibaseCassandraApplication {

	@Autowired
	JdbcTemplate jdbcTemplate;

	@EventListener(ApplicationReadyEvent.class)
	public void checkData() throws SQLException {
		jdbcTemplate.query("SELECT * FROM tbl_test", rs -> {
            System.out.println("------------------------------------");
            System.out.println("keyname:  " + rs.getString(1));
            System.out.println("bool_col: " + rs.getBoolean(2));
            System.out.println("int_col:  " + rs.getInt(3));
        });
    }

	public static void main(String[] args) {
		SpringApplication.run(SpringLiquibaseCassandraApplication.class, args);
	}

}

Running your application, the output should be the following, proving the Liquibase changes have been executed successfully:

------------------------------------
keyname:  key1
bool_col: true
int_col:  1
------------------------------------
keyname:  key2
bool_col: false
int_col:  2

@shubha11m
Copy link

shubha11m commented May 25, 2024

as like we are connecting sql db from spring boot using liquibase it is possible to connect aws keyspace using liquibase without jdbc
in this example you guys put local cassendra can we use amazone keypace to run our jar at ec2 and connect with keyspace and create tables there

@maximevw
Copy link
Contributor

@shubha11m Liquibase is based on JDBC, so it's not possible to use it without JDBC. But it should not really be a problem.
Theoretically, it could be possible to connect to AWS Keyspaces, but as I said in previous discussions, some operations performed by Liquibase are currently not supported by AWS Keyspaces (see #297 for further details).
However, to connect to AWS Keyspaces using JDBC, you could use this minimal connection string:
jdbc:cassandra://cassandra.{ec2-region}.amazonaws.com:9142/{keyspace}?compliancemode=Liquibase (the parameter compliancemode=Liquibase is only required when used with Liquibase, {keyspace} is the target keyspace you want to use and {ec2-region} has to be replaced by the appropriate EC2 region of your server). The documentation of the JDBC connection string for the driver used by liquibase-cassandra is available here.

@shubha11m
Copy link

shubha11m commented May 25, 2024

HI @maximevw thankyou for you response so as previously the snip of code is there to connect keyspace with spring boot is it whole code or do we need to add cql config file and other than that and are we not able to put our changes into xml file rather then .sql

i had applied all changes #212 (comment)
but when i run the appliction its just run as plain spring boot application and not executing the
the liquibase chnagelog file

@maximevw
Copy link
Contributor

Hi @shubha11m

The example given in this issue is just an example among a lot of possible configurations, so, obviously you can adapt it according to your needs. If you prefer use xml changelog, you can.

A lot of documentation about running Liquibase with SpringBoot is available here and in the linked pages.

Maybe try to specify the changelog location in the application.properties file:

spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml # Adapt the path to match your changelog file.

Also, ensure the dependencies are up-to-date (those mentioned in this example are old and may contain some bugs):

<dependency>
	<groupId>com.ing.data</groupId>
	<artifactId>cassandra-jdbc-wrapper</artifactId>
	<version>4.12.0</version>
</dependency>
<dependency>
	<groupId>org.liquibase.ext</groupId>
	<artifactId>liquibase-cassandra</artifactId>
	<version>4.28.0</version>
</dependency>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants