rpk topic produce

Produce records to a topic. Producing records reads from STDIN, parses input according to --format, and produces records to Redpanda.

The input formatter understands a wide variety of formats. Parsing input operates on either sizes or on delimiters, both of which can be specified in the same formatting options.

Formatting

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

Escape Description

\t

Tabs

\n

Newlines

\r

Carriage returns

\\

Slashes

\xNN

Hex encoded characters

The percent encodings are represented like this:

Percent encoding Description

%t

Topic

%T

Topic length

%k

Key

%K

Key length

%v

Value

%V

Value length

%h

Begin the header specification

%H

Number of headers

%p

Partition

%o

Offset

%e

Leader epoch

%d

Timestamp (formatting described below)

%x

Producer ID

%y

Producer epoch

%[

Partition log start offset

%|

Partition last stable offset

%]

Partition high watermark

%%

Record attributes (formatting described below)

%a

Percent sign

%{

Left brace

%}

Right brace

%i

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:

Format Description

ascii

Print the number as ASCII (default)

hex64

Sixteen hex characters

hex32

Eight hex characters

hex16

Four hex characters

hex8

Two hex characters

hex4

One hex character

big64

Eight byte big endian number

big32

Four byte big endian number

big16

Two byte big endian number

big8

Alias for byte

little64

Eight byte little endian number

little32

Four byte little endian number

little16

Two byte little endian number

little8

Alias for byte

byte

One byte number

bool

true 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 prints a single null, whereas printing %V{big8} prints 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 prints 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] ===}

This prints [YYYY-MM-DD], while the surrounding three # on each side are used to wrap the formatting.

For more information on Go time formatting, see the Go documentation.

For more information on strftime formatting, run man strftime.

Usage

rpk topic produce <topic> [flags]

Schema registry

Records can be encoded using a specified schema from your schema registry. Use the --schema-id or --schema-key-id flags to define the schema ID, and rpk will retrieve the schemas and encode the record accordingly.

Additionally, using topic in these flags allows for the use of the Topic Name Strategy. This strategy identifies a schema subject name based on the topic itself. For example:

Produce to foo, encode using the latest schema in the subject foo-value:

rpk topic produce foo --schema-id=topic

For protobuf schemas, you can specify the fully qualified name of the message you want the record to be encoded with. Use the --schema-type flag or --schema-key-type. If the schema contains only one message, specifying the message name is unnecessary. For example:

Produce to foo, using schema ID 1, message FQN Person.Name:

rpk topic produce foo --schema-id 1 --schema-type Person.Name

Tombstones

By default, records produced without a value will have an empty-string value, "". The below example produces a record with the key not_a_tombstone_record and the value "":

rpk topic produce foo -k not_a_tombstone_record
[Enter]

Tombstone records (records with a null value) can be produced by using the -Z flag and creating empty-string value records. Using the same example from above, but adding the -Z flag will produce a record with the key tombstone_record and the value null:

rpk topic produce foo -k tombstone_record -Z
[Enter]

Records produced with values of string "null" are not considered tombstones by Redpanda.

Miscellaneous

Producing requires a topic to produce to. The topic can be specified either directly 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.

Examples

This section provides examples of how to use rpk topic produce.

Format examples

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

rpk topic produce my-topic -f '%k %v\n'

A four byte topic, four byte key, and four byte value

rpk topic produce -f '%T{4}%K{4}%V{4}%t%k%v'

A value to a specific partition (requires non-negative --partition flag)

rpk topic produce my-topic -p 0 -f '%p %v\n'

A big-endian uint16 key size, the text " foo ", and then that key

rpk topic produce my-topic -f '%K{big16} foo %k'

A value that can be two or three characters followed by a newline

rpk topic produce my-topic -f '%v{re#...?#}\n'

A key and a JSON value, separated by a space

rpk topic produce my-topic -f '%k %v{json}'

Flags

Value Type Description

--acks

int

Number of acknowledgments required before a produce request is considered successful (-1 = all in-sync replicas, 0 = no acknowledgment, 1 = leader only).

--allow-auto-topic-creation

bool

Auto-create non-existent topics; requires auto_create_topics_enabled on the broker.

-z, --compression

string

Compression algorithm to use when producing batches (none, gzip, snappy, lz4, zstd).

--delivery-timeout

duration

Per-record delivery timeout, if non-zero, min 1s.

-f, --format

string

Input record format.

-H, --header

stringArray

Headers in format key:value to add to each record (repeatable).

-k, --key

string

A fixed key to use for each record (parsed input keys take precedence).

--max-message-bytes

int32

If non-negative, maximum size of a record batch before compression.

-o, --output-format

string

Output to write to stdout when a record is successfully produced.

-p, --partition

int32

Partition to directly produce to, if non-negative (also allows %p parsing to set partitions).

--schema-id

string

Schema ID to encode the record value with, use topic for TopicName strategy.

--schema-key-id

string

Schema ID to encode the record key with, use topic for TopicName strategy.

--schema-key-type

string

Name of the protobuf message type to be used to encode the record key using schema registry.

--schema-type

string

Name of the protobuf message type to be used to encode the record value using schema registry.

-Z, --tombstone

bool

Produce empty values as tombstones.

Global flags

Value Type Description

--config

string

Redpanda or rpk config file; default search paths are ~/.config/rpk/rpk.yaml, $PWD/redpanda.yaml, and /etc/redpanda/redpanda.yaml.

-X, --config-opt

stringArray

Override rpk configuration settings; -X help for detail or -X list for terser detail.

--ignore-profile

bool

Ignore rpk.yaml and redpanda.yaml; use default settings.

--profile

string

rpk profile to use.

-v, --verbose

bool

Enable verbose logging.