在GCP数据存储区(Java)中存储`GeoPoint`的类

时间:2018-02-06 09:45:42

标签: google-cloud-platform google-cloud-datastore geopoints

我试图通过GCP Java客户端库在数据存储区中存储geo_point类型数据。我想出了如何处理Date类型数据,但无法得到我使用GeoPoint类的线索。

import com.google.datastore.v1.Entity;
import static com.google.datastore.v1.client.DatastoreHelper.makeValue;
import java.util.Date;

...

public class WriteToDatastoreFromTwitter {
    private static Value dValue(Date k) {
        return makeValue(k).setExcludeFromIndexes(true).build();
    }


    public static void main(String[] args) throws TwitterException {
        final Builder builder = Entity.newBuilder().
                setKey(key).
                putProperties("timestamp", dValue(tweet.getCreatedAt()));
                // How can I add a `geo_point` data?

我只是不确定是否应该使用datastore包之外的类,例如:https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/search/GeoPoint

1 个答案:

答案 0 :(得分:1)

我自己想通了。数据存储区包中的依赖包com.google.type中有a class LatLng,我可以使用它来成功存储geo_point数据。以下是初始化对象的方法:

import com.google.type.LatLng;

...

LatLng x = LatLng.
        newBuilder().
        setLatitude(loc.getLatitude()).
        setLongitude(loc.getLongitude()).
        build();

在我的情况下,我通过

存储它
private static Value gValue(LatLng k) {
    return makeValue(k).setExcludeFromIndexes(true).build();
}
builder.putProperties("geo_point", gValue(x));
相关问题