rpk topic produce

Produce records to a topic.

Producing records reads from STDIN, parses input according to --format, and produce 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. If using sizes to specify something, the size must come before what it is specifying. Delimiters match on an exact text basis. This command will quit with an error if any input fails to match your specified format.

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.

Schema registry

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

Additionally, utilizing topic in the mentioned 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

Examples

In the below examples, we can parse many records at once. The produce command reads input and tokenizes based on your specified format. Every time the format is completely matched, a record is produced and parsing begins anew.

  • A key and value, separated by a space and ending in newline: -f '%k %v\n'

  • A four byte topic, four byte key, and four byte value: -f '%T{4}%K{4}%V{4}%t%k%v'

  • A value to a specific partition, if using a non-negative --partition flag: -f '%p %v\n'

  • A big-endian uint16 key size, the text " foo ", and then that key: -f '%K{big16} foo %k'

  • A value that can be two or three characters followed by a newline: -f '%v{re#...?#}\n'

  • A key and a json value, separated by a space: -f '%k %v{json}'

Miscellaneous

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.

Usage

rpk topic produce [TOPIC] [flags]

Flags

Value Type Description

--acks

int

Number of acks required for producing (-1=all, 0=none, 1=leader) (default -1).

--allow-auto-topic-creation

-

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

-z, --compression

string

Compression to use for producing batches (none, gzip, snappy, lz4, zstd) (default "snappy").

--delivery-timeout

duration

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

-f, --format

string

Input record format (default "%v\n").

-H, --header

stringArray

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

-h, --help

-

Help for produce.

-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 (default -1).

-o, --output-format

string

what to write to stdout when a record is successfully produced (default "Produced to partition %p at offset %o with timestamp %d.\n").

-p, --partition

int32

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

--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

-

Produce empty values as tombstones.

--config

string

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

-X, --config-opt

stringArray

Override rpk configuration settings. See rpk -X or execute rpk -X help for inline detail or rpk -X list for terser detail.

--profile

string

Profile to use. See rpk profile for more details.

-v, --verbose

-

Enable verbose logging.