Skip to content

Commit

Permalink
service: add csv.delim query param to load API (#4333)
Browse files Browse the repository at this point in the history
The csv.delim query parameter to the load API (POST
/pool/{pool}/branch/{branch}) specifies the field delimiter for CSV
input, allowing use of a character other than ",".

For #4238.
  • Loading branch information
nwt authored Jan 26, 2023
1 parent 3212008 commit 21e80a3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/lake/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ POST /pool/{pool}/branch/{branch}
| branch | string | path | **Required.** Name of branch to which data will be loaded. |
| | various | body | **Required.** Contents of the posted data. |
| Content-Type | string | header | MIME type of the posted content. If undefined, the service will attempt to introspect the data and determine type automatically. |
| csv.delim | string | query | Exactly one character specifing the field delimiter for CSV data. Defaults to ",". |

**Example Request**

Expand Down
10 changes: 10 additions & 0 deletions service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/brimdata/zed/service/srverr"
"github.com/brimdata/zed/zio"
"github.com/brimdata/zed/zio/anyio"
"github.com/brimdata/zed/zio/csvio"
"github.com/brimdata/zed/zio/zngio"
"github.com/segmentio/ksuid"
)
Expand Down Expand Up @@ -333,6 +334,14 @@ func handleBranchLoad(c *Core, w *ResponseWriter, r *Request) {
if !ok {
return
}
var csvDelim rune
if s := r.URL.Query().Get("csv.delim"); s != "" {
if len(s) != 1 {
w.Error(srverr.ErrInvalid(`invalid query param "csv.delim": must be exactly one character`))
return
}
csvDelim = rune(s[0])
}
message, ok := r.decodeCommitMessage(w)
if !ok {
return
Expand Down Expand Up @@ -375,6 +384,7 @@ func handleBranchLoad(c *Core, w *ResponseWriter, r *Request) {
}
opts := anyio.ReaderOpts{
Format: format,
CSV: csvio.ReaderOpts{Delim: csvDelim},
// Force validation of ZNG when loading into the lake.
ZNG: zngio.ReaderOpts{Validate: true},
}
Expand Down
29 changes: 29 additions & 0 deletions service/ztests/curl-load-csv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
script: |
source service.sh
zed create -q test
curl -H Content-Type:text/csv --data-binary @in.csv \
--fail $ZED_LAKE/pool/test/branch/main | zq -z commit:=0 -
curl -H Content-Type:text/csv --data-binary @in-dot.csv \
--fail $ZED_LAKE/pool/test/branch/main?csv.delim=. | zq -z commit:=0 -
echo //
zed query -z 'from test'
inputs:
- name: in.csv
data: |
a,b
1,2
- name: in-dot.csv
data: |
a.b
3.4
- name: service.sh

outputs:
- name: stdout
data: |
{commit:0,warnings:[]([string])}
{commit:0,warnings:[]([string])}
//
{a:1.,b:2.}
{a:3.,b:4.}

0 comments on commit 21e80a3

Please sign in to comment.