-
Notifications
You must be signed in to change notification settings - Fork 70
/
write.mjs
executable file
·45 lines (41 loc) · 1.9 KB
/
write.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env node
//////////////////////////////////////////
// Shows how to use InfluxDB write API. //
//////////////////////////////////////////
import {InfluxDB, Point, HttpError} from '@influxdata/influxdb-client'
import {url, token, org, bucket} from './env.mjs'
import {hostname} from 'node:os'
console.log('*** WRITE POINTS ***')
// create a write API, expecting point timestamps in nanoseconds (can be also 's', 'ms', 'us')
const writeApi = new InfluxDB({url, token}).getWriteApi(org, bucket, 'ns')
// setup default tags for all writes through this API
writeApi.useDefaultTags({location: hostname()})
// write point with the current (client-side) timestamp
const point1 = new Point('temperature')
.tag('example', 'write.ts')
.floatField('value', 20 + Math.round(100 * Math.random()) / 10)
writeApi.writePoint(point1)
console.log(` ${point1}`)
// write point with a custom timestamp
const point2 = new Point('temperature')
.tag('example', 'write.ts')
.floatField('value', 10 + Math.round(100 * Math.random()) / 10)
.timestamp(new Date()) // can be also a number, but in writeApi's precision units (s, ms, us, ns)!
writeApi.writePoint(point2)
console.log(` ${point2.toLineProtocol(writeApi)}`)
// WriteApi always buffer data into batches to optimize data transfer to InfluxDB server.
// writeApi.flush() can be called to flush the buffered data. The data is always written
// asynchronously, Moreover, a failed write (caused by a temporary networking or server failure)
// is retried automatically. Read `writeAdvanced.js` for better explanation and details.
//
// close() flushes the remaining buffered data and then cancels pending retries.
try {
await writeApi.close()
console.log('FINISHED ... now try ./query.ts')
} catch (e) {
console.error(e)
if (e instanceof HttpError && e.statusCode === 401) {
console.log('Run ./onboarding.js to setup a new InfluxDB database.')
}
console.log('\nFinished ERROR')
}