Developing Applications
A Simple Stock Quote Application
Logical Steps
Data Feed Provider --> Historical Data --> Stock Screener --> Alert List
Implementation Approach
- Data Source: Yahoo! finance
- User Interface: Console application
- Database: Cassandra
- Programming Language: Java
- Intergrated Development Environment: Eclipse IDE
- Dependency Management: Maven
- Java Cassandra Driver: DataStax Java Driver for Cassandra
- Technical Analysis: TA-Lib
Connect Java to Cassandra
Simple Client of Cassandra
package CassandraClient;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.Session;
public class CassandraClient {
private Cluster cluster;
private Session session;
public void connect(String node) {
cluster = Cluster.builder()
.addContactPoint(node)
.build();
Metadata metadata = cluster.getMetadata();
System.out.printf("Connected to cluster: %s\n", metadata.getClusterName());
for (Host host: metadata.getAllHosts()) {
System.out.printf("Datacenter: %s; Host: %s; Rack: %s\n",
host.getDatacenter(), host.getAddress(), host.getRack());
}
session = cluster.connect();
}
public void close() {
cluster.close();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CassandraClient client = new CassandraClient();
client.connect("127.0.0.1");
System.out.println("Already connected!");
client.close();
System.out.println("Already close!");
}
}
Dependencies
If use datastax's java driver, may need involve netty.
Besides, when using maven to manage dependencies, the build plugin should use shade
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rapidcassandra</groupId>
<artifactId>Chapter0301</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>uber-${artifactId}-${version}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Design Steps
Data Model
- Keyspace: packtcdma
- Stock Quote Data
Queries:
- Save financial data feed into the quote table
INSERT INTO quote (symbol, price_time, open_price, high_price, low_price, close_price, volume) VALUE (?,?,?,?,?,?)
- Select historical data of a stock over a date period
SELECT * FROM quote WHERE symbol=? AND price_time >= ? AND price_time <= ?
- Get the latest date of the historical data of a stock
- SELECT price_time FROM quote WHERE symbol = ? ORDER BY price_time DESC LIMIT 1
Java Classes
- CassandraTrader: Main program
- YahooFetcher: Handle the collection of Yahoo! Finance data feed
- Quote: POJO of quote
- CassandraManager: manage the connection to Cassandra
- CassandraTraderDAO: Data assess object handling persistence and queries
- TradingSignal: Core logic for scanning trading signals
- CassandraTraderUtil: Utilities for conversion between string, date, and decimal