> For the complete documentation index, see [llms.txt](https://docs.lenses.io/latest/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.lenses.io/latest/user-guide/6.2-user-guide/using/using-sql-to-query-kafka/metadata-fields.md).

# Metadata fields

Understand using Kafka topic metadata fields in Lenses SQL Snapshot concepts engine for Apache Kafka

When running queries against Kafka, Snapshot Engine enables you to access the record metadata through the special `_meta` facet.

These are the available meta fields:

| Field               | Description                                           |
| ------------------- | ----------------------------------------------------- |
| `_meta.offset`      | The offset of the record in its Kafka topic partition |
| `_meta.partition`   | The Kafka topic partition of the record               |
| `_meta.timestamp`   | The Kafka record timestamp                            |
| `_meta.__keysize`   | The length in bytes of the raw key stored in Kafka    |
| `_meta.__valuesize` | The length in bytes of the raw value stored in Kafka  |

### Select all the meta fields

The following query will select all the meta fields listed above:

```sql
SELECT _meta.* FROM topic
```

### View headers

To view the value of a specific header you can run:

```sql
SELECT HEADERASSTRING("User") as user
FROM trips
LIMIT 100
```

### Filter on record timestamp <a href="#filter-on-record-timestamp-1" id="filter-on-record-timestamp-1"></a>

```sql
SELECT ...
    FROM topic
WHERE _meta.timestamp > YESTERDAY()
```

### Filter on table partition <a href="#filter-on-table-partition" id="filter-on-table-partition"></a>

To read records from a specific partition, the following query can be used:

```sql
SELECT ...
    FROM topic
WHERE _meta.partition = 1
OR _meta.partition = 8
```

### Search for a record on a specific offset <a href="#search-for-a-record-on-a-specific-offset" id="search-for-a-record-on-a-specific-offset"></a>

Here is the query to use when the record offset and partition are known:

```sql
SELECT ...
    FROM topic
WHERE _meta.partition = 2
AND _meta.offset = 8
LIMIT 1
```

### Get the latest N records per partition <a href="#get-the-latest-n-records-per-partition" id="get-the-latest-n-records-per-partition"></a>

This query will get the latest 100 records per partition (assuming the topic is not compacted):

```sql
SELECT ...
    FROM topic
WHERE _meta.offset >= LAST_OFFSET() - 100
```

This instead will get the latest 100 records for a given partition (again assuming the topic is not compacted):

```sql
SELECT ...
    FROM topic
WHERE _meta.offset >= LAST_OFFSET() - 100 
AND _meta.partition = 2
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.lenses.io/latest/user-guide/6.2-user-guide/using/using-sql-to-query-kafka/metadata-fields.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
