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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
/*
* Copyright 2013 Jesse Morgan
*/
package com.p4square.grow.backend.db;
import com.netflix.astyanax.AstyanaxContext;
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolConfigurationImpl;
import com.netflix.astyanax.connectionpool.impl.CountingConnectionPoolMonitor;
import com.netflix.astyanax.connectionpool.NodeDiscoveryType;
import com.netflix.astyanax.connectionpool.OperationResult;
import com.netflix.astyanax.impl.AstyanaxConfigurationImpl;
import com.netflix.astyanax.Keyspace;
import com.netflix.astyanax.ColumnMutation;
import com.netflix.astyanax.model.Column;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.model.ColumnList;
import com.netflix.astyanax.MutationBatch;
import com.netflix.astyanax.serializers.StringSerializer;
import com.netflix.astyanax.thrift.ThriftFamilyFactory;
import org.apache.log4j.Logger;
/**
* Cassandra Database Abstraction for the Backend.
*
* @author Jesse Morgan <jesse@jesterpm.net>
*/
public class CassandraDatabase {
private static Logger cLog = Logger.getLogger(CassandraDatabase.class);
// Configuration fields.
private String mClusterName;
private String mKeyspaceName;
private String mSeedEndpoint = "127.0.0.1:9160";
private int mPort = 9160;
private AstyanaxContext<Keyspace> mContext;
private Keyspace mKeyspace;
/**
* Connect to Cassandra.
*
* Cluster and Keyspace must be set before calling init().
*/
public void init() {
mContext = new AstyanaxContext.Builder()
.forCluster(mClusterName)
.forKeyspace(mKeyspaceName)
.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
)
.withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
.setPort(mPort)
.setMaxConnsPerHost(1)
.setSeeds(mSeedEndpoint)
)
.withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
.buildKeyspace(ThriftFamilyFactory.getInstance());
mContext.start();
mKeyspace = mContext.getClient();
}
/**
* Close the database connection.
*/
public void close() {
mContext.shutdown();
}
/**
* Set the cluster name to connect to.
*/
public void setClusterName(final String cluster) {
mClusterName = cluster;
}
/**
* Set the name of the keyspace to open.
*/
public void setKeyspaceName(final String keyspace) {
mKeyspaceName = keyspace;
}
/**
* Change the seed endpoint.
* The default is 127.0.0.1:9160.
*/
public void setSeedEndpoint(final String endpoint) {
mSeedEndpoint = endpoint;
}
/**
* Change the port to connect to.
* The default is 9160.
*/
public void setPort(final int port) {
mPort = port;
}
/**
* @return The entire row associated with this key.
*/
public ColumnList<String> getRow(final String cfName, final String key) {
try {
ColumnFamily<String, String> cf = new ColumnFamily(cfName,
StringSerializer.get(),
StringSerializer.get());
OperationResult<ColumnList<String>> result =
mKeyspace.prepareQuery(cf)
.getKey(key)
.execute();
return result.getResult();
} catch (ConnectionException e) {
cLog.error("getRow failed due to Connection Exception", e);
throw new RuntimeException(e);
}
}
/**
* @return The value associated with the given key.
*/
public String getKey(final String cfName, final String key) {
return getKey(cfName, key, "value");
}
/**
* @return The value associated with the given key, column pair.
*/
public String getKey(final String cfName, final String key, final String column) {
final ColumnList<String> row = getRow(cfName, key);
if (row != null) {
final Column rowColumn = row.getColumnByName(column);
if (rowColumn != null) {
return rowColumn.getStringValue();
}
}
return null;
}
/**
* Assign value to key.
*/
public void putKey(final String cfName, final String key, final String value) {
putKey(cfName, key, "value", value);
}
/**
* Assign value to the key, column pair.
*/
public void putKey(final String cfName, final String key,
final String column, final String value) {
ColumnFamily<String, String> cf = new ColumnFamily(cfName,
StringSerializer.get(),
StringSerializer.get());
MutationBatch m = mKeyspace.prepareMutationBatch();
m.withRow(cf, key).putColumn(column, value);
try {
m.execute();
} catch (ConnectionException e) {
cLog.error("putKey failed due to Connection Exception", e);
throw new RuntimeException(e);
}
}
/**
* Remove a key, column pair.
*/
public void deleteKey(final String cfName, final String key, final String column) {
ColumnFamily<String, String> cf = new ColumnFamily(cfName,
StringSerializer.get(),
StringSerializer.get());
try {
ColumnMutation m = mKeyspace.prepareColumnMutation(cf, key, column);
m.deleteColumn().execute();
} catch (ConnectionException e) {
cLog.error("deleteKey failed due to Connection Exception", e);
throw new RuntimeException(e);
}
}
}
|