Java - 如何从 Google Cloud Datastore 中删除实体

Java - How to delete an entity from Google Cloud Datastore

架构:我有一个 Web 应用程序,我从那里与数据存储区和一个客户端 (raspberry pi) 交互,它使用 Google Cloud Endpoints 从 Web 应用程序调用方法。

我必须补充一点,我对 Web 应用程序不是很熟悉,我认为 setConsumed() 方法有问题,因为我可以在应用程序引擎仪表板中看到 /create 的调用,但没有条目/setConsumed.

我可以使用 objectify 将实体添加到数据存储区:

//client method
private static void sendSensorData(long index, String serialNumber) throws IOException {
    SensorData data = new SensorData();
    data.setId(index+1);
    data.setSerialNumber(serialNumber);
    sensor.create(data).execute();
}

//api method in the web application
@ApiMethod(name = "create", httpMethod = "post")
public SensorData create(SensorData data, User user) {
    // check if user is authenticated and authorized
    if (user == null) {
        log.warning("User is not authenticated");
        System.out.println("Trying to authenticate user...");
        createUser(user);
        // throw new RuntimeException("Authentication required!");
    } else if (!Constants.EMAIL_ADDRESS.equals(user.getEmail())) {

        log.warning("User is not authorised, email: " + user.getEmail());
        throw new RuntimeException("Not authorised!");
    }
    data.save();

    return data;
}

//method in entity class SensorData
public Key<SensorData> save() {
    return ofy().save().entity(this).now();
}

但是,我无法使用以下代码从数据存储中删除实体。

编辑: Stackdriver Logging 中有很多创建请求的日志,但是 none 个 setConsumed()。所以看起来调用甚至没有到达 API 尽管两种方法都在相同的 class.

编辑 2: 当我从 Powershell 调用该方法时实体被删除,因此问题很可能出在客户端。

//client method
private static void removeSensorData(long index) throws IOException {
    sensor.setConsumed(index+1);
}

//api method in the web application
@ApiMethod(name = "setConsumed", httpMethod = "put")
public void setConsumed(@Named("id") Long id, User user) {
    // check if user is authenticated and authorized
    if (user == null) {
        log.warning("User is not authenticated");
        System.out.println("Trying to authenticate user...");
        createUser(user);
        // throw new RuntimeException("Authentication required!");
    } else if (!Constants.EMAIL_ADDRESS.equals(user.getEmail())) {

        log.warning("User is not authorised, email: " + user.getEmail());
        throw new RuntimeException("Not authorised!");
    }

    Key serialKey = KeyFactory.createKey("SensorData", id);
    datastore.delete(serialKey);
}

终于可以自己解决了!

问题只与用于 removeSensorData(long index) 的索引的数据类型有关,它来自 for-loop,因此是整数而不是长整数。

这是我从数据存储中删除实体的方法。

public boolean deleteEntity(String propertyValue) {
    String entityName = "YOUR_ENTITY_NAME";
    String gql = "SELECT * FROM "+entityName +" WHERE property= "+propertyValue+"";
    Query<Entity> query = Query.newGqlQueryBuilder(Query.ResultType.ENTITY, gql)
            .setAllowLiteral(true).build();
    try{
        QueryResults<Entity> results = ds.run(query);           
        if (results.hasNext()) {
            Entity rs = results.next();             
            ds.delete(rs.getKey());
            return true;
        }
        return false;
    }catch(Exception e){
        logger.error(e.getMessage());
        return false;
    }
}

如果你不想使用文字,你也可以使用如下绑定:

String gql = "SELECT * FROM "+entityName+" WHERE property1= @prop1 AND property2= @prop2";      
Query<Entity> query = Query.newGqlQueryBuilder(Query.ResultType.ENTITY, gql)
            .setBinding("prop1", propertyValue1)
            .setBinding("prop2", propertyValue2)
            .build();

希望对您有所帮助。