In this blog I will present how to get started and how to do simple lookups.
The complete source code (actually a bit extended to show batching) for this example can be found at Severalnines (here). The code you will see below is just snippets.
Environment
To start with you need MySQL Cluster up and running.
For development on localhost then you can get a sandbox from Severalnines.
If you want to have a test/production environment you should you the Configurator for MySQL Cluster.
First of all you have to import the following Cluster/J jars into your project.
In Eclipse, on the project, do "Properties -> Java Build Path --> Add External JARs"
The JARs you need to import are typically located in /usr/local/mysql/share/java/ (if you have installed MySQL Cluster in this location - this is the default location used by Severalnines).
You also need to set the JVM configuration in order to point out the libndbclient library :
-Djava.library.path=/usr/local/mysql/lib/ -Xms128m -Xmx512m
Create the table
The first thing we should do is to create the table we need for this example:
CREATE TABLE `my_data` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`data` varbinary(255) DEFAULT NULL,
`last_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`) ) ENGINE=ndbcluster DEFAULT CHARSET=latin1
Mapping table to Java Interface
An interfae describing each table is needed. You annotate the code with @PrimaryKey and @Column do denote if the names of columns and the column names.
public interface MyData {
@PrimaryKey
long getId();
void setId(long i);
@Column(name = "data")
byte[] getData();
void setData(byte[] b);
@Column(name = "last_updated")
long getLastUpdated();
void setLastUpdated(long ts);
}
Setting up the connection to MySQL ClusterProperties p = new Properties();Creating a Session
p.setProperty("com.mysql.clusterj.connectstring", "localhost:1186");
p.setProperty("com.mysql.clusterj.database", "test");
SessionFactory sf=ClusterJHelper.getSessionFactory(p);
The SessionFactory provides Sessions. On a session instance you can do a couple of things, e.g, makePersistent, updatePersistent, and find.
/**
* Sessions are not thread safe!
*/
Session s = sf.getSession();
Performing an insert
Populate the fields in the object, and make it persistent.
MyData my_data=s.newInstance(MyData.class);That is it, if you run the example code, you will see the output.
/**
* Set the data on the object
*/
my_data.setId(i);
my_data.setData(data);
my_data.setLastUpdated(System.currentTimeMillis());
/**
* Persist the object */
s.makePersistent(my_data);
Done inserting 1000 records in 230 ms