influxdata / influxdb-java

Java client for InfluxDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why that code doesn't work?

jayjupdhig opened this issue · comments

public void connect(final boolean caUseHttps) {
		if (caUseHttps) {
			InfluxDbConnector.mProtocol = "https";
		}

		InfluxDbConnector.mConnectionUrl = mProtocol + "://" + cmServerHostNameOrIp + ":" + InfluxDbConnector.cmPort;
		System.out.println("mConnectionUrl: " + InfluxDbConnector.mConnectionUrl);

		////////////////////////////////////////////////
		Exception lException = null;
		InfluxDB lInfluxDB = null;

		try {
			lInfluxDB = InfluxDBFactory.connect(mConnectionUrl, cmUsername, cmPassword);
		} catch (final Exception clException) {
			lException = clException;
		} finally {
			if (lException != null) {
				System.out.println(lException.getMessage());
				throw new RuntimeException(lException);
			}
		}
		////////////////////////////////////////////////

		System.out.println("Verbindung zu InfluxDB wurde erfolgreich hergestellt");

		lInfluxDB.query(new Query("CREATE DATABASE " + cmDatabaseName));
		lInfluxDB.setDatabase(cmDatabaseName);

		// Enable batch writes to get better performance.
		lInfluxDB.enableBatch(BatchOptions.DEFAULTS.threadFactory(runnable -> {
			Thread thread = new Thread(runnable);
			thread.setDaemon(true);
			return thread;
		}));

		// Close it if your application is terminating or you are not using it anymore.
		Runtime.getRuntime().addShutdownHook(new Thread(lInfluxDB::close));

		// Write points to InfluxDB.
		lInfluxDB.write(Point.measurement("h2o_feet").time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
				.tag("location", "santa_monica").addField("level description", "below 3 feet")
				.addField("water_level", 2.064d).build());

		lInfluxDB.write(Point.measurement("h2o_feet").time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
				.tag("location", "coyote_creek").addField("level description", "between 6 and 9 feet")
				.addField("water_level", 8.12d).build());

		////////////////////////////////////////////////		
		/*
		lException = null;
		
		try {
			if (lInfluxDB != null) {
				lInfluxDB.close();
			}
		} catch (final Exception caException) {
			lException = caException;
		} finally {
			if (lException != null) {
				System.out.println(lException.getMessage());
				throw new RuntimeException(lException);
			}
		}
		
		System.out.println("Verbindung zu InfluxDB wurde erfolgreich geschlossen - Programm wird beendet");
		*/
		////////////////////////////////////////////////		
	}

...and what's the difference between https://github.com/influxdata/influxdb-client-java AND https://github.com/influxdata/influxdb-java...?

Thank you for your feedback(s)...

Best regards from Switzerland

"Why that code doesn't work?"
-> Will write NOTHING to the database, that was the question...

...and what's the difference between https://github.com/influxdata/influxdb-client-java AND https://github.com/influxdata/influxdb-java...?

This library is for use with InfluxDB 1.x and 2.x compatibility API. For full supports of InfluxDB 2.x features, please use the influxdb-client-java client.

// Enable batch writes to get better performance.
lInfluxDB.enableBatch(BatchOptions.DEFAULTS.threadFactory(runnable -> {
Thread thread = new Thread(runnable);
thread.setDaemon(true);
return thread;
}));

This code enable writes at background thread in specified intervals. You have to use something like Thread.sleep(5_000L); before do your query.

For more info see: https://github.com/influxdata/influxdb-java/blob/master/MANUAL.md#enabling-batch-writes

Thank you very much - solved! :-)