|
1 | | -Google Cloud Datastore Json API Java |
| 1 | +# GCD Json Java |
| 2 | + |
| 3 | +GCD Json Java is a Java data access API designed for the Google App Engine datastore (https://cloud.google.com/datastore/docs/) through JSON API reference (https://cloud.google.com/datastore/docs/apis/v1beta2/). In this version, you can do basic actions like : commit, query and lookup. |
| 4 | + |
| 5 | +## To use library : |
| 6 | + |
| 7 | +- You need an appengine application (https://console.developers.google.com/project) with Google Cloud Datastore API status is On. |
| 8 | + - Create your own project. Visit https://console.developers.google.com > Create Project, enter project name and project id. |
| 9 | + - Important : Enable Google Cloud Datastore API. Visit https://console.developers.google.com. Choose your created project > APIs & auth (left menu) > APIs > Google Cloud Datastore API > On. |
| 10 | +- Some information to config. Choose your created project > APIs & auth (left menu) > Credentials > Create New Client ID > Chooose Service account > Create Client ID. |
| 11 | + - projectName : is your project id with "s~" before, example your project id : "your-project-id" -> projectName = "s~your-project-id". |
| 12 | + - iss : is Email address. |
| 13 | + - p12 key file : click Generate new P12 key, download, keyLocation variable is path to your p12 Key file. |
| 14 | + |
| 15 | +## Sample code : |
| 16 | + |
| 17 | +- Config |
| 18 | +``` |
| 19 | + String projectName = "s~your-project-id"; |
| 20 | + String iss = "xxxx20893014-d1kh9putd2n3hbqjsjlsbes1i8spxxx@developer.gserviceaccount.com"; |
| 21 | + String keyLocation = "your-project-id-xxxxx.p12"; |
| 22 | + GCDConfig config = new GCDConfig(projectName, iss, keyLocation); |
| 23 | + GCDService ds = GCDServiceFactory.getInstance(config); |
| 24 | +``` |
| 25 | + |
| 26 | +- Registry your entry |
| 27 | + - Mark annotation @Entity for your entry. |
| 28 | + - Mark annotation @Id for entry key. |
| 29 | + - Mark annotation @Index for index field. |
| 30 | + |
| 31 | +``` |
| 32 | + import cloud.google.datastore.annotation.Annotation.Entity; |
| 33 | + import cloud.google.datastore.annotation.Annotation.Id; |
| 34 | + import cloud.google.datastore.annotation.Annotation.Index; |
| 35 | +
|
| 36 | + @Entity |
| 37 | + public class Foo { |
| 38 | +
|
| 39 | + @Id |
| 40 | + private String id = ""; |
| 41 | +
|
| 42 | + private String name = ""; |
| 43 | +
|
| 44 | + @Index |
| 45 | + private String category = ""; |
| 46 | +
|
| 47 | + @Index |
| 48 | + private Date doc = Calendar.getInstance().getTime(); |
| 49 | +
|
| 50 | + @Index |
| 51 | + private int status = 1; |
| 52 | +
|
| 53 | + } |
| 54 | +``` |
| 55 | + |
| 56 | +- Commit |
| 57 | + |
| 58 | +``` |
| 59 | + // Insert single entry |
| 60 | + String projectName = "s~your-project-id"; |
| 61 | + String iss = "xxxx20893014-d1kh9putd2n3hbqjsjlsbes1i8spxxx@developer.gserviceaccount.com"; |
| 62 | + String keyLocation = "your-project-id-xxxxx.p12"; |
| 63 | + GCDConfig config = new GCDConfig(projectName, iss, keyLocation); |
| 64 | + GCDService ds = GCDServiceFactory.getInstance(config); |
| 65 | +
|
| 66 | + Foo f = new Foo(); |
| 67 | + f.setId("entry-01"); |
| 68 | + f.setName("Entry 01"); |
| 69 | + List<Key<Foo>> listKey = ds.commit(Foo.class).entities(f).insert(); |
| 70 | +``` |
| 71 | + |
| 72 | +``` |
| 73 | + // Insert multi entry |
| 74 | + Foo f1 = new Foo(); |
| 75 | + f1.setId("entry-01"); |
| 76 | + f1.setName("Entry 01"); |
| 77 | +
|
| 78 | + Foo f2 = new Foo(); |
| 79 | + f2.setId("entry-02"); |
| 80 | + f2.setName("Entry 02"); |
| 81 | +
|
| 82 | + List<Key<Foo>> listKey = ds.commit(Foo.class).entities(f1, f2).insert(); |
| 83 | +``` |
| 84 | + |
| 85 | + |
| 86 | +``` |
| 87 | + // Insert list entry |
| 88 | + Foo f1 = new Foo(); |
| 89 | + f1.setId("entry-01"); |
| 90 | + f1.setName("Entry 01"); |
| 91 | +
|
| 92 | + Foo f2 = new Foo(); |
| 93 | + f2.setId("entry-02"); |
| 94 | + f2.setName("Entry 02"); |
| 95 | +
|
| 96 | + List<Foo> list = new ArrayList<Foo>(); |
| 97 | + list.add(f1); |
| 98 | + list.add(f2); |
| 99 | +
|
| 100 | + List<Key<Foo>> listKey = ds.commit(Foo.class).entities(list).insert(); |
| 101 | +``` |
| 102 | + |
| 103 | +``` |
| 104 | + // Update single entry |
| 105 | + Foo f = new Foo(); |
| 106 | + f.setId("entry-01"); |
| 107 | + f.setName("Entry 01"); |
| 108 | + List<Key<Foo>> listKey = ds.commit(Foo.class).entities(f).update(); |
| 109 | +``` |
| 110 | + |
| 111 | +``` |
| 112 | + // Delete single entry |
| 113 | + Foo f = new Foo(); |
| 114 | + f.setId("entry-01"); |
| 115 | + f.setName("Entry 01"); |
| 116 | + boolean success = ds.commit(Foo.class).entities(f).delete(); |
| 117 | +``` |
| 118 | + |
| 119 | +- Lookup |
| 120 | + |
| 121 | +``` |
| 122 | + // Lookup single entry |
| 123 | + String id = "entry-01"; |
| 124 | + Foo lookupFoo = ds.lookup(Foo.class).id(f.getId()).get(); |
| 125 | +``` |
| 126 | + |
| 127 | +``` |
| 128 | + // Lookup multi entry |
| 129 | + String id1 = "entry-01"; |
| 130 | + String id2 = "entry-01"; |
| 131 | + List<Foo> listResult = ds.lookup(Foo.class).ids(id1, id2).list(); |
| 132 | +``` |
| 133 | + |
| 134 | +``` |
| 135 | + // Lookup list entry |
| 136 | + String id1 = "entry-01"; |
| 137 | + String id2 = "entry-01"; |
| 138 | + List<String> listId = new ArrayList<String>(); |
| 139 | + listId.add(id1); |
| 140 | + listId.add(id2); |
| 141 | + List<Foo> listResult = ds.lookup(Foo.class).ids(listId).list(); |
| 142 | +``` |
| 143 | + |
| 144 | +- Query |
| 145 | + |
| 146 | +``` |
| 147 | + // Query with limit |
| 148 | + List<Foo> list = ds.query(Foo.class).limit(10).list(); |
| 149 | +``` |
| 150 | + |
| 151 | +``` |
| 152 | + // Query with offset |
| 153 | + List<Foo> list = ds.query(Foo.class).offset(10).list(); |
| 154 | +``` |
| 155 | + |
| 156 | +``` |
| 157 | + // Query with order : ASCENDING, DESCENDING |
| 158 | + List<Foo> listOrderByDocASC = ds.query(Foo.class).order("doc", OrderDirection.ASCENDING).list(); |
| 159 | + List<Foo> listOrderByDocDESC = ds.query(Foo.class).order("doc", OrderDirection.DESCENDING).list(); |
| 160 | +``` |
| 161 | + |
| 162 | +``` |
| 163 | + // Query with filter : EQUAL, GREATER_THAN, GREATER_THAN_OR_EQUAL, LESS_THAN, LESS_THAN_OR_EQUAL |
| 164 | + List<Foo> listFilterByStatus = ds.query(Foo.class).filter("status", 1, FilterOperator.EQUAL).list(); |
| 165 | + List<Foo> list = ds.query(Foo.class).filter("status", 1, FilterOperator.GREATER_THAN_OR_EQUAL).filter("status", 10, FilterOperator.LESS_THAN_OR_EQUAL).list(); |
| 166 | +``` |
| 167 | + |
| 168 | +``` |
| 169 | + // Composite query |
| 170 | + List<Foo> list = ds.query(Foo.class).filter("status", 1, FilterOperator.GREATER_THAN_OR_EQUAL).limit(10).offset(10).order("doc", OrderDirection.ASCENDING).list(); |
| 171 | +``` |
| 172 | + |
| 173 | +- GQL Query : query with your query string (GQL Syntax : https://cloud.google.com/appengine/docs/python/datastore/gqlreference) |
| 174 | + |
| 175 | +``` |
| 176 | + List<Foo> list = ds.gqlQuery(Foo.class).queryString("SELECT * FROM Foo").list(); |
| 177 | +``` |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
0 commit comments