-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b515ac
commit 5b9d000
Showing
9 changed files
with
242 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,5 @@ go.work.sum | |
|
||
avro/ | ||
map.go | ||
*.schema | ||
*.schema | ||
*.pgo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
cmd/* | ||
cmd/*.json | ||
cmd/*.parquet | ||
*.json | ||
*.parquet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.json | ||
*.parquet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/goccy/go-json" | ||
|
||
"github.com/redpanda-data/benthos/v4/public/bloblang" | ||
) | ||
|
||
// jcleaner takes as input a JSONL file, and removes all null fields, empty arrays, | ||
// empty objects and empty strings. | ||
func main() { | ||
inputFile := flag.String("in", "", "input file") | ||
outputFile := flag.String("out", "", "output file") | ||
flag.Parse() | ||
if *inputFile == "" { | ||
log.Fatal("no input file specified") | ||
} | ||
if *outputFile == "" { | ||
log.Fatal("no output file specified") | ||
} | ||
problemLines := fileNameWithoutExt(*outputFile) + "_problem.json" | ||
f, err := os.Open(*inputFile) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer func() { | ||
if r := recover(); r != nil { | ||
fmt.Println(err) | ||
} | ||
}() | ||
defer f.Close() | ||
bloblangMapping := `map remove_null_empty { | ||
root = match { | ||
(this.type() == "object" && this.length() == 0) => deleted() | ||
this.type() == "object" => this.map_each(i -> i.value.apply("remove_null_empty")) | ||
(this.type() == "array" && this.length() == 0) => deleted() | ||
this.type() == "array" => this.map_each(v -> v.apply("remove_null_empty")) | ||
this.type() == "null" => deleted() | ||
this.type() == "string" && this.length() == 0 => deleted() | ||
} | ||
} | ||
root = this.apply("remove_null_empty")` | ||
exe, err := bloblang.Parse(bloblangMapping) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
|
||
nf, err := os.Create(*outputFile) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer nf.Close() | ||
w := bufio.NewWriterSize(nf, 1024*4) | ||
|
||
pf, err := os.Create(problemLines) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer pf.Close() | ||
pw := bufio.NewWriterSize(nf, 1024*4) | ||
|
||
r := bufio.NewReaderSize(f, 1024*4) | ||
s := bufio.NewScanner(r) | ||
newline := []byte("\n") | ||
for s.Scan() { | ||
y := s.Bytes() | ||
b, err := ApplyBloblangMapping(y, exe) | ||
if err != nil { | ||
pw.Write(y) | ||
pw.Write(newline) | ||
continue | ||
} | ||
_, err = w.Write(b) | ||
if err != nil { | ||
pw.Write(y) | ||
pw.Write(newline) | ||
continue | ||
} | ||
w.Write(newline) | ||
} | ||
w.Flush() | ||
} | ||
|
||
func ApplyBloblangMapping(jsonInput []byte, exe *bloblang.Executor) ([]byte, error) { | ||
// Parse the JSON input into a map[string]interface{} | ||
var inputMap map[string]interface{} | ||
if err := json.Unmarshal(jsonInput, &inputMap); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Execute the Bloblang mapping | ||
res, err := exe.Query(inputMap) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Convert the result back into a JSON string | ||
jsonResult, err := json.Marshal(res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return jsonResult, nil | ||
} | ||
|
||
func fileNameWithoutExt(fileName string) string { | ||
return fileName[:len(fileName)-len(filepath.Ext(fileName))] | ||
} |
Oops, something went wrong.