google datastore使用java将Date插入为null值

时间:2016-12-04 08:08:20

标签: java google-app-engine google-cloud-datastore

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字段插入为零或单个记录的空值使用Google Cloud Datastore时,我需要在数据库中插入一些记录,并将NULL作为日期值。我尝试了不同的方法,但它反映了插入错误。         / *

  As per this link  https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/datastore/src/main/java/com/google/datastore/snippets/TaskList.java

1 个答案:

答案 0 :(得分:2)

自最新编辑以来的更新

(我不认为你最新更新中的代码会编译,但忽略了...... :-))

您确定这些是正确的导入(我指的是appengine-api-1.0-sdk-1.9.24的API)吗?

您的示例使用:

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;

但是,您似乎正在编写不同的API (可能是low-level external API。这是为了在appengine之外使用,可能工作但是如果你只是使用regular appengine api可能会更容易,正如我在下面的原始答案中所示(注意我正在使用{{1不是setProperty)。

忽略上述内容,我怀疑在这些方面:

set

你应该像在这一行中那样使用值构建器:

.set("receiveddate",(receiveddate))    
.set("created", DateTime.now())

我相信您使用的是com.google.cloud.datastore API。尝试使用.set("description", StringValue.newBuilder(description). ,请参阅以下内容:

NullValue

原始答案

这似乎有效。只需调用处理程序,然后在数据存储区查看器中查看两个实体(在dev_appserver上测试):

// 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());

你正在做的事情有什么根本不同的东西吗?