How to add "Date Type Facet" by using Dgraph4j

Hi all,
Does Dgraph4J supports add “Date Type Facet” to a Predicate?
My code is:

DgraphProto.Mutation.Builder builder = DgraphProto.Mutation.newBuilder();
DgraphProto.NQuad.Builder builder1 = DgraphProto.NQuad.newBuilder().setSubject("_:e").setPredicate(
"address").setObjectValue(DgraphProto.Value.newBuilder().setStrVal(Long.toString(System.currentTimeMillis()/1000)).build());
builder1.addFacets(DgraphProto.Facet.newBuilder().setKey("sendDate").setValType(DgraphProto.Facet.ValType.DATETIME).setValue(ByteString.copyFromUtf8("1136214246L")));
builder.addSet(builder1.build());
transaction.mutate(builder.build());
transaction.commit();

Then I got an Exception:

Exception in thread "main" io.grpc.StatusRuntimeException: UNKNOWN: Error while parsing facet: [key:"sendDate" value:"1136214246L" val_type:DATETIME ] error: Time.UnmarshalBinary: unsupported version
	at io.grpc.Status.asRuntimeException(Status.java:526)
	at io.grpc.stub.ClientCalls$StreamObserverToCallListenerAdapter.onClose(ClientCalls.java:434)
	at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)
	at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23)
	............

Then I change String “1136214246L” to “2007-05-01T15:43:26.3452-07:00”.But It doesn’t work.
SO,could anyone tell me How to add “Date Type Facet” using Dgraph4j ?
Thanks

Is it RFC3339?

Note

Dgraph supports date and time formats for dateTime scalar type only if they are RFC 3339 compatible which is different from ISO 8601(as defined in the RDF spec). You should convert your values to RFC 3339 format before sending them to Dgraph.

time.Time (RFC3339 format [Optional timezone] eg: 2006-01-02T15:04:05.999999999+10:00 or 2006-01-02T15:04:05.999999999)

Source: https://docs.dgraph.io/query-language#schema-types

BUT, Since this is Facets, you can add whatever you want there. This error must be something else. Add it as string.

ps. I’m not a Java Dev.

Thank you for your replay.I try “RFC 3339” and “Long” format.But the result is the same.
I know I can insert it as a string, but is there a performance problem ?
In other words, does Dgraph optimize for “Date Type”?

I don’t think so, just testing it.

If it appears in the Facet in the right format. Dgraph will deal with date as it intent to.

You can also try to do what you doing with a JSON object (JSON mutation). That’s a good way to be sure that you may doing something wrong with the procedures in JAVA. If you are able to insert the Date Type as you’re doing but in JSON, so you have a clue.

Cheers

Ok,Thanks.

I’m going to try JSON to find a clue.

ps. I prefer to use RDF:)

The error occurred because the server is expecting a binary format of time

If the client is go, the binary format should be created using the Marshal method in the time package
https://golang.org/src/time/time.go

But since this is the java client and it seems the implementation above is specific to go, we’ll need to figure something out. Will get back to this shortly after discussing with others internally.

1 Like

Also on the server side, we expect the source format of facet to be “BinaryID”

I’ve put a PR to fix this issue Validate and converting facets to binary by gitlw · Pull Request #2797 · dgraph-io/dgraph · GitHub

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.