use of net.opentsdb.core.TSDB in project opentsdb by OpenTSDB.
the class AddDataExample method main.
public static void main(final String[] args) throws Exception {
processArgs(args);
// Create a config object with a path to the file for parsing. Or manually
// override settings.
// e.g. config.overrideConfig("tsd.storage.hbase.zk_quorum", "localhost");
final Config config;
if (pathToConfigFile != null && !pathToConfigFile.isEmpty()) {
config = new Config(pathToConfigFile);
} else {
// Search for a default config from /etc/opentsdb/opentsdb.conf, etc.
config = new Config(true);
}
final TSDB tsdb = new TSDB(config);
// Declare new metric
String metricName = "my.tsdb.test.metric";
// First check to see it doesn't already exist
// we don't actually need this for the first
byte[] byteMetricUID;
// addPoint().
try {
byteMetricUID = tsdb.getUID(UniqueIdType.METRIC, metricName);
} catch (IllegalArgumentException iae) {
System.out.println("Metric name not valid.");
iae.printStackTrace();
System.exit(1);
} catch (NoSuchUniqueName nsune) {
// If not, great. Create it.
byteMetricUID = tsdb.assignUid("metric", metricName);
}
// Make a single datum
long timestamp = System.currentTimeMillis() / 1000;
long value = 314159;
// Make key-val
Map<String, String> tags = new HashMap<String, String>(1);
tags.put("script", "example1");
// Start timer
long startTime1 = System.currentTimeMillis();
// Write a number of data points at 30 second intervals. Each write will
// return a deferred (similar to a Java Future or JS Promise) that will
// be called on completion with either a "null" value on success or an
// exception.
int n = 100;
ArrayList<Deferred<Object>> deferreds = new ArrayList<Deferred<Object>>(n);
for (int i = 0; i < n; i++) {
Deferred<Object> deferred = tsdb.addPoint(metricName, timestamp, value + i, tags);
deferreds.add(deferred);
timestamp += 30;
}
// Add the callbacks to the deferred object. (They might have already
// returned, btw)
// This will cause the calling thread to wait until the add has completed.
System.out.println("Waiting for deferred result to return...");
Deferred.groupInOrder(deferreds).addErrback(new AddDataExample().new errBack()).addCallback(new AddDataExample().new succBack()).join();
// Alternatively you can add another callback here or use a join with a
// timeout argument.
// End timer.
long elapsedTime1 = System.currentTimeMillis() - startTime1;
System.out.println("\nAdding " + n + " points took: " + elapsedTime1 + " milliseconds.\n");
// Gracefully shutdown connection to TSDB. This is CRITICAL as it will
// flush any pending operations to HBase.
tsdb.shutdown().join();
}
use of net.opentsdb.core.TSDB in project opentsdb by OpenTSDB.
the class UidManager method main.
public static void main(String[] args) throws Exception {
ArgP argp = new ArgP();
CliOptions.addCommon(argp);
CliOptions.addVerbose(argp);
argp.addOption("--idwidth", "N", "Number of bytes on which the UniqueId is encoded.");
argp.addOption("--ignore-case", "Ignore case distinctions when matching a regexp.");
argp.addOption("-i", "Short for --ignore-case.");
args = CliOptions.parse(argp, args);
if (args == null) {
usage(argp, "Invalid usage");
System.exit(2);
} else if (args.length < 1) {
usage(argp, "Not enough arguments");
System.exit(2);
}
final short idwidth = (argp.has("--idwidth") ? Short.parseShort(argp.get("--idwidth")) : 3);
if (idwidth <= 0) {
usage(argp, "Negative or 0 --idwidth");
System.exit(3);
}
final boolean ignorecase = argp.has("--ignore-case") || argp.has("-i");
// get a config object
Config config = CliOptions.getConfig(argp);
final byte[] table = config.getString("tsd.storage.hbase.uid_table").getBytes();
final TSDB tsdb = new TSDB(config);
tsdb.getClient().ensureTableExists(config.getString("tsd.storage.hbase.uid_table")).joinUninterruptibly();
argp = null;
int rc;
try {
rc = runCommand(tsdb, table, idwidth, ignorecase, args);
} finally {
try {
LOG.info("Shutting down TSD....");
tsdb.getClient().shutdown().joinUninterruptibly();
LOG.info("Gracefully shutdown the TSD");
} catch (Exception e) {
LOG.error("Unexpected exception while shutting down", e);
rc = 42;
}
}
System.exit(rc);
}
use of net.opentsdb.core.TSDB in project opentsdb by OpenTSDB.
the class TextImporter method main.
public static void main(String[] args) throws Exception {
ArgP argp = new ArgP();
CliOptions.addCommon(argp);
CliOptions.addAutoMetricFlag(argp);
argp.addOption("--skip-errors", "Whether or not to skip exceptions " + "during processing");
args = CliOptions.parse(argp, args);
if (args == null) {
usage(argp, 1);
} else if (args.length < 1) {
usage(argp, 2);
}
// get a config object
Config config = CliOptions.getConfig(argp);
final TSDB tsdb = new TSDB(config);
final boolean skip_errors = argp.has("--skip-errors");
tsdb.checkNecessaryTablesExist().joinUninterruptibly();
argp = null;
try {
int points = 0;
final long start_time = System.nanoTime();
for (final String path : args) {
points += importFile(tsdb.getClient(), tsdb, path, skip_errors);
}
final double time_delta = (System.nanoTime() - start_time) / 1000000000.0;
LOG.info(String.format("Total: imported %d data points in %.3fs" + " (%.1f points/s)", points, time_delta, (points / time_delta)));
// TODO(tsuna): Figure out something better than just writing to stderr.
tsdb.collectStats(new StatsCollector("tsd") {
@Override
public final void emit(final String line) {
System.err.print(line);
}
});
} finally {
try {
tsdb.shutdown().joinUninterruptibly();
} catch (Exception e) {
LOG.error("Unexpected exception", e);
System.exit(1);
}
}
}
use of net.opentsdb.core.TSDB in project opentsdb by OpenTSDB.
the class CliQuery method main.
public static void main(String[] args) throws Exception {
ArgP argp = new ArgP();
CliOptions.addCommon(argp);
CliOptions.addVerbose(argp);
argp.addOption("--graph", "BASEPATH", "Output data points to a set of files for gnuplot." + " The path of the output files will start with" + " BASEPATH.");
args = CliOptions.parse(argp, args);
if (args == null) {
usage(argp, "Invalid usage.", 1);
} else if (args.length < 3) {
usage(argp, "Not enough arguments.", 2);
}
// get a config object
Config config = CliOptions.getConfig(argp);
final TSDB tsdb = new TSDB(config);
tsdb.checkNecessaryTablesExist().joinUninterruptibly();
final String basepath = argp.get("--graph");
argp = null;
Plot plot = null;
try {
plot = doQuery(tsdb, args, basepath != null);
} finally {
try {
tsdb.shutdown().joinUninterruptibly();
} catch (Exception e) {
LOG.error("Unexpected exception", e);
System.exit(1);
}
}
if (plot != null) {
try {
final int npoints = plot.dumpToFiles(basepath);
LOG.info("Wrote " + npoints + " for Gnuplot");
} catch (IOException e) {
LOG.error("Failed to write the Gnuplot file under " + basepath, e);
System.exit(1);
}
}
}
use of net.opentsdb.core.TSDB in project opentsdb by OpenTSDB.
the class TestTSMeta method before.
@Before
public void before() throws Exception {
config = mock(Config.class);
when(config.getString("tsd.storage.hbase.data_table")).thenReturn("tsdb");
when(config.getString("tsd.storage.hbase.uid_table")).thenReturn("tsdb-uid");
when(config.getString("tsd.storage.hbase.meta_table")).thenReturn("tsdb-meta");
when(config.getString("tsd.storage.hbase.tree_table")).thenReturn("tsdb-tree");
when(config.enable_tsuid_incrementing()).thenReturn(true);
when(config.enable_realtime_ts()).thenReturn(true);
tsdb = new TSDB(client, config);
storage = new MockBase(tsdb, client, true, true, true, true);
final List<byte[]> families = new ArrayList<byte[]>();
families.add(TSMeta.FAMILY);
storage.addTable(META_TABLE, families);
storage.addColumn(UID_TABLE, new byte[] { 0, 0, 1 }, NAME_FAMILY, "metrics".getBytes(MockBase.ASCII()), "sys.cpu.0".getBytes(MockBase.ASCII()));
storage.addColumn(UID_TABLE, new byte[] { 0, 0, 1 }, NAME_FAMILY, "metric_meta".getBytes(MockBase.ASCII()), ("{\"uid\":\"000001\",\"type\":\"METRIC\",\"name\":\"sys.cpu.0\"," + "\"description\":\"Description\",\"notes\":\"MyNotes\",\"created\":" + "1328140801,\"displayName\":\"System CPU\"}").getBytes(MockBase.ASCII()));
storage.addColumn(UID_TABLE, new byte[] { 0, 0, 1 }, NAME_FAMILY, "tagk".getBytes(MockBase.ASCII()), "host".getBytes(MockBase.ASCII()));
storage.addColumn(UID_TABLE, new byte[] { 0, 0, 1 }, NAME_FAMILY, "tagk_meta".getBytes(MockBase.ASCII()), ("{\"uid\":\"000001\",\"type\":\"TAGK\",\"name\":\"host\"," + "\"description\":\"Description\",\"notes\":\"MyNotes\",\"created\":" + "1328140801,\"displayName\":\"Host server name\"}").getBytes(MockBase.ASCII()));
storage.addColumn(UID_TABLE, new byte[] { 0, 0, 1 }, NAME_FAMILY, "tagv".getBytes(MockBase.ASCII()), "web01".getBytes(MockBase.ASCII()));
storage.addColumn(UID_TABLE, new byte[] { 0, 0, 1 }, NAME_FAMILY, "tagv_meta".getBytes(MockBase.ASCII()), ("{\"uid\":\"000001\",\"type\":\"TAGV\",\"name\":\"web01\"," + "\"description\":\"Description\",\"notes\":\"MyNotes\",\"created\":" + "1328140801,\"displayName\":\"Web server 1\"}").getBytes(MockBase.ASCII()));
storage.addColumn(META_TABLE, TSUID, NAME_FAMILY, "ts_meta".getBytes(MockBase.ASCII()), ("{\"tsuid\":\"000001000001000001\",\"" + "description\":\"Description\",\"notes\":\"Notes\",\"created\":1328140800," + "\"custom\":null,\"units\":\"\",\"retention\":42,\"max\":1.0,\"min\":" + "\"NaN\",\"displayName\":\"Display\",\"dataType\":\"Data\"}").getBytes(MockBase.ASCII()));
storage.addColumn(META_TABLE, TSUID, TSMeta.FAMILY, "ts_ctr".getBytes(MockBase.ASCII()), Bytes.fromLong(1L));
storage.addColumn(META_TABLE, new byte[] { 0, 0, 1, 0, 0, 1, 0, 0, 2 }, TSMeta.FAMILY, "ts_meta".getBytes(MockBase.ASCII()), ("{\"tsuid\":\"000001000001000002\",\"" + "description\":\"Description\",\"notes\":\"Notes\",\"created\":1328140800," + "\"custom\":null,\"units\":\"\",\"retention\":42,\"max\":1.0,\"min\":" + "\"NaN\",\"displayName\":\"Display\",\"dataType\":\"Data\"}").getBytes(MockBase.ASCII()));
}
Aggregations