How do I read in a large flat file in Golang

2019-03-19 15:41发布

I have a flat file that has 339276 line of text in it for a size of 62.1 MB. I am attempting to read in all the lines, parse them based on some conditions I have and then insert them into a database.

I originally attempted to use a bufio.Scan() loop and bufio.Text() to get the line but I was running out of buffer space. I switched to using bufio.ReadLine/ReadString/ReadByte (I tried each) and had the same problem with each. I didn't have enough buffer space.

I tried using read and setting the buffer size but as the document says it actually a const that can be made smaller but never bigger that 64*1024 bytes. I then tried to use File.ReadAt where I set the starting postilion and moved it along as I brought in each section to no avail. I have looked at the following examples and explanations (not an exhaustive list):

Read text file into string array (and write) How to Read last lines from a big file with Go every 10 secs reading file line by line in go

How do I read in an entire file (either line by line or the whole thing at once) into a slice so I can then go do things to the lines?

Here is some code that I have tried:

                 file, err := os.Open(feedFolder + value)
                 handleError(err)
                 defer file.Close()
                 //              fileInfo, _ := file.Stat()
                 var linesInFile []string

             r := bufio.NewReader(file)
             for {
                     path, err := r.ReadLine("\n") // 0x0A separator = newline

                     linesInFile = append(linesInFile, path)
                     if err == io.EOF {
                             fmt.Printf("End Of File: %s", err)
                             break
                     } else if err != nil {
                             handleError(err) // if you return error
                     }
             }
             fmt.Println("Last Line: ", linesInFile[len(linesInFile)-1])

Here is something else I tried:

var fileSize int64 = fileInfo.Size()
    fmt.Printf("File Size: %d\t", fileSize)
    var bufferSize int64 = 1024 * 60
    bytes := make([]byte, bufferSize)
    var fullFile []byte
    var start int64 = 0
    var interationCounter int64 = 1
    var currentErr error = nil
         for currentErr != io.EOF {
            _, currentErr = file.ReadAt(bytes, st)
            fullFile = append(fullFile, bytes...)
            start = (bufferSize * interationCounter) + 1
            interationCounter++
          }
     fmt.Printf("Err: %s\n", currentErr)
     fmt.Printf("fullFile Size: %s\n", len(fullFile))
     fmt.Printf("Start: %d", start)

     var currentLine []string


   for _, value := range fullFile {
      if string(value) != "\n" {
          currentLine = append(currentLine, string(value))
      } else {
         singleLine := strings.Join(currentLine, "")
         linesInFile = append(linesInFile, singleLine)
         currentLine = nil
              }   
      }

I am at a loss. Either I don't understand exactly how the buffer works or I don't understand something else. Thanks for reading.

标签: go buffer
3条回答
我命由我不由天
2楼-- · 2019-03-19 15:45

bufio.Scan() and bufio.Text() in a loop perfectly works for me on a files with much larger size, so I suppose you have lines exceeded buffer capacity. Then

  • check your line ending
  • and which Go version you use path, err :=r.ReadLine("\n") // 0x0A separator = newline? Looks like func (b *bufio.Reader) ReadLine() (line []byte, isPrefix bool, err error) has return value isPrefix specifically for your use case http://golang.org/pkg/bufio/#Reader.ReadLine
查看更多
倾城 Initia
3楼-- · 2019-03-19 15:45

It seems to me this variant of readLines is shorter and faster than suggested peterSO

func readLines(filename string) (map[int]string, error) {
    lines := make(map[int]string)

    data, err := ioutil.ReadFile(filename)
    if err != nil {
        return nil, err
    }

    for n, line := range strings.Split(string(data), "\n") {
        lines[n] = line
    }

    return lines, nil
}
查看更多
乱世女痞
4楼-- · 2019-03-19 15:47

It's not clear that it's necessary to read in all the lines before parsing them and inserting them into a database. Try to avoid that.

You have a small file: "a flat file that has 339276 line of text in it for a size of 62.1 MB." For example,

package main

import (
    "bytes"
    "fmt"
    "io"
    "io/ioutil"
)

func readLines(filename string) ([]string, error) {
    var lines []string
    file, err := ioutil.ReadFile(filename)
    if err != nil {
        return lines, err
    }
    buf := bytes.NewBuffer(file)
    for {
        line, err := buf.ReadString('\n')
        if len(line) == 0 {
            if err != nil {
                if err == io.EOF {
                    break
                }
                return lines, err
            }
        }
        lines = append(lines, line)
        if err != nil && err != io.EOF {
            return lines, err
        }
    }
    return lines, nil
}

func main() {
    // a flat file that has 339276 lines of text in it for a size of 62.1 MB
    filename := "flat.file"
    lines, err := readLines(filename)
    fmt.Println(len(lines))
    if err != nil {
        fmt.Println(err)
        return
    }
}
查看更多
登录 后发表回答