GithubHelp home page GithubHelp logo

isabella232 / stream_lines Goto Github PK

View Code? Open in Web Editor NEW

This project forked from algolia/stream_lines

0.0 0.0 0.0 2.51 MB

A Ruby gem for streaming remote files one line at a time

License: MIT License

Ruby 99.34% Shell 0.66%

stream_lines's Introduction

StreamLines

Gem Version Tests (Ruby)

An API for streaming files from remote locations one line at a time.

Background

Some applications run in production environments without a writable file system; usually this is a security measure. Futhermore, with the proliferation of container-based production environments, containers may not have access to tremendous amounts of memory. Thus, it can be impossible to read large files unless you read the file into memory in small doses. A common pattern is to use a line-delimited file like JSON Lines or a CSV and to read the file one line at a time in order to iterate over a dataset. This gem aims to provide an Enumerable interface for iterating over remote, line-delimited datasets.

Installation

Add this line to your application's Gemfile:

gem 'stream_lines'

And then execute:

$ bundle

Or install it yourself as:

$ gem install stream_lines

Usage

Reading

From a URL

url = 'https://my.remote.file/file.txt'
stream = StreamLines::Reading::Stream.new(url)

stream.each do |line|
  # Do something with the line of data (the line will be a String)
end

# A StreamLines::Reading::Stream object is Enumerable, so you can also use
# any Enumerable methods.

stream.each_slice(100) do |lines|
  # Do something with the 100 lines of data
end
CSVs

This gem provides first-class support for streaming CSVs from a remote URL.

url = 'https://my.remote.file/file.csv'
stream = StreamLines::Reading::CSV.new(url)

stream.each do |row|
  # each row will be an Array
end

# Supports most Ruby CSV options (see ignored options below)
stream = StreamLines::Reading::CSV.new(url, headers: true)

stream.each do |row|
  # each row will be a CSV::Row object that you can access like row['column_name']
end

Most options that you can pass to Ruby's CSV library are supported; however, the following options are explicitly ignored:

  • return_headers
  • header_converters
  • skip_lines

I suspect that these options are not used terribly frequently, and each would require additional logic in the StreamLines::Reading::CSV#each method. Rather than attempting to implement sensible solutions for these options, I am choosing to explicitly ignore them until there is enough outcry to support them.

JSON Lines/Streaming JSON

This gem provides first-class support for streaming JSON lines from a remote URL.

url = 'https://my.remote.file/file.jsonl'
stream = StreamLines::Reading::JSONLines.new(url)

stream.each do |row|
  # each row will be an Hash
end

# Supports all Ruby JSON::parse options
stream = StreamLines::Reading::JSONLines.new(url, symbolize_names: true)

stream.each do |row|
  # each row will be a Hash
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Releasing

After merging in the new functionality to the main branch:

git checkout main
git pull --prune
bundle exec rake version:bump:<major, minor, or patch>
bundle exec rubocop -a
git commit -a --amend
bundle exec rake release

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/jdlubrano/stream_lines. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

stream_lines's People

Contributors

jdlubrano avatar dependabot-preview[bot] avatar dependabot[bot] avatar robertmogos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.