Writing Data — Line Protocol
Outcome: points in the bucket, written
by the CLI from a file you can read, and
a count that proves exactly how many
landed. Transcript:
influxdb-lab/transcripts/t02-write-and-first-query.txt.
One line per point
InfluxDB's write format is line protocol: plain text, one point per line, in four parts — measurement, tags, fields, timestamp — separated by single spaces and commas:
weather,station=rooftop-1 temperature=18.1,humidity=57i 1793499960
weather,station=garden-1 temperature=17.2,humidity=63i 1793499960
Read the first line slowly. weather
is the measurement. station=rooftop-1
is a tag: a label, always a word.
temperature=18.1 and humidity=57i
are fields: the values. Note the i —
it makes the humidity an integer.
A bare number is a decimal; a quoted
value (name="north") is a string;
true and false are booleans. The
type is fixed by the first point that
sets a field, and the server will refuse
a later point that disagrees — chapter
8's transcript shows it rejecting the
unquoted word hot as a temperature,
with an error that quotes the offending
line. The last number is the timestamp:
when the reading was true, counted in
the precision you declare.
Precision is part of the write, not
the line: nanoseconds by default, or
--precision s for seconds, as here.
Write second-sized stamps without
declaring seconds and your weather
station reports from January 1970 —
the commonest first-day bug in the
building, and an entirely silent one.
The fixture
The guide's data is invented: a weather
station that does not exist, reporting
every ten minutes for an hour, from two
stations. The generator is in the lab
(influxdb-lab/gen-weather.py) so the
timestamps are always "the hour ending
now" — six readings per station, the
temperature climbing gently, humidity
falling, values chosen so the averages
in chapter 5 can be checked by eye.
The write — and its one trap
influx write --bucket weather \
--file weather.lp --precision s < /dev/null
Success is silent; the exit code is 0.
The < /dev/null is not decoration, and
it is this guide's favourite verified
trap (the lab fell into it for three
minutes, with two different client
builds): influx write also listens
to standard input. Run it in a script
or a terminal harness where stdin is
held open, and the command takes the
data — the points do land — and then
waits, apparently hung, for input that
is never coming. The lab proved the
points had landed by querying from
another shell while the writer still
"hung". Close stdin, or pipe the data
through stdin deliberately, and the
command returns instantly. Chapter 14
keeps the full story.
Writing the same point twice is safe in a specific way: a point's identity is its series plus its timestamp, so re-writing is an overwrite, not a duplicate. The lab's count stayed at one for a point it accidentally wrote three times during the stdin investigation.
The proof
Never trust a silent success — count:
from(bucket: "weather")
|> range(start: -1h)
|> group(columns: ["station", "_field"])
|> count()
garden-1 humidity 6
garden-1 temperature 6
rooftop-1 humidity 6
rooftop-1 temperature 6
(The real output is a table per group;
transcript t02 keeps it whole.)
Six readings × two stations × two
fields, exactly what the fixture
promised. This count-after-write habit
is the pattern for the whole guide:
every chapter's claim is a query away,
and chapters 5 and 6 teach the queries
themselves.