Skip to main content
Version: 23.1

rpk topic

You can use rpk topic to manage your topics, create new messages and consume them.

Formatting

Formatting is based on percent escapes and modifiers. Slashes can be used for common escapes:

EscapeDescription
\t Tabs.
\nNewlines
\rCarriage returns.
\\Slashes.
\xNNHex encoded characters.

The percent encodings are represented like this:

Percent encodingDescription
%tTopic.
%TTopic length.
%kKey.
%KKey length.
%vValue.
%VValue length.
%hBegin the header specification.
%HNumber of headers.
%pPartition.
%oOffset.
%eLeader epoch.
%dTimestamp (formatting described below).
%xProducer id.
%y

Producer epoch.

%[Partition log start offset.
%|Partition last stable offset.
%]Partition high watermark.
%%Record attributes (formatting described below).
%aPercent sign
%{Left brace.
%}Right brace.
%iThe number of records formatted.

Modifiers

Text and numbers can be formatted in many different ways, and the default format can be changed within brace modifiers. %v prints a value, while %v{hex} prints the value hex encoded. %T prints the length of a topic in ascii, while %T{big8} prints the length of the topic as an eight byte big endian.

All modifiers go within braces following a percent-escape.

Numbers

Formatting number values can have the following modifiers:

FormatDescription
asciiPrint the number as ascii (default).
hex64Sixteen hex characters.
hex32Eight hex characters.
hex16Four hex characters.
hex8Two hex characters.
hex4One hex character.
big64Eight byte big endian number.
big32Four byte big endian number.
big16Two byte big endian number.
big8Alias for byte.
little64Eight byte little endian number
little32Four byte little endian number.
little16Two byte little endian number.
little8Alias for byte.
byteOne byte number.
booltrue if the number is non-zero, false if the number is zero

All numbers are truncated as necessary per the modifier. Printing %V{byte} for a length 256 value will print a single null, whereas printing %V{big8} would print the bytes 1 and 0.

When writing number sizes, the size corresponds to the size of the raw values, not the size of encoded values. %T% t{hex} for the topic foo will print 3 666f6f, not 6 666f6f.

Timestamps

By default, the timestamp field is printed as a millisecond number value. In addition to the number modifiers above, timestamps can be printed with either Go formatting:

%d{go[2006-01-02T15:04:05Z07:00]}

Or strftime formatting:

%d{strftime[%F]}

An arbitrary amount of brackets (or braces, or # symbols) can wrap your date formatting:

%d{strftime### [%F] ###}

The above will print [YYYY-MM-DD], while the surrounding three # on each side are used to wrap the formatting.

Further details on Go time formatting can be found at Go's time documentation website.

Further details on strftime formatting can be read by running man strftime.

Attributes

Each record (or batch of records) has a set of possible attributes. Internally, these are packed into bit flags. Printing an attribute requires first selecting which attribute you want to print, and then optionally specifying how you want it to be printed:

  %a{compression}
%a{compression;number}
%a{compression;big64}
%a{compression;hex8}

Compression is by default printed as text (none, gzip, ...). Compression can be printed as a number with ;number, where number is any number formatting option described above. No compression is 0, gzip is 1, etc.

  %a{timestamp-type}
%a{timestamp-type;big64}

The record's timestamp type is printed as:

  • -1 for very old records (before timestamps existed)
  • 0 for client generated timestamps
  • 1 for broker generated timestamps.
note

Number formatting can be controlled with ;number.

%a{transactional-bit}
%a{transactional-bit;bool}

Prints 1 if the record a part of a transaction or 0 if it is not.

  %a{control-bit}
%a{control-bit;bool}

Prints 1 if the record is a commit marker or 0 if it is not.

Text

Text fields without modifiers default to writing the raw bytes. Alternatively, there are the following modifiers:

ModifierDescription
%t{hex}Hex encoding
%k{base64}Base64 encoding
%k{base64raw}Base64 encoding raw
%v{unpack[<bBhH>iIqQc.$]}The unpack modifier has a further internal specification, similar to timestamps above.

The hex modifier hex encodes the text, the base64 modifier base64 encodes the text with standard encoding, and the base64raw modifier encodes the text with raw standard encoding. The unpack modifier has a further internal specification, similar to timestamps above:

CharacterDescription
xPad character (does not parse input).
<Switch what follows to little endian.
>Switch what follows to big endian.
bSigned byte.
BUnsigned byte.
hint16 ("half word").
Huint16 ("half word").
iint32.
Iuint32.
qint64 ("quad word").
Quint64 ("quad word").
cAny character.
.Alias for c.
sConsume the rest of the input as a string
$Match the end of the line (append error string if anything remains).

Unpacking text can allow translating binary input into readable output. If a value is a big-endian uint32, %v will print the raw four bytes, while %v{unpack[>I]} will print the number in as ascii. If unpacking exhausts the input before something is unpacked fully, an error message is appended to the output.

Headers

Headers are formatted with percent encoding inside of the modifier:

%h{%k=%v{hex}}

will print all headers with a space before the key and after the value, an equals sign between the key and value, and with the value hex encoded. Header formatting actually just parses the internal format as a record format, so all of the above rules about %K, %V, text, and numbers apply.

Examples

A key and value, separated by a space and ending in newline:

-f '%k %v\n'

A key length as four big endian bytes, and the key as hex:

-f '%K{big32}%k{hex}'

A little endian uint32 and a string unpacked from a value:

-f '%v{unpack[is$]}'

Offsets

The --offset flag allows for specifying where to begin consuming, and optionally, where to stop consuming. The literal words start and end specify consuming from the start and the end.

OffsetDescription
startConsume from the beginning.
endConsume from the end.
:endConsume until the current end.
+ooConsume oo after the current start offset.
-ooConsume oo before the current end offset.
ooConsume after an exact offset.
oo:Alias for oo.
:ooConsume until an exact offset.
o1:o2Consume from exact offset o1 until exact offset o2.
@tConsume starting from a given timestamp.
@t:alias for @t.
@:tConsume until a given timestamp.
@t1:t2Consume from timestamp t1 until timestamp t2.

There are a few options for timestamps, with each option being evaluated until one succeeds:

TimestampDescription
13 digitsParsed as a unix millisecond.
9 digitsParsed as a unix second.
YYYY-MM-DDParsed as a day, UTC.
YYYY-MM-DDTHH:MM:SSZParsed as RFC3339, UTC; fractional seconds optional (.MMM).
-durDuration ago; from now (as t1) or from t1 (as t2).
durFor t2 in @t1:t2, relative duration from t1.
endFor t2 in @t1:t2, the current end of the partition.

Durations are parsed simply:

3ms    three milliseconds
10s ten seconds
9m nine minutes
1h one hour
1m3ms one minute and three milliseconds

For example:

-o @2022-02-14:1h   consume 1h of time on Valentine's Day 2022
-o @-48h:-24h consume from 2 days ago to 1 day ago
-o @-1m:end consume from 1m ago until now
-o @:-1hr consume from the start until an hour ago

Misc

Producing requires a topic to produce to. The topic can be specified either directly on as an argument, or in the input text through %t. A parsed topic takes precedence over the default passed in topic. If no topic is specified directly and no topic is parsed, this command will quit with an error.

The input format can parse partitions to produce directly to with %p. Doing so requires specifying a non-negative --partition flag. Any parsed partition takes precedence over the --partition flag; specifying the flag is the main requirement for being able to directly control which partition to produce to.

You can also specify an output format to write when a record is produced successfully. The output format follows the same formatting rules as the topic consume command. See that command's help text for a detailed description.

rpk topic

Create, delete, produce to and consume from Redpanda topics.

Usage

rpk topic [command]

Flags

ValueTypeDescription
--brokersstringsComma-separated list of broker <ip>:<port> pairs (for example,--brokers '192.168.78.34:9092,192.168.78.35:9092,192.179.23.54:9092'). Alternatively, you may set the REDPANDA_BROKERSenvironment variable with the comma-separated list of broker addresses.
--configstringRedpanda config file, if not set the file will be searched for in the default locations.
-h, --help-Help for topic.
--passwordstringSASL password to be used for authentication.
--sasl-mechanismstringThe authentication mechanism to use. Supported values:SCRAM-SHA-256, SCRAM-SHA-512.
--tls-certstringThe certificate to be used for TLS authentication with the broker.
--tls-enabled-Enable TLS for the Kafka API (not necessary if specifying custom certs).
--tls-keystringThe certificate key to be used for TLS authentication with the broker.
--tls-truststorestringThe truststore to be used for TLS communication with the broker.
--userstringSASL user to be used for authentication.
-v, --verbose-Enable verbose logging (default false).

What do you like about this doc?




Optional: Share your email address if we can contact you about your feedback.

Let us know what we do well: