Reputation: 13
package com.example.guestbook;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import java.io.IOException;
import java.util.Date;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Key addTask(String description, DateTime receiveddate) {
Key key = datastore.allocateId(keyFactory.newKey());
Entity task = Entity.newBuilder(key)
.set("description", StringValue.newBuilder(description).
setExcludeFromIndexes(true).build())
.set("receiveddate",(receiveddate))
.set("created", DateTime.now())
.set("done", false)
.build();
datastore.put(task);
return key;
}
void handleCommandLine(String commandLine) {
String[] args = commandLine.split("\\s+");
if (args.length < 1) {
throw new IllegalArgumentException("not enough args");
}
String command = args[0];
switch (command) {
case "new":
args = commandLine.split("\\s+", 3);
if (args.length != 3) {
throw new IllegalArgumentException("missing description");
}
addTask(args[1],DateTime.copyFrom());
System.out.println("task added");
break;
public static void main(String[] args) throws Exception {
TaskList taskList = new TaskList();
System.out.println("Cloud Datastore Task List");
System.out.println();
printUsage();
while (true) {
String commandLine = System.console().readLine("> ");
if (commandLine.trim().isEmpty()) {
break;
}
try {
taskList.handleCommandLine(commandLine);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
printUsage();
}
}
System.out.println("exiting");
System.exit(0);
}
private static void printUsage() {
System.out.println("Usage:");
System.out.println();
System.out.println(" new <description> Adds a task with a description <description>");
System.out.println(" done <task-id> Marks a task as done");
System.out.println(" list Lists all tasks by creation time");
System.out.println(" delete <task-id> Deletes a task");
System.out.println();
}
}
Trying to insert DateTime field as 0 or null value for a partiular record While using Google Cloud Datastore, I need to insert a few records into the database with NULL as the date value. I've tried different ways but it is reflecting errors in inserting. /*
As per this link https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/datastore/src/main/java/com/google/datastore/snippets/TaskList.java
Upvotes: 0
Views: 1718
Reputation: 3250
(I do not think the code in your latest update will compile, but ignoring that... :-) )
Are you sure these are the correct imports (I am referring to the APIs for appengine-api-1.0-sdk-1.9.24
)?
Your example uses:
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
However you seem to be coding to a different API (perhaps the low-level external API). This is meant for use outside appengine, it might work but it might be easier if you just use the regular appengine api as I showed in my original answer below (Notice I am using setProperty
not set
).
Overlooking the above, I suspect that in these lines:
.set("receiveddate",(receiveddate))
.set("created", DateTime.now())
you should have used a value builder as you did in this line:
.set("description", StringValue.newBuilder(description).
I believe you are using the com.google.cloud.datastore API. Try using NullValue
see below:
// beware untested code!
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.DateTime;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.KeyFactory;
import com.google.cloud.datastore.NullValue;
Datastore datastore =
DatastoreOptions.getDefaultInstance().getService();
KeyFactory keyFactory = datastore.newKeyFactory().setKind("Dummy");
Entity.Builder entity0 = Entity.newBuilder(keyFactory.newKey("abc"));
DateTime receiveddate = null /* or some other value */;
entity0.set("receiveddate", receiveddate == null ? NullValue.of() : receiveddate);
entity0.set("created", DateTime.now());
This seems to work. Just invoke the handler and then see the two entities in the datastore viewer (tested on dev_appserver):
package com.example.guestbook;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import java.io.IOException;
import java.util.Date;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GuestbookServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity entity0 = new Entity("Dummy", "abc");
entity0.setProperty("created", new Date());
datastore.put(entity0);
Entity entity1 = new Entity("Dummy", "xyz");
entity1.setProperty("created", null);
datastore.put(entity1);
}
}
Is there something fundamentally different about what you are doing?
Upvotes: 2