· 9 years ago · Dec 26, 2016, 06:34 PM
1CREATE KEYSPACE IF NOT EXISTS activitylogs WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
2
3CREATE TABLE IF NOT EXISTS activitylogs.activities (
4 activity_id timeuuid,
5 actor_id text,
6 app_id text,
7 item_id text,
8 viewer_id text,
9 activity_type int,
10 ts timestamp,
11 PRIMARY KEY (actor_id, activity_id, app_id)
12 ) WITH CLUSTERING ORDER BY (activity_id DESC, app_id ASC);
13
14INSERT INTO activities (activity_id,actor_id, app_id, item_id, viewer_id, activity_type) VALUES ( now(), 'fsdgs346-sdsd5-4242','blossom','ff235-fsd54-fadsfdfs45','hj923hjn-2jnkl23-323yfh',0);
15
16group 'com.abc'
17version '1.0-SNAPSHOT'
18
19apply plugin: 'java'
20apply plugin: 'eclipse'
21apply plugin: 'idea'
22apply plugin: 'spring-boot'
23
24buildscript {
25 repositories {
26 mavenCentral()
27 }
28 dependencies {
29 classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
30 }
31}
32
33jar {
34 baseName = 'gs-serving-web-content'
35 version = '0.1.0'
36}
37
38repositories {
39 mavenCentral()
40}
41
42
43sourceCompatibility = 1.8
44
45repositories {
46 mavenCentral()
47}
48
49
50dependencies {
51 compile "org.springframework.boot:spring-boot-starter-web"
52 compile "org.springframework.data:spring-data-cassandra:1.4.6.RELEASE"
53 compile 'org.slf4j:slf4j-api:1.6.6'
54 compile 'ch.qos.logback:logback-classic:1.0.13'
55 testCompile "junit:junit"
56}
57
58task wrapper(type: Wrapper) {
59 gradleVersion = '2.3'
60}
61
62package com.abc.activitystream.entity;
63
64import org.springframework.data.cassandra.mapping.Column;
65import org.springframework.data.cassandra.mapping.PrimaryKey;
66import org.springframework.data.cassandra.mapping.Table;
67
68import java.security.Timestamp;
69
70@Table(value = "activities")
71public class Activity
72{
73
74 /*CREATE TABLE IF NOT EXISTS activitylogs.activities (
75 activity_id timeuuid,
76 actor_id text,
77 app_id text,
78 item_id text,
79 viewer_id text,
80 activity_type int,
81 ts timestamp,
82 PRIMARY KEY (actor_id, activity_id, app_id)) WITH CLUSTERING ORDER BY (activity_id DESC);*/
83
84 @PrimaryKey
85 private ActivityKey ak;
86
87 @Column(value = "item_id")
88 private String item_id;
89
90 @Column(value = "viewer_id")
91 private String viewer_id;
92
93 @Column(value = "activity_type")
94 private int activity_type;
95
96 @Column(value = "ts")
97 private Timestamp ts;
98
99 public String getItem_id() {
100 return item_id;
101 }
102
103 public void setItem_id(String item_id) {
104 this.item_id = item_id;
105 }
106
107 public String getViewer_id() {
108 return viewer_id;
109 }
110
111 public void setViewer_id(String viewer_id) {
112 this.viewer_id = viewer_id;
113 }
114
115 public int getActivity_type() {
116 return activity_type;
117 }
118
119 public void setActivity_type(int activity_type) {
120 this.activity_type = activity_type;
121 }
122
123 public Timestamp getTs() {
124 return ts;
125 }
126
127 public void setTs(Timestamp ts) {
128 this.ts = ts;
129 }
130
131 public ActivityKey getAk() {
132 return ak;
133 }
134
135 public void setAk(ActivityKey ak) {
136 this.ak = ak;
137 }
138}
139
140@PrimaryKeyClass
141public class ActivityKey implements Serializable {
142
143 @PrimaryKeyColumn(name = "actor_id",ordinal = 0,type = PrimaryKeyType.PARTITIONED)
144 private String actor_id;
145
146 @PrimaryKeyColumn(name="activity_id",ordinal = 1,type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING )
147 private UUID activity_id = UUIDs.timeBased();
148
149 @PrimaryKeyColumn(name="app_id", ordinal = 2, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.ASCENDING)
150 private String app_id;
151
152 public String getActor_id() {
153 return actor_id;
154 }
155
156 public void setActor_id(String actor_id) {
157 this.actor_id = actor_id;
158 }
159
160 public UUID getActivity_id() {
161 return activity_id;
162 }
163
164 public void setActivity_id(UUID activity_id) {
165 this.activity_id = activity_id;
166 }
167
168 public String getApp_id() {
169 return app_id;
170 }
171
172 public void setApp_id(String app_id) {
173 this.app_id = app_id;
174 }
175
176 @Override
177 public int hashCode() {
178 final int prime = 31;
179 int result = 1;
180 result = prime * result + ((actor_id == null) ? 0 : actor_id.hashCode());
181 result = prime * result + ((app_id == null) ? 0 : app_id.hashCode());
182 return result;
183 }
184
185 @Override
186 public boolean equals(Object obj) {
187 if (this == obj)
188 return true;
189 if (obj == null)
190 return false;
191 if (getClass() != obj.getClass())
192 return false;
193 ActivityKey other = (ActivityKey) obj;
194 if (actor_id == null) {
195 if (other.actor_id != null)
196 return false;
197 } else if (!actor_id.equals(other.actor_id))
198 return false;
199 if (app_id == null) {
200 if (other.app_id != null)
201 return false;
202 } else if (!app_id.equals(other.app_id))
203 return false;
204 return true;
205 }
206
207}
208
209@RestController
210public class ActivityController {
211
212 @Autowired
213 private ActivityRepository activityRepository;
214
215 @RequestMapping(value = "/activity",method = RequestMethod.GET)
216 @ResponseBody
217 public List<Activity> activity() {
218 List<Activity> activities = new ArrayList<>();
219 activityRepository.findAll().forEach(e->activities.add(e));
220 return activities;
221 }
222
223public interface ActivityRepository extends CassandraRepository<Activity> {
224
225 @Query("SELECT*FROM activities WHERE actor_id=?0 LIMIT ?1")
226 Iterable<Activity> findByActor_Id(String actor_id,Integer limit);
227
228}
229
230@Configuration
231//@PropertySource(value = {"classpath:META-INF/cassandra.properties"})
232@EnableCassandraRepositories(basePackages = {"com.abc"})
233public class CassandraConfig {
234
235 @Autowired
236 private Environment environment;
237
238 private static final Logger LOGGER = LoggerFactory.getLogger(CassandraConfig.class);
239
240 @Bean
241 public CassandraClusterFactoryBean cluster() {
242
243 CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
244 cluster.setContactPoints("localhost");
245 cluster.setPort(Integer.parseInt("9042"));
246 return cluster;
247 }
248
249 @Bean
250 public CassandraMappingContext mappingContext() {
251 return new BasicCassandraMappingContext();
252 }
253
254 @Bean
255 public CassandraConverter converter() {
256 return new MappingCassandraConverter(mappingContext());
257 }
258
259 @Bean
260 public CassandraSessionFactoryBean session() throws Exception {
261
262 CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
263 session.setCluster(cluster().getObject());
264 session.setKeyspaceName("activitylogs");
265 session.setConverter(converter());
266 session.setSchemaAction(SchemaAction.NONE);
267
268 return session;
269 }
270
271 @Bean
272 public CassandraOperations cassandraTemplate() throws Exception {
273 return new CassandraTemplate(session().getObject());
274 }
275
276}