tsvreader

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2018 License: MIT Imports: 7 Imported by: 6

README

tsvreader - fast reader for tab-separated data

Features

  • Optimized for speed. May read more than 20M rows per second on a single CPU core.
  • Compatible with TSV (aka TabSeparated) format used in ClickHouse responses. See chclient - clickhouse client built on top of tsvreader.
  • May read rows with variable number of columns using Reader.HasCols. This functionality allows reading WITH TOTALS row from ClickHouse responses and BlockTabSeparated responses.

Documentation

See these docs.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader reads tab-separated data.

Call New for creating new TSV reader. Call Next before reading the next row.

It is expected that columns are separated by tabs while rows are separated by newlines.

Example
package main

import (
	"bytes"
	"fmt"
	"github.com/valyala/tsvreader"
)

func main() {
	bs := bytes.NewBufferString(
		"foo\t42\n" +
			"bar\t123\n")

	r := tsvreader.New(bs)
	for r.Next() {
		col1 := r.String()
		col2 := r.Int()
		fmt.Printf("col1=%s, col2=%d\n", col1, col2)
	}
	if err := r.Error(); err != nil {
		fmt.Printf("unexpected error: %s", err)
	}

}
Output:

col1=foo, col2=42
col1=bar, col2=123

func New

func New(r io.Reader) *Reader

New returns new Reader that reads TSV data from r.

func (*Reader) Bytes

func (tr *Reader) Bytes() []byte

Bytes returns the next bytes column value from the current row.

The returned value is valid until the next call to Reader.

func (*Reader) Date

func (tr *Reader) Date() time.Time

Date returns the next date column value from the current row.

date must be in the format YYYY-MM-DD

func (*Reader) DateTime

func (tr *Reader) DateTime() time.Time

DateTime returns the next datetime column value from the current row.

datetime must be in the format YYYY-MM-DD hh:mm:ss.

func (*Reader) Error

func (tr *Reader) Error() error

Error returns the last error.

func (*Reader) Float32

func (tr *Reader) Float32() float32

Float32 returns the next float32 column value from the current row.

func (*Reader) Float64

func (tr *Reader) Float64() float64

Float64 returns the next float64 column value from the current row.

func (*Reader) HasCols

func (tr *Reader) HasCols() bool

HasCols returns true if the current row contains unread columns.

An empty row doesn't contain columns.

This function may be used if TSV stream contains rows with different number of colums.

Example
package main

import (
	"bytes"
	"fmt"
	"github.com/valyala/tsvreader"
)

func main() {
	bs := bytes.NewBufferString(
		"foo\n" +
			"bar\tbaz\n" +
			"\n" +
			"a\tb\tc\n")

	r := tsvreader.New(bs)
	for r.Next() {
		for r.HasCols() {
			s := r.String()
			fmt.Printf("%q,", s)
		}
		fmt.Printf("\n")
	}
	if err := r.Error(); err != nil {
		fmt.Printf("unexpected error: %s", err)
	}

}
Output:

"foo",
"bar","baz",

"a","b","c",

func (*Reader) Int

func (tr *Reader) Int() int

Int returns the next int column value from the current row.

func (*Reader) Int16

func (tr *Reader) Int16() int16

Int16 returns the next int16 column value from the current row.

func (*Reader) Int32

func (tr *Reader) Int32() int32

Int32 returns the next int32 column value from the current row.

func (*Reader) Int64

func (tr *Reader) Int64() int64

Int64 returns the next int64 column value from the current row.

func (*Reader) Int8

func (tr *Reader) Int8() int8

Int8 returns the next int8 column value from the current row.

func (*Reader) Next

func (tr *Reader) Next() bool

Next advances to the next row.

Returns true if the next row does exist.

Next must be called after reading all the columns on the previous row. Check Error after Next returns false.

HasCols may be used for reading rows with variable number of columns.

Example
package main

import (
	"bytes"
	"fmt"
	"github.com/valyala/tsvreader"
)

func main() {
	bs := bytes.NewBufferString("1\n2\n3\n42\n")

	r := tsvreader.New(bs)
	for r.Next() {
		n := r.Int()
		fmt.Printf("%d\n", n)
	}
	if err := r.Error(); err != nil {
		fmt.Printf("unexpected error: %s", err)
	}

}
Output:

1
2
3
42

func (*Reader) Reset

func (tr *Reader) Reset(r io.Reader)

Reset resets the reader for reading from r.

func (*Reader) ResetError

func (tr *Reader) ResetError()

ResetError resets the current error, so the reader could proceed further.

func (*Reader) SkipCol

func (tr *Reader) SkipCol()

SkipCol skips the next column from the current row.

func (*Reader) String

func (tr *Reader) String() string

String returns the next string column value from the current row.

String allocates memory. Use Bytes to avoid memory allocations.

func (*Reader) Uint

func (tr *Reader) Uint() uint

Uint returns the next uint column value from the current row.

func (*Reader) Uint16

func (tr *Reader) Uint16() uint16

Uint16 returns the next uint16 column value from the current row.

func (*Reader) Uint32

func (tr *Reader) Uint32() uint32

Uint32 returns the next uint32 column value from the current row.

func (*Reader) Uint64

func (tr *Reader) Uint64() uint64

Uint64 returns the next uint64 column value from the current row.

func (*Reader) Uint8

func (tr *Reader) Uint8() uint8

Uint8 returns the next uint8 column value from the current row.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL