-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jesse Yates
committed
Apr 10, 2017
1 parent
7736cac
commit ad37e7b
Showing
6 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
java/example/src/main/java/io/fineo/client/example/Event.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
136
java/example/src/main/java/io/fineo/client/example/ExampleDriver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters