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

Add support for JSON export #3309

Merged
merged 57 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
e77d7e5
Put data before extensions in JSON response.
Mar 21, 2019
349b2e3
Parse query args in export request url.
Mar 22, 2019
62a1ceb
Add json_fmt field to ExportRequest protobuf message.
Mar 23, 2019
2cf24d5
Add opts arg to ExportOverNetwork().
Mar 23, 2019
0edb635
First working(?) version of JSON export.
Mar 25, 2019
bf0534a
Handle import of empy JSON arrays.
Mar 25, 2019
258b74f
JSON export uids as array of maps rather than array of strings.
Mar 25, 2019
132a3d3
Add reminder.
Mar 26, 2019
1dc16ad
Put data before extensions in JSON response.
Mar 21, 2019
b068040
Parse query args in export request url.
Mar 22, 2019
a81c6f8
Add json_fmt field to ExportRequest protobuf message.
Mar 23, 2019
98dd033
Add opts arg to ExportOverNetwork().
Mar 23, 2019
394f637
First working(?) version of JSON export.
Mar 25, 2019
b72439e
Handle import of empy JSON arrays.
Mar 25, 2019
1f06535
JSON export uids as array of maps rather than array of strings.
Mar 25, 2019
dd31ee9
Add reminder.
Mar 26, 2019
8913fb3
Merge branch 'javier/json_export' of github.com:dgraph-io/dgraph into…
Mar 26, 2019
b6aab03
Add language tags to JSON live load test.
Mar 26, 2019
97543b1
Fix language support in JSON export.
Mar 27, 2019
90ec634
Add clarifying comment.
Mar 27, 2019
abb4cdf
Add facets to JSON live load test.
Mar 27, 2019
4394527
Replace one of two string facets with a numeric one.
Mar 27, 2019
ccb6cfa
Output facets in JSON export.
Mar 27, 2019
1834ff8
Remove commented-out code from test.
Mar 27, 2019
e03b35f
Merge master.
Apr 1, 2019
46e7c8c
Add note about JSON exports to documentation.
Apr 1, 2019
6c6858b
Fix line length.
Apr 1, 2019
85f31de
Add export JSON test.
Apr 1, 2019
ab39fa1
Refactor out common code from JSON and RDF export tests.
Apr 1, 2019
b3e87e4
Simplify JSON export code.
Apr 2, 2019
0205c34
Replace long argument list with a struct in function call. Refactor c…
Apr 9, 2019
ed3217f
Replace export format flag with a string.
Apr 10, 2019
0183e60
Try to put all export format related settings in one spot.
Apr 11, 2019
21a9d64
More refactoring.
Apr 11, 2019
fd5c29f
Merge master
Apr 12, 2019
7d3aeb1
Merge master
Apr 17, 2019
6c0c377
Replace buf.WriteString()s with fmt.Fprintf()s.
Apr 18, 2019
42cf706
Make toRDF and toJSON methods instead of functions.
Apr 18, 2019
36e220f
Change all buf.Write* to fmt.Fprint* for consistency.
Apr 18, 2019
e6eec67
Return copy of buffer since buffer is re-used.
Apr 18, 2019
52ba8b9
Go back to no reusing buffer. Only use buffer in function where it is…
Apr 18, 2019
1a5dc38
Checkpoint...
Apr 18, 2019
e9613ac
Simplify (I think) and refactor JSON export code.
Apr 19, 2019
b495017
Merge master.
Apr 20, 2019
2f43571
Fix RDF export.
Apr 22, 2019
e37db21
Fix user of old --dgraph argument.
Apr 22, 2019
7520041
Fix logic bug.
Apr 22, 2019
f990d79
Refactor RDF exporter to share more code with JSON exporter.
Apr 22, 2019
1996e8c
Remove unused function.
Apr 22, 2019
2dba25a
Revert unnecessary changes.
Apr 22, 2019
38b58e6
Undo unnecessary change.
Apr 22, 2019
5706954
Add test of request arg handling.
Apr 23, 2019
7c070c3
Set HTTP status in response.
Apr 23, 2019
6893f79
Minor cleanup.
Apr 23, 2019
291d240
Changes suggested by PR review.
Apr 25, 2019
79ee305
Merge master
Apr 29, 2019
ed7bb1c
Fix schema file extension.
Apr 29, 2019
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
5 changes: 4 additions & 1 deletion chunker/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ func (jsonChunker) Chunk(r *bufio.Reader) (*bytes.Buffer, error) {
if err != nil {
return out, err
}
if ch != '{' {
if ch == ']' {
// Handle loading an empty JSON array ("[]") without error.
return nil, io.EOF
} else if ch != '{' {
return nil, fmt.Errorf("Expected JSON map start. Found: %v", string(ch))
}
x.Check2(out.WriteRune(ch))
Expand Down
27 changes: 25 additions & 2 deletions dgraph/cmd/alpha/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,31 @@ func exportHandler(w http.ResponseWriter, r *http.Request) {
if !handlerInit(w, r, http.MethodGet) {
return
}
// Export logic can be moved to dgraphzero.
if err := worker.ExportOverNetwork(context.Background()); err != nil {
if err := r.ParseForm(); err != nil {
x.SetHttpStatus(w, http.StatusBadRequest, "Parse of export request failed.")
return
}

format := worker.DefaultExportFormat
for key, vals := range r.Form {
switch key {
case "format":
if len(vals) > 1 {
x.SetHttpStatus(w, http.StatusBadRequest,
"Only one export format may be specified.")
return
}
format = worker.NormalizeExportFormat(vals[0])
if format == "" {
x.SetHttpStatus(w, http.StatusBadRequest, "Invalid export format.")
return
}
default:
x.SetHttpStatus(w, http.StatusBadRequest, "Invalid export argument.")
return
}
}
if err := worker.ExportOverNetwork(context.Background(), format); err != nil {
x.SetStatus(w, err.Error(), "Export failed.")
return
}
Expand Down
1 change: 1 addition & 0 deletions dgraph/cmd/alpha/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func queryHandler(w http.ResponseWriter, r *http.Request) {
x.SetStatusWithData(w, x.Error, err.Error())
return
}

out.WriteRune('{')
writeEntry("data", resp.Json)
out.WriteRune(',')
Expand Down
39 changes: 32 additions & 7 deletions dgraph/cmd/live/load-json/family.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"name":"Homer",
"age":"38",
"role":"father",
"parent_to": [
"role|gender":"male",
"role@es":"padre",
"role@fr":"père",
"role@hi":"पिता",
"parent_to":[
{ "uid":"_:b" },
{ "uid":"_:l" },
{ "uid":"_:m2" }
Expand All @@ -15,8 +19,12 @@
"name":"Marge",
"age":"34",
"role":"mother",
"role|gender":"female",
"role@es":"madre",
"role@fr":"mère",
"role@hi":"मां",
"aka":"Midge",
"parent_to": [
"parent_to":[
{ "uid":"_:b" },
{ "uid":"_:l" },
{ "uid":"_:m2" }
Expand All @@ -27,9 +35,13 @@
"name":"Bart",
"age":"10",
"role":"son",
"role|gender":"male",
"role@es":"hijo",
"role@fr":"fils",
"role@hi":"बेटा",
"aka":"El Barto",
"carries":"slingshot",
"sibling_of": [
"sibling_of":[
{ "uid":"_:l" },
{ "uid":"_:m2" }
]
Expand All @@ -39,8 +51,12 @@
"name":"Lisa",
"age":"8",
"role":"daughter",
"role|gender":"female",
"role@es":"hija",
"role@fr":"fille",
"role@hi":"बेटी",
"carries":"saxomophone",
"sibling_of": [
"sibling_of":[
{ "uid":"_:b" },
{ "uid":"_:m2" }
]
Expand All @@ -50,8 +66,13 @@
"name":"Maggie",
"age":"1",
"role":"daughter",
"role|gender":"female",
"role|generation":3,
"role@es":"hija",
"role@fr":"fille",
"role@hi":"बेटी",
"carries":"pacifier",
"sibling_of": [
"sibling_of":[
{ "uid":"_:b" },
{ "uid":"_:l" }
]
Expand All @@ -60,9 +81,13 @@
"uid":"_:a",
"name":"Abraham",
"age":"83",
"role":"father",
"role":"grandfather",
"role|gender":"male",
"role@es":"abuelo",
"role@fr":"grand-père",
"role@hi":"दादा",
"aka":"Grampa",
"parent_to": [
"parent_to":[
{ "uid":"_:h" }
]
}
Expand Down
2 changes: 1 addition & 1 deletion dgraph/cmd/live/load-json/family.schema
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name:string @index(term) .
age: int .
role:string @index(term) .
role:string @index(term) @lang .
aka:string @index(term) .
carries:string @index(term) .
parent_to: [uid] @reverse .
Expand Down
2 changes: 2 additions & 0 deletions dgraph/cmd/live/load-json/family1.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"name":"Homer",
"age":"38",
"role":"father",
"role|gender":"male",
"role@es":"padre",
"parent_to": [
{ "uid":"_:b" },
{ "uid":"_:l" },
Expand Down
3 changes: 3 additions & 0 deletions dgraph/cmd/live/load-json/family2.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"name":"Maggie",
"age":"1",
"role":"daughter",
"role|gender":"female",
"role|generation":3,
"role@es":"hija",
"carries":"pacifier",
"sibling_of": [
{ "uid":"_:b" },
Expand Down
36 changes: 28 additions & 8 deletions dgraph/cmd/live/load-json/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,22 @@ func checkLoadedData(t *testing.T) {
q(func: anyofterms(name, "Homer")) {
name
age
role
role @facets(gender,generation)
role@es
}
}
`)
require.NoError(t, err)
z.CompareJSON(t, `
{
"q": [
"q": [
{
"name": "Homer",
"age": 38,
"role": "father"
}
"role": "father",
"role@es": "padre",
"role|gender": "male"
}
]
}
`, string(resp.GetJson()))
Expand All @@ -68,25 +71,42 @@ func checkLoadedData(t *testing.T) {
{
q(func: anyofterms(name, "Maggie")) {
name
role
role @facets(gender,generation)
role@es
carries
}
}
`)
require.NoError(t, err)
z.CompareJSON(t, `
{
"q": [
"q": [
{
"name": "Maggie",
"role": "daughter",
"carries": "pacifier"
}
"role@es": "hija",
"carries": "pacifier",
"role|gender": "female",
"role|generation": 3
}
]
}
`, string(resp.GetJson()))
}

func TestLiveLoadJSONFileEmpty(t *testing.T) {
z.DropAll(t, dg)

pipeline := [][]string{
{"echo", "[]"},
{os.ExpandEnv("$GOPATH/bin/dgraph"), "live",
"--schema", testDataDir + "/family.schema", "--files", "/dev/stdin",
"--alpha", alphaService},
}
err := z.Pipeline(pipeline)
require.NoError(t, err, "live loading JSON file ran successfully")
}

func TestLiveLoadJSONFile(t *testing.T) {
z.DropAll(t, dg)

Expand Down
2 changes: 1 addition & 1 deletion dgraph/cmd/live/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (l *loader) processLoadFile(ctx context.Context, rd *bufio.Reader, ck chunk
// batches (each one containing opt.batchSize entries) and sends the batches
// to the loader.reqs channel
func (l *loader) processChunk(chunkBuf *bytes.Buffer, ck chunker.Chunker) {
if chunkBuf == nil && chunkBuf.Len() == 0 {
if chunkBuf == nil || chunkBuf.Len() == 0 {
return
}

Expand Down
7 changes: 4 additions & 3 deletions protos/pb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,10 @@ message BackupRequest {
}

message ExportRequest {
uint32 group_id = 1; // Group id to back up.
uint64 read_ts = 2;
int64 unix_ts = 3;
uint32 group_id = 1; // Group id to back up.
uint64 read_ts = 2;
int64 unix_ts = 3;
string format = 4;
}

// vim: noexpandtab sw=2 ts=2
Loading