Skip to content

Commit

Permalink
Adding a simple example driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Yates committed Apr 10, 2017
1 parent 7736cac commit ad37e7b
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Welcome to the Fineo platform! With Fineo you can easily upload data from connec

This guide is going to walk through connecting a simple device, creating a schema, sending data and then reading the data back. Then it will show to flexibly manage mistakes and how to evolve schema.

For those interested in just jumping into writing code, a simple example driver is [available](https://github.com/fineoio/fineo-client/blob/master/java/example/src/main/java/io/fineo/client/tools/ExampleDriver.java).

## Creating a device

The 'Devices' tab is the first screen shown when logging into the [web application]. Add a new device by clicking the '+' button. The platform will generate a unique ID for each device.
Expand Down
44 changes: 44 additions & 0 deletions java/example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>fineo-client-parent</artifactId>
<groupId>io.fineo.client</groupId>
<version>1.1.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>example</artifactId>
<name>Fineo Client - Example</name>
<description>A simple example project for interacting with the Fineo API</description>

<dependencies>
<dependency>
<groupId>io.fineo.client</groupId>
<artifactId>common</artifactId>
</dependency>
<dependency>
<groupId>io.fineo.client</groupId>
<artifactId>jdbc</artifactId>
</dependency>
<dependency>
<groupId>io.fineo.client.models</groupId>
<artifactId>schema</artifactId>
</dependency>
<dependency>
<groupId>io.fineo.client.models</groupId>
<artifactId>write</artifactId>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
54 changes: 54 additions & 0 deletions java/example/src/main/java/io/fineo/client/example/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.fineo.client.example;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
*
*/
public class Event {
private final String metrictype = ExampleDriver.METRIC_NAME;
private String f1;
private int f2;
private long timestamp;

public Event(String f1, int f2) {
this.f1 = f1;
this.f2 = f2;
this.timestamp = System.currentTimeMillis();
}

@JsonProperty("metrictype")
public String getMetrictype() {
return metrictype;
}

@JsonProperty("f1")
public String getF1() {
return f1;
}

public Event withF1(String f1) {
this.f1 = f1;
return this;
}

@JsonProperty("f2")
public int getF2() {
return f2;
}

public Event withF2(int f2) {
this.f2 = f2;
return this;
}

@JsonProperty("timestamp")
public long getTimestamp() {
return timestamp;
}

public Event withTimestamp(long timestamp) {
this.timestamp = timestamp;
return this;
}
}
136 changes: 136 additions & 0 deletions java/example/src/main/java/io/fineo/client/example/ExampleDriver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package io.fineo.client.example;

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.fineo.client.FineoClientBuilder;
import io.fineo.client.model.schema.SchemaApi;
import io.fineo.client.model.schema.field.CreateFieldRequest;
import io.fineo.client.model.schema.metric.CreateMetricRequest;
import io.fineo.client.model.write.StreamWrite;
import io.fineo.read.Driver;
import io.fineo.read.jdbc.FineoConnectionProperties;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static java.lang.String.format;
import static java.lang.Thread.sleep;

/**
* A simple example for creating a simple schema, uploading data and then reading that data back.
* <p>
* API KEY is the first argument. Access credentials are loaded using the default AWS chain
* {@link DefaultAWSCredentialsProviderChain}.
* <p>
* After running this driver you should be able to see the 'example metric' schema in the
* <a href="http://app.fineo.io>UI</a> and be able to read two rows from the 'example metric' table.
*
* @see DefaultAWSCredentialsProviderChain
*/
public class ExampleDriver {

static final String METRIC_NAME = "example metric";

public static void main(String[] args) throws Exception {
String apikey = args[0];
FineoClientBuilder builder = new FineoClientBuilder()
.withApiKey(apikey)
.withCredentials(new DefaultAWSCredentialsProviderChain());
setupSchema(builder);
write(builder);
read(apikey, args[1], args[2]);
}

private static void setupSchema(FineoClientBuilder builder) throws Exception {
System.out.println("Starting to create table/metric type: " + METRIC_NAME);
try (SchemaApi.Metric metrics = builder.build(SchemaApi.Metric.class);
SchemaApi.Field fields = builder.build(SchemaApi.Field.class)) {
CreateMetricRequest cm = new CreateMetricRequest().setMetricName(METRIC_NAME);
metrics.createMetric(cm);

// create fields matching the 'Event' to send below
CreateFieldRequest field = new CreateFieldRequest()
.setFieldType("VARCHAR")
.setFieldName("f1")
.setMetricName(METRIC_NAME);
fields.createField(field);

field.setFieldType("INTEGER")
.setFieldName("f2");
fields.createField(field);
}
System.out.println("Completed creating table/metric type: " + METRIC_NAME);
}

private static void write(FineoClientBuilder builder) throws Exception {
System.out.println(format("Starting to write events to '%s'", METRIC_NAME));
try (StreamWrite stream = builder.build(StreamWrite.class)) {
// we can just send the regular event because we have:
// - metrictype (case sensitive!)
// - timestamp
// - JSON annotations for fields such that the ObjectMapper can translate (a simple POJO
// will work here as well)
stream.writeEvent(new Event("first", 1));
// wait to get a new timestamp, otherwise this will look like the same row
sleep(1000);
stream.writeEvent(new Event("second", 2));
}
System.out.println(format("Completed writing events to '%s'", METRIC_NAME));
}

private static void read(String apikey, String username, String password)
throws InterruptedException, SQLException, JsonProcessingException {
System.out.println("Starting to read data from Fineo");

// ensure the driver is loaded
Driver.load();

// make the connection with our api key set.
// you could also do this as:
// DriverManager.getConnection("jdbc:fineo:api_key=<key>", username, password)
Properties props = new Properties();
FineoConnectionProperties.API_KEY.set(props, apikey);
props.setProperty("username", username);
props.setProperty("passsword", password);

// get the connection and attempt to read
try (Connection conn = DriverManager.getConnection("jdbc:fineo", props)) {
long start = System.currentTimeMillis();
while (readInternal(conn) == 0) {
sleep(100);
}
Duration d = Duration.ofMillis(System.currentTimeMillis() - start);
System.out.println();
System.out.println("Inconsistency window between write and read completion:");
System.out.println(d.toMillis() + " ms (" + d.toMillis() / 1000.0 + " sec)");
}
}

private static int readInternal(Connection conn)
throws SQLException, JsonProcessingException {
int rowCount = 0;
try (Statement stmt = conn.createStatement();
ResultSet set = stmt.executeQuery(format("SELECT * FROM `%s`", METRIC_NAME))) {
// just page through the results and print them out
List<Map<String, Object>> results;
while (set.next()) {
rowCount++;
MapListHandler handler = new MapListHandler();
results = handler.handle(set);
ObjectMapper mapper = new ObjectMapper();
String out = mapper.writeValueAsString(results);
System.out.println(out);
}
}
return rowCount;
}
}
24 changes: 24 additions & 0 deletions java/example/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %c{3}[%L] - %m%n</pattern>
</encoder>
</appender>


<logger name="io.fineo" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>

<logger name="fineo.client" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<!-- this logs like crazy at DEBUG -->
<logger name="fineo.client.org.asynchttpclient" level="INFO" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>

<root level="WARN">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
1 change: 1 addition & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<module>tools</module>
<module>schema</module>
<module>cognito-auth</module>
<module>example</module>
</modules>
<packaging>pom</packaging>

Expand Down

0 comments on commit ad37e7b

Please sign in to comment.