Build java REST API with cassandra -
i trying build rest api cassandra database don't see documentation or examples online, using datastax java driver 3.0.0.
public class cassandradbutil { private cluster cluster; private poolingoptions options; private session session; public void connect(string node) { options = new poolingoptions(). setmaxrequestsperconnection(hostdistance.local, 32768); cluster = cluster.builder() .addcontactpoint(node) .withaddresstranslator(new ec2multiregionaddresstranslator()) .withpoolingoptions(options) .build(); metadata metadata = cluster.getmetadata(); system.out.printf("connected cluster: %s\n", metadata.getclustername()); (host host : metadata.getallhosts()) { system.out.printf("datacenter: %s; host: %s; rack: %s\n", host.getdatacenter(), host.getaddress(), host.getrack()); } session = cluster.connect("dropalletcassdb"); } public session getsession(){ if(this.cluster != null){ system.out.println("@@@@ returning old session"); return this.session; }else{ system.out.println("@@@ creating new session"); connect("127.0.0.1"); return this.session; } }
below function in class,
@get @path("/get") @produces(mediatype.application_json) public list<activebid> gettrackinjson() { logger.info("loginapi: returning active bid"); session session = cassdb.getsession(); string cqlstatement = "select * active_bid username='xyz'"; (row row : session.execute(cqlstatement)) { system.out.println(row.tostring()); } session.close(); activebid activebid = new activebid("3d673111-894d-4fcb-84f7-7d027a9a2419", "xxx-xxx-xxxxxxx", "/p/project_id=1367777", 25, new biginteger("125"), new biginteger("150"), "2016-06-04 18:32:37"); list<activebid> activebidlist = new arraylist<activebid>(); activebidlist.add(activebid); return activebidlist; }
in above code, have util class builds cluster , session , have gettrackinjson in class invokes when request made http://localhost:8080/cassexample/rest/get. every time invoke call /get, builds cluster , session , average response time 500ms. not sure doing wrong, not returning resultset response, returning mocked response , taking time. can please point me in right direction.
just put static class variables . following
private static cluster cluetr; private static session session; . . . create constructor of class , initialize class variables . create 1 method getsession . in first check if cluster , session variables initialized or not if not initialized create obeject of class default constructor initialize variables , send session variable.