-
Notifications
You must be signed in to change notification settings - Fork 70
/
influxdb-1.8.ts
executable file
·50 lines (41 loc) · 1.79 KB
/
influxdb-1.8.ts
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
46
47
48
49
50
#!./node_modules/.bin/esr
////////////////////////////////////////////////////////////////////
// Shows how to use forward compatibility APIs from InfluxDB 1.8. //
////////////////////////////////////////////////////////////////////
// [InfluxDB 2.0 API compatibility endpoints](https://docs.influxdata.com/influxdb/v1.8/tools/api/#influxdb-2-0-api-compatibility-endpoints)
// are part of the InfluxDB 1.x line since InfluxDB 1.8.0. This allows you to leverage InfluxDB 2.x client libraries for both writing and
// querying data with Flux.
// https://docs.influxdata.com/influxdb/v1.8/about_the_project/releasenotes-changelog/#forward-compatibility
import {ClientOptions, InfluxDB, Point} from '@influxdata/influxdb-client'
const username = 'username'
const password = 'password'
const database = 'telegraf'
const retentionPolicy = 'autogen'
const bucket = `${database}/${retentionPolicy}`
const clientOptions: ClientOptions = {
url: 'http://localhost:8086',
token: `${username}:${password}`,
}
const influxDB = new InfluxDB(clientOptions)
async function writePoints(): Promise<void> {
console.log('*** WRITE POINTS ***')
const writeAPI = influxDB.getWriteApi('', bucket)
const point = new Point('mem')
.tag('host', 'host1')
.floatField('used_percent', 23.43234543)
writeAPI.writePoint(point)
await writeAPI.close()
}
async function queryRows(): Promise<void> {
console.log('*** QUERY ROWS ***')
const queryAPI = influxDB.getQueryApi('')
const query = `from(bucket: "${bucket}") |> range(start: -1h)`
for await (const {values, tableMeta} of queryAPI.iterateRows(query)) {
const o = tableMeta.toObject(values)
console.log(`${o._time} ${o._measurement} : ${o._field}=${o._value}`)
}
console.log('\nQuery FINISHED')
}
writePoints()
.then(queryRows)
.catch((e) => console.error(e))