Skip to content

Instantly share code, notes, and snippets.

@maxim5
Created January 30, 2017 16:59
Show Gist options
  • Save maxim5/956f24d9fdc9dfdc719ed10e55159769 to your computer and use it in GitHub Desktop.
Save maxim5/956f24d9fdc9dfdc719ed10e55159769 to your computer and use it in GitHub Desktop.
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import java.util.List;
public class Main {
public static void main(String[] args) {
try {
Session session = connect();
if (args.length > 0 && args[0].equals("insert")) {
insert(session);
} else {
select(session);
}
} finally {
}
}
private static Session connect() {
Cluster cluster;
Session session;
cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
session = cluster.connect();
System.out.println("Connected");
return session;
}
private static void insert(Session session) {
System.out.println("Inserting");
session.execute("drop keyspace if exists projects;");
session.execute("CREATE KEYSPACE projects WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'};");
session.execute("CREATE TABLE projects.reviews (\n" +
" projectid int,\n" +
" reviewid int,\n" +
" branches list<text>,\n" +
" closed boolean,\n" +
" closedat bigint,\n" +
" closedby text,\n" +
" createdat bigint,\n" +
" createdby text,\n" +
" deleted boolean,\n" +
" discussions list<text>,\n" +
" duration bigint,\n" +
" issues list<text>,\n" +
" participants map<text, blob>,\n" +
" revisions set<text>,\n" +
" revisionsinreview map<text, blob>,\n" +
" timestamp bigint,\n" +
" title text,\n" +
" users set<text>,\n" +
" PRIMARY KEY (projectid, reviewid)\n" +
") WITH CLUSTERING ORDER BY (reviewid DESC)\n" +
" AND bloom_filter_fp_chance = 0.1\n" +
" AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}\n" +
" AND comment = ''\n" +
" AND compaction = {'class': 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy'}\n" +
" AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}\n" +
" AND crc_check_chance = 1.0\n" +
" AND dclocal_read_repair_chance = 0.1\n" +
" AND default_time_to_live = 0\n" +
" AND gc_grace_seconds = 1\n" +
" AND max_index_interval = 2048\n" +
" AND memtable_flush_period_in_ms = 0\n" +
" AND min_index_interval = 128\n" +
" AND read_repair_chance = 0.0\n" +
" AND speculative_retry = '99PERCENTILE';\n");
session.execute("CREATE CUSTOM INDEX reviews_idx ON projects.reviews () USING 'com.stratio.cassandra.lucene.Index' WITH OPTIONS =\n" +
"{'refresh_seconds': '30', 'schema': '{\"analyzers\":{\"code\":{\"type\":\"classpath\",\"class\":\"com.jetbrains.upsource.lucene.CodeAnalyzer\"},\"filename\":{\"type\":\"classpath\",\"class\":\"com.jetbrains.upsource.lucene.FileNameAnalyzer\"}},\"fields\":{\"title\":{\"type\":\"text\",\"analyzer\":\"standard\"},\"branches\":{\"type\":\"string\",\"case_sensitive\":false},\"discussions\":{\"type\":\"string\",\"case_sensitive\":false},\"issues\":{\"type\":\"string\",\"case_sensitive\":false},\"revisions\":{\"type\":\"string\",\"case_sensitive\":false},\"users\":{\"type\":\"string\",\"case_sensitive\":false},\"createdat\":{\"type\":\"long\"},\"createdby\":{\"type\":\"string\",\"case_sensitive\":false},\"closed\":{\"type\":\"boolean\"},\"closedat\":{\"type\":\"long\"},\"closedby\":{\"type\":\"string\",\"case_sensitive\":false},\"duration\":{\"type\":\"long\"}}}'};");
for (int i = 0; i < 10; i++) {
/*
session.execute("insert into projects.reviews (projectid, reviewid, branches, closed, closedat, closedby, createdat, createdby, deleted, discussions, duration, issues, participants, revisions, revisionsinreview, timestamp, title, users) values\n" +
"(1, " + i + ", [], false, 0, 'closedby', 0, 'createdby',\n" +
"false, [], 0, [], {}, {'rev" + i + "'}, {}, 0, 'title', {});\n");
*/
Object[] values = new Object[18];
values[0] = i;
values[1] = 1485778518344L;
values[2] = "mention issue #2";
values[3] = 1485778518344L;
values[4] = "48591f21-0fab-4b09-9b8d-0f96603ce30e";
values[5] = false;
values[9] = false;
values[17] = 1;
session.execute("INSERT INTO projects.reviews (reviewId,timestamp,title,createdAt,createdBy,closed,closedAt,closedBy,duration,deleted,branches,discussions,issues,revisions,revisionsinreview,users,participants,projectId) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", values);
session.execute("UPDATE projects.reviews SET revisions=revisions+{'rev" + i + "'},users=users+{'maxim'},users=users+{'48591f21-0fab-4b09-9b8d-0f96603ce30e'},participants=participants+{'maxim':0x0200},participants=participants+{'48591f21-0fab-4b09-9b8d-0f96603ce30e':0x0400},timestamp=1485779427824,revisionsinreview=revisionsinreview+{'2316881798e520120a604eb07224ea774cb26413':0x002434383539316632312d306661622d346230392d396238642d30663936363033636533306501844a6b2543b5a31c98511bdc59010000314d6178696d2e506f646b6f6c7a696e65203c4d6178696d2e506f646b6f6c7a696e65406a6574627261696e732e636f6d3e} WHERE projectId=1 AND reviewId=" + i);
}
System.out.println("Inserted");
}
private static void select(Session session) {
System.out.println("Selecting");
for (int i = 0; i < 10; i++) {
ResultSet rs = session.execute("select revisions from projects.reviews where projectid=1 and expr(reviews_idx, '{filter:{type:\"match\",field:\"revisions\",value:\"rev" + i + "\"}}');");
List<Row> all = rs.all();
System.out.println(all.size());
assert all.size() == 1;
}
System.out.println("Done selecting");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment