> For the complete documentation index, see [llms.txt](https://docs.redivis.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.redivis.com/api/client-libraries/redivis-r/reference/readstream.md).

# ReadStream

## *class* <mark style="color:purple;">ReadStream</mark>

ReadStreams are an advanced concept that can be used for processing large tabular data in parallel. For example, a table can be broken into multiple ReadStreams for processing, where each stream can be read simultaneous in a separate thread / process.

## Constructors

<table data-header-hidden><thead><tr><th width="334.4296875">Method</th><th>Description</th></tr></thead><tbody><tr><td><a href="/api/client-libraries/redivis-r/reference/upload/uploadusdto_.md"><strong><code>Upload$to_read_streams</code></strong></a>(target_count)</td><td>Create parallel read streams to read an upload.</td></tr><tr><td><a href="/api/client-libraries/redivis-r/reference/query/queryusdto_.md"><strong><code>Query$to_read_streams</code></strong></a>(target_count)</td><td>Create parallel read streams to read query results.</td></tr><tr><td><a href="/api/client-libraries/redivis-r/reference/table/tableusdto_read_streams.md"><strong><code>Table$to_read_streams</code></strong></a>(target_count)</td><td>Create parallel read streams to read a table.</td></tr></tbody></table>

## Examples

```r
library(redivis)
library(arrow)
library(parallel)

table <- redivis$table(
  "demo.cms_2014_medicare_data:349j.physicians_and_other_supplier:kn00"
)

streams <- table$to_read_streams(
  variables = list("average_submitted_chrg_amt"),
  target_count = 4L
)

process_stream <- function(stream) {
  reader <- stream$to_arrow_batch_reader()
  on.exit(reader$close())
  total <- 0
  count <- 0L

  while (TRUE) {
    batch <- reader$read_next_batch()
    if (is.null(batch)) break

    column <- as.vector(batch[["average_submitted_chrg_amt"]])
    total <- total + as.numeric(sum(column, na.rm = TRUE))
    count <- count + length(column)
  }

  list(total = total, count = count)
}

# Process streams in parallel
future::plan(future::multisession, workers = length(streams))
results <- furrr::future_map(streams, process_stream)

grand_total <- sum(vapply(results, `[[`, numeric(1), "total"))
grand_count <- sum(vapply(results, `[[`, integer(1), "count"))

average <- if (grand_count > 0) grand_total / grand_count else 0

cat(sprintf("Average submitted charge amount: $%.2f\n", average))
cat(sprintf(
  "Computed across %d rows using %d parallel streams\n",
  grand_count,
  length(streams)
))
```

## Attributes

| **`properties`** | A dict containing the attributes `id` and `estimatedRows`. |
| ---------------- | ---------------------------------------------------------- |

## Methods

<table data-header-hidden><thead><tr><th width="441"></th><th></th></tr></thead><tbody><tr><td><a href="/api/client-libraries/redivis-r/reference/readstream/readstreamusdto_.md"><strong><code>ReadStream$to_*</code></strong></a>([max_results, *, ...])</td><td>Various methods to read query results. Mirrors the various <a href="/api/client-libraries/redivis-r/reference/table.md">Table$to_*</a> methods (e.g., <code>ReadStream$to_tibble()</code></td></tr></tbody></table>


---

# 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.redivis.com/api/client-libraries/redivis-r/reference/readstream.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.
