Skip to content

Instantly share code, notes, and snippets.

@ambud
Last active September 23, 2016 23:34
Show Gist options
  • Select an option

  • Save ambud/72575e698d7f318bb3eb80d2a41a1099 to your computer and use it in GitHub Desktop.

Select an option

Save ambud/72575e698d7f318bb3eb80d2a41a1099 to your computer and use it in GitHub Desktop.
OpenTSDB Java Embedded Read Write
<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.test</groupId>
<artifactId>tsdb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>tsdb</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.opentsdb</groupId>
<artifactId>opentsdb</artifactId>
<version>2.3.0-RC1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import net.opentsdb.core.Aggregators;
import net.opentsdb.core.DataPoints;
import net.opentsdb.core.Query;
import net.opentsdb.core.TSDB;
import net.opentsdb.utils.Config;
/**
*
*/
public class Reader {
public static void main(String[] args) {
loadConfiguration();
}
public static void loadConfiguration() {
org.hbase.async.HBaseClient client = null;
try {
client = new org.hbase.async.HBaseClient("xxxxxxx");
client.ensureTableExists("tsdb").joinUninterruptibly();
client.ensureTableExists("tsdb-uid").joinUninterruptibly();
client.setFlushInterval((short) 100);
Config config = new Config("config.props");
TSDB tsdb = new TSDB(client, config);
Map<String, String> tags = new HashMap<>();
tags.put("host", "xxxxxx");
tags.put("metric", "mem.heap.used");
Query query = tsdb.newQuery();
Calendar instance = Calendar.getInstance();
instance.add(Calendar.DATE, -10);
query.setStartTime(instance.getTimeInMillis());
query.setEndTime(System.currentTimeMillis());
query.setTimeSeries("argus.jvm", tags, Aggregators.AVG, false);
query.downsample(10, Aggregators.AVG);
System.out.println("\n\n\n");
for (DataPoints dataPoints : query.run()) {
dataPoints.forEach(dp -> {
if (dp.isInteger()) {
System.out.println(dp.longValue() + "\t" + dp.timestamp());
} else {
System.out.println(dp.doubleValue() + "\t" + dp.timestamp());
}
});
}
System.out.println("\n\n\n");
tsdb.shutdown().join();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.symantec.cpe.volta.tsdb;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import net.opentsdb.core.TSDB;
import net.opentsdb.utils.Config;
/**
*
*/
public class Writer {
public static void main(String[] args) {
loadConfiguration();
}
public static void loadConfiguration() {
org.hbase.async.HBaseClient client = null;
try {
client = new org.hbase.async.HBaseClient("xxxx");
client.ensureTableExists("tsdb").joinUninterruptibly();
client.ensureTableExists("tsdb-uid").joinUninterruptibly();
client.setFlushInterval((short) 100);
Config config = new Config("config.props");
TSDB tsdb = new TSDB(client, config);
try {
tsdb.assignUid("metric", "ambud.metrics");
} catch (Exception e) {
}
Map<String, String> tags = new HashMap<>();
tags.put("host", "xxxxx");
tags.put("metric", "ambudmetrics");
Random rand = new Random();
long startTs = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
tsdb.addPoint("ambud.metrics", startTs + (i * 10), rand.nextInt(), tags).join();
}
tsdb.shutdown().join();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment