Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Support for Custom Line Break Characters #27

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"math"
"os"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -81,8 +82,8 @@ var (
retryInfo map[int]int
showVersion bool
queueSize int

bufferPool = sync.Pool{
lineDelimiter byte = '\n'
bufferPool = sync.Pool{
New: func() interface{} {
return make([]byte, 0, bufferSize)
},
Expand Down Expand Up @@ -191,6 +192,24 @@ func initFlags() {
utils.InitLog(logLevel)
}

// Restore hex escape sequences like \xNN to their corresponding characters
func restoreHexEscapes(s1 string) (string, error) {
if s1 == `\n` {
return "\n", nil
}

re := regexp.MustCompile(`\\x([0-9A-Fa-f]{2})`)

return re.ReplaceAllStringFunc(s1, func(match string) string {
hexValue := match[2:] // Remove the \x prefix
decValue, err := strconv.ParseInt(hexValue, 16, 0)
if err != nil {
return match
}
return string(rune(decValue))
}), nil
}

//go:generate go run gen_version.go
func paramCheck() {
if showVersion {
Expand Down Expand Up @@ -253,6 +272,19 @@ func paramCheck() {
if strings.ToLower(kv[0]) == "format" && strings.ToLower(kv[1]) != "csv" {
enableConcurrency = false
}

if strings.ToLower(kv[0]) == "line_delimiter" {

restored, err := restoreHexEscapes(kv[1])
if err != nil || len(restored) != 1 {
log.Errorf("line_delimiter invalid: %s", kv[1])
os.Exit(1)
} else {
lineDelimiter = restored[0]
}

}

if len(kv) > 2 {
headers[kv[0]] = strings.Join(kv[1:], ":")
} else {
Expand Down Expand Up @@ -369,7 +401,7 @@ func main() {
streamLoad.Load(workers, maxRowsPerTask, maxBytesPerTask, &retryInfo)
reporter.Report()
defer reporter.CloseWait()
reader.Read(reporter, workers, maxBytesPerTask, &retryInfo, loadResp, retryCount)
reader.Read(reporter, workers, maxBytesPerTask, &retryInfo, loadResp, retryCount, lineDelimiter)
reader.Close()

streamLoad.Wait(loadInfo, retryCount, &retryInfo, startTime)
Expand Down
16 changes: 11 additions & 5 deletions reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (
"sync/atomic"
"time"

log "github.com/sirupsen/logrus"
report "doris-streamloader/report"
loader "doris-streamloader/loader"
report "doris-streamloader/report"

log "github.com/sirupsen/logrus"
)

type FileReader struct {
Expand Down Expand Up @@ -108,7 +109,8 @@ func NewFileReader(filePaths string, batchRows int, batchBytes int, bufferSize i
}

// Read File
func (f *FileReader) Read(reporter *report.Reporter, workers int, maxBytesPerTask int, retryInfo *map[int]int, loadResp *loader.Resp, retryCount int) {
func (f *FileReader) Read(reporter *report.Reporter, workers int, maxBytesPerTask int, retryInfo *map[int]int,
loadResp *loader.Resp, retryCount int, lineDelimiter byte) {
index := 0
data := f.pool.Get().([]byte)
count := f.batchRows
Expand All @@ -125,16 +127,20 @@ func (f *FileReader) Read(reporter *report.Reporter, workers int, maxBytesPerTas
for _, file := range f.files {
loadResp.LoadFiles = append(loadResp.LoadFiles, file.Name())
reader := bufio.NewReaderSize(file, f.bufferSize)

for {
if atomic.LoadUint64(&reporter.FinishedWorkers) == atomic.LoadUint64(&reporter.TotalWorkers) {
return
}
line, err := reader.ReadBytes('\n')
if err == io.EOF {
line, err := reader.ReadBytes(lineDelimiter)
if err == io.EOF && len(line) == 0 {
file.Close()
break
} else if err != nil {
log.Errorf("Read file failed, error message: %v, before retrying, we suggest:\n1.Check the input data files and fix if there is any problem.\n2.Do select count(*) to check whether data is partially loaded.\n3.If the data is partially loaded and duplication is unacceptable, consider dropping the table (with caution that all data in the table will be lost) and retry.\n4.Otherwise, just retry.\n", err)
if len(line) !=0 {
log.Error("5.When using a specified line delimiter, the file must end with that delimiter.")
}
os.Exit(1)
}

Expand Down
Loading