forked from aftersunday/gcd-json-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.mkd~
More file actions
128 lines (96 loc) · 3.57 KB
/
README.mkd~
File metadata and controls
128 lines (96 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# GCD Json Java
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.
## To use library :
- You need an appengine application (https://console.developers.google.com/project) with Google Cloud Datastore API status is On.
- Create your own project. Visit https://console.developers.google.com > Create Project, enter project name and project id.
- 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.
- Some information to config. Choose your created project > APIs & auth (left menu) > Credentials > Create New Client ID > Chooose Service account > Create Client ID.
- projectName : is your project id with "s~" before, example your project id : "your-project-id" -> projectName = "s~your-project-id".
- iss : is Email address.
- p12 key file : click Generate new P12 key, download, keyLocation variable is path to your p12 Key file.
## Sample code :
- Config
```
String projectName = "s~your-project-id";
String iss = "xxxx20893014-d1kh9putd2n3hbqjsjlsbes1i8spxxx@developer.gserviceaccount.com";
String keyLocation = "your-project-id-xxxxx.p12";
GCDConfig config = new GCDConfig(projectName, iss, keyLocation);
GCDService ds = GCDServiceFactory.getInstance(config);
```
- Registry your entry
- Mark annotation @Entity for your entry.
- Mark annotation @Id for entry key.
- Mark annotation @Index for index field.
```
import cloud.google.datastore.annotation.Annotation.Entity;
import cloud.google.datastore.annotation.Annotation.Id;
import cloud.google.datastore.annotation.Annotation.Index;
@Entity
public class Foo {
@Id
private String id = "";
private String name = "";
@Index
private String category = "";
@Index
private Date doc = Calendar.getInstance().getTime();
@Index
private int status = 1;
}
```
- Commit
```
// Insert single entry
String projectName = "s~your-project-id";
String iss = "xxxx20893014-d1kh9putd2n3hbqjsjlsbes1i8spxxx@developer.gserviceaccount.com";
String keyLocation = "your-project-id-xxxxx.p12";
GCDConfig config = new GCDConfig(projectName, iss, keyLocation);
GCDService ds = GCDServiceFactory.getInstance(config);
Foo f = new Foo();
f.setId("entry-01");
f.setName("Entry 01");
List<Key<Foo>> listKey = ds.commit(Foo.class).entities(f).insert();
```
```
// Insert multi entry
Foo f1 = new Foo();
f1.setId("entry-01");
f1.setName("Entry 01");
Foo f2 = new Foo();
f2.setId("entry-02");
f2.setName("Entry 02");
List<Key<Foo>> listKey = ds.commit(Foo.class).entities(f1, f2).insert();
```
```
// Insert list entry
Foo f1 = new Foo();
f1.setId("entry-01");
f1.setName("Entry 01");
Foo f2 = new Foo();
f2.setId("entry-02");
f2.setName("Entry 02");
List<Foo> list = new ArrayList<Foo>();
list.add(f1);
list.add(f2);
List<Key<Foo>> listKey = ds.commit(Foo.class).entities(list).insert();
```
```
// Update single entry
Foo f = new Foo();
f.setId("entry-01");
f.setName("Entry 01");
List<Key<Foo>> listKey = ds.commit(Foo.class).entities(f).update();
```
```
// Delete single entry
Foo f = new Foo();
f.setId("entry-01");
f.setName("Entry 01");
boolean success = ds.commit(Foo.class).entities(f).delete();
```
- Lookup
```
// Lookup single entry
String id = "entry-01";
Foo lookupFoo = ds.lookup(Foo.class).id(f.getId()).get();
```