谷歌数据存储使用Java将日期插入为空值 [英] google datastore Insert Date as null value using java
问题描述
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();
}
}
尝试将DateTime字段插入0或特定记录的空值使用Google Cloud Datastore时,我需要将一些记录插入到数据库中,并将NULL作为日期值.我尝试了不同的方法,但它反映了插入错误. /*
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
推荐答案
自最新编辑以来的更新
(我认为您最新更新中的代码不会编译,但是会忽略... :-))
Updates since latest edits
(I do not think the code in your latest update will compile, but ignoring that... :-) )
您确定这些是正确的导入(我指的是appengine-api-1.0-sdk-1.9.24
的API)吗?
Are you sure these are the correct imports (I am referring to the APIs for appengine-api-1.0-sdk-1.9.24
)?
您的示例使用:
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
但是您似乎正在使用不同的API 编码(也许低级外部API ).这是为在Appengine外部使用而设计的,它可能可能工作,但是如果您仅使用
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).
我相信您正在使用 com.google.cloud .datastore API.尝试使用NullValue
参见下文:
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());
原始答案
这似乎有效.只需调用处理程序,然后在数据存储查看器中查看两个实体(在dev_appserver上进行了测试):
Original Answer
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?
这篇关于谷歌数据存储使用Java将日期插入为空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!