Skip to content

Commit 996b211

Browse files
committed
Merge pull request #4 from aftersunday/master
new readme
2 parents a54e287 + d650d8b commit 996b211

8 files changed

Lines changed: 754 additions & 6 deletions

File tree

README.mkd

Lines changed: 181 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,181 @@
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+

README.mkd~

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+

lib/commons-codec-1.9.jar

258 KB
Binary file not shown.

lib/gson-2.2.4.jar

186 KB
Binary file not shown.

src/main/java/cloud/google/datastore/GCDConfig.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class GCDConfig {
3333
private String projectName = "";
3434
private String iss = "";
3535
private String keystoreLoc = "";
36+
private String accessToken = "";
3637

3738
/**
3839
* <p>
@@ -62,6 +63,20 @@ public GCDConfig(String projectName, String iss, String keystoreLoc) {
6263
this.keystoreLoc = keystoreLoc;
6364
}
6465

66+
public GCDConfig(String projectName, String accessToken) {
67+
super();
68+
this.projectName = projectName;
69+
this.accessToken = accessToken;
70+
}
71+
72+
public String getAccessToken() {
73+
return accessToken;
74+
}
75+
76+
public void setAccessToken(String accessToken) {
77+
this.accessToken = accessToken;
78+
}
79+
6580
public String getProjectName() {
6681
return projectName;
6782
}

src/test/org/google/datastore/test/CommitBasicTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.google.datastore.test;
1818

1919
import java.util.ArrayList;
20+
import java.util.Calendar;
2021
import java.util.List;
2122

2223
import org.google.datastore.test.entity.Foo;
@@ -63,18 +64,20 @@ public class CommitBasicTest {
6364
@Test
6465
public void testInsertOne() {
6566
Foo f = new Foo();
66-
f.setId("this-is-id");
67-
f.setName("This is Name");
68-
69-
// remove enity if exists
67+
f.setId("this-is-id-01");
68+
f.setName("This is Name 01");
69+
f.setIndexString("category-01");
70+
f.setIndexInt(1);
71+
Calendar cal = Calendar.getInstance();
72+
cal.set(2012, 07, 17);
73+
f.setDoc(cal.getTime());
7074
ds.commit(Foo.class).entities(f).delete();
7175

7276
// start insert.
7377
List<Key<Foo>> listKey = ds.commit(Foo.class).entities(f).insert();
7478

7579
assertEquals(listKey.size(), 1);
7680
assertEquals(listKey.get(0).getPath().get(0).getName(), f.getId());
77-
7881
Foo lookupFoo = ds.lookup(Foo.class).id(f.getId()).get();
7982
assertEquals(lookupFoo.getId(), f.getId());
8083
}

0 commit comments

Comments
 (0)