{"id":2072,"date":"2015-07-14T16:37:22","date_gmt":"2015-07-14T16:37:21","guid":{"rendered":"https:\/\/www.couchbase.com\/blog\/?p=2072"},"modified":"2025-06-13T23:47:50","modified_gmt":"2025-06-14T06:47:50","slug":"traveling-with-couchbase-using-the-java-sdk","status":"publish","type":"post","link":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/","title":{"rendered":"Traveling with Couchbase using the Java SDK"},"content":{"rendered":"<p>At Couchbase Connect 2015 we demonstrated an example application that uses N1QL to query data from a sample Couchbase bucket.<\/p>\n<p>If you missed the conference, not a problem. We&#39;re going to go through how to reproduce this application and check out some of the highlights of Couchbase 4.0.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Apache Maven 3<\/li>\n<li>Java Development Kit (JDK) 1.7<\/li>\n<li>Couchbase Server 4.0<\/li>\n<li>IntelliJ IDEA 14.1+, Eclipse, or NetBeans. IntelliJ IDEA will be used in this example.<\/li>\n<\/ul>\n<h2>Creating a New Project<\/h2>\n<p>Open IntelliJ IDEA and choose to create a new Java project, making sure to use JDK 1.7 if asked. For purposes of this guide, let&#39;s call the project <strong>try-cb-java<\/strong>.<\/p>\n<p>Now right-click <strong>try-cb-java<\/strong> in your project tree, then choose <strong>Add Frameworks Support<\/strong> and select <strong>Maven<\/strong>. This will add a <strong>pom.xml<\/strong> file to your project.<\/p>\n<h2>Setting up Maven<\/h2>\n<p>Inside the <strong>pom.xml<\/strong> file, start by giving the project a more appealing group name:<\/p>\n<pre>\n<code class=\"language-xml\">\n<groupId>com.couchbase.example<\/groupId>\n<\/code>\n<\/pre>\n<p>Then proceed to adding the rest of our dependencies to the file, that include Spring Boot, the Couchbase client, and the Spring security framework.<\/p>\n<pre>\n<code class=\"language-xml\">\n<parent>\n    <groupId>org.springframework.boot<\/groupId>\n    <artifactId>spring-boot-starter-parent<\/artifactId>\n    <version>1.2.3.RELEASE<\/version>\n<\/parent>\n\n<dependencies>\n    <dependency>\n        <groupId>org.springframework.boot<\/groupId>\n        <artifactId>spring-boot-starter-web<\/artifactId>\n    <\/dependency>\n    <dependency>\n        <groupId>org.springframework<\/groupId>\n        <artifactId>spring-tx<\/artifactId>\n    <\/dependency>\n    <dependency>\n        <groupId>org.springframework.security<\/groupId>\n        <artifactId>spring-security-core<\/artifactId>\n    <\/dependency>\n    <dependency>\n        <groupId>com.couchbase.client<\/groupId>\n        <artifactId>java-client<\/artifactId>\n        <version>2.2.0-dp<\/version>\n    <\/dependency>\n<\/dependencies>\n\n<repositories>\n    <repository>\n        <id>couchbase<\/id>\n        <name>couchbase repo<\/name>\n        <url>https:\/\/files.couchbase.com\/maven2<\/url>\n        <snapshots><enabled>false<\/enabled><\/snapshots>\n    <\/repository>\n<\/repositories>\n\n<build>\n    <plugins>\n        <plugin>\n            <groupId>org.springframework.boot<\/groupId>\n            <artifactId>spring-boot-maven-plugin<\/artifactId>\n        <\/plugin>\n    <\/plugins>\n<\/build>\n<\/code>\n<\/pre>\n<h2>Creating a Run Profile<\/h2>\n<p>As of right now, if you try to run the application, it will error or nothing will happen because there is no profile currently configured.<\/p>\n<p>From the toolbar, choose <strong>Run -> Edit Configurations<\/strong> and choose to add a new Maven configuration. You can name it whatever you want, but it is important have the following in the command line field:<\/p>\n<pre>\n<code class=\"language-bash\">\nspring-boot:run\n<\/code>\n<\/pre>\n<p>For this article we&#39;re going to name the configuration <strong>Spring Boot<\/strong>.<\/p>\n<p>IntelliJ IDEA should now be ready for development.<\/p>\n<h2>Creating a Indices on the Couchbase Bucket<\/h2>\n<p>Because this tutorial uses N1QL queries, we must first add indices to our Couchbase Server bucket. Now this can easily be done through code, but for this example we&#39;re going to shortcut it and add them via the Couchbase Query (CBQ) client that gets automatically installed with a Mac OS and Windows installation of Couchbase 4+.<\/p>\n<p>On Mac OS, launch CBQ found at <strong>\/Applications\/Couchbase Server.app\/Contents\/Resources\/couchbase-core\/bin\/cbq<\/strong> and run the following:<\/p>\n<pre>\n<code class=\"language-sql\">\nCREATE PRIMARY INDEX def_primary ON `travel-sample` USING gsi;\n<\/code>\n<\/pre>\n<p>On Windows, launch CBQ found at <strong>C:\/Program Files\/Couchbase\/Server\/bin\/cbq.exe<\/strong> and execute the same N1QL command as done on Mac OS.<\/p>\n<h2>Creating a Main Application Class<\/h2>\n<p>The main class for this project will be <strong>Application.java<\/strong> and can be created by right-clicking the <strong>trycb<\/strong> package from the project tree and choosing <strong>New -> Java Class<\/strong>.<\/p>\n<p>Add the following to get the class in its most basic runnable state:<\/p>\n<pre>\n<code class=\"language-java\">\npackage trycb;\n\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\n@SpringBootApplication\n@RestController\n@RequestMapping(\"\/api\")\npublic class Application {\n\n    public static void main(String[] args) {\n        SpringApplication.run(Application.class, args);\n    }\n\n}\n<\/code>\n<\/pre>\n<p>Make sure the project runs without errors by choosing <strong>Run -> Run &#39;Spring Boot&#39;<\/strong> from the IntelliJ IDEA toolbar.<\/p>\n<h3>Handling Cross Origin Resource Sharing (CORS)<\/h3>\n<p>Since most of our testing will be done locally, we need to make sure CORS is enabled, otherwise the web browser is going to complain when trying to hit our API endpoints with JavaScript.<\/p>\n<p>Make sure the Application class implements the Filter class and add the following code:<\/p>\n<pre>\n<code class=\"language-java\">\n@Override\npublic void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)\n        throws IOException, ServletException {\n    HttpServletResponse response = (HttpServletResponse) res;\n    response.setHeader(\"Access-Control-Allow-Origin\", \"*\");\n    response.setHeader(\"Access-Control-Allow-Headers\", \"Origin, X-Requested-With, Content-Type, Accept\");\n    chain.doFilter(req, res);\n}\n\n@Override\npublic void init(FilterConfig filterConfig) throws ServletException {}\n\n@Override\npublic void destroy() {}\n<\/code>\n<\/pre>\n<h3>Configure Couchbase Cluster and Bucket Options<\/h3>\n<p>As of right now we essentially have a Spring Boot application with no Couchbase interation. We&#39;ve included the Java client via Maven, so it is time to start using it.<\/p>\n<p>Add the following to the Application class:<\/p>\n<pre>\n<code class=\"language-java\">\n@Value(\"${hostname}\")\nprivate String hostname;\n\n@Value(\"${bucket}\")\nprivate String bucket;\n\n@Value(\"${password}\")\nprivate String password;\n\npublic @Bean Cluster cluster() {\n   return CouchbaseCluster.create(hostname);\n}\n\npublic @Bean Bucket bucket() {\n   return cluster().openBucket(bucket, password);\n}\n<\/code>\n<\/pre>\n<p>We&#39;ve not set up the <strong>hostname<\/strong>, <strong>bucket<\/strong>, and <strong>password<\/strong> variables, but they will be used to connect to a Couchbase cluster and a particular bucket.<\/p>\n<h4>Add Resource Variables<\/h4>\n<p>You saw that we were using <strong>hostname<\/strong>, <strong>bucket<\/strong>, and <strong>password<\/strong>, so now it is time to set them.<\/p>\n<p>In the IntelliJ IDEA project tree, right-click <strong>src\/main\/resources<\/strong> and choose <strong>New -> File<\/strong>. Name the new file <strong>application.properties<\/strong> and add the following lines:<\/p>\n<pre>\nhostname=127.0.0.1\nbucket=travel-sample\npassword=<\/pre>\n<p>Spring Boot will pick up this <strong>application.properties<\/strong> file for you. Further information on application related properties can be seen in the <a href=\"https:\/\/docs.spring.io\/spring-boot\/docs\/current\/reference\/html\/boot-features-external-config.html\">official Spring documentation<\/a>.<\/p>\n<h3>Creating RESTful Endpoints<\/h3>\n<p>This application is going to be API based so certain endpoints need to be created:<\/p>\n<pre>\n<code class=\"language-java\">\n@RequestMapping(value=\"\/airport\/findAll\", method=RequestMethod.GET)\npublic List<Map<String, Object>> airports(@RequestParam String search) { }\n\n@RequestMapping(value=\"\/flightPath\/findAll\", method=RequestMethod.GET)\npublic List<Map<String, Object>> all(@RequestParam String from, @RequestParam String to, @RequestParam String leave) throws Exception { }\n\n@RequestMapping(value=\"\/user\/login\", method=RequestMethod.GET)\npublic Object login(@RequestParam String user, @RequestParam String password) { }\n\n@RequestMapping(value=\"\/user\/login\", method=RequestMethod.POST)\npublic Object createLogin(@RequestBody String json) { }\n\n@RequestMapping(value=\"\/user\/flights\", method=RequestMethod.POST)\npublic Object book(@RequestBody String json) { }\n\n@RequestMapping(value=\"\/user\/flights\", method=RequestMethod.GET)\npublic Object booked(@RequestParam String username) { }\n<\/code>\n<\/pre>\n<p>Essentially we&#39;ll have endpoints for user registration and sign-ins, booking and finding flights, as well as searching flight information.<\/p>\n<p>The logic behind these endpoints will appear in another class for cleanliness.<\/p>\n<h2>Creating a Database Class<\/h2>\n<p>We just set up the driving endpoints of our Spring Boot application, but it is now time to take a look at the logic behind interacting with the database.<\/p>\n<p>The database class for this project will be <strong>Database.java<\/strong> and can be created by right-clicking the <strong>trycb<\/strong> package from the project tree of IntelliJ IDEA and choosing <strong>New -> Java Class<\/strong>.<\/p>\n<p>Add the following to get the class for a nice skeleton of where we&#39;re going:<\/p>\n<pre>\n<code class=\"language-java\">\npackage trycb;\n\npublic class Database {\n\n    private Database() { }\n\n    public static List<Map<String, Object>> findAllAirports(final Bucket bucket, final String params) { }\n\n    public static List<Map<String, Object>> findAllFlightPaths(final Bucket bucket, String from, String to, Calendar leave) { }\n\n    public static ResponseEntity<String> login(final Bucket bucket, final String username, final String password) { }\n\n    public static ResponseEntity<String> createLogin(final Bucket bucket, final String username, final String password) { }\n\n    private static List<Map<String, Object>> extractResultOrThrow(QueryResult result) { }\n\n}\n<\/code>\n<\/pre>\n<p>From here, we&#39;re going to complete each of these methods in the order they are likely to be interacted with by the user.<\/p>\n<h3>Creating a New User<\/h3>\n<p>When the user issues a POST to the <strong>\/api\/user\/login<\/strong>, the following database function must be called:<\/p>\n<pre>\n<code class=\"language-java\">\npublic static ResponseEntity<String> createLogin(final Bucket bucket, final String username, final String password) {\n    JsonObject data = JsonObject.create()\n        .put(\"type\", \"user\")\n        .put(\"name\", username)\n        .put(\"password\", BCrypt.hashpw(password, BCrypt.gensalt()));\n    JsonDocument doc = JsonDocument.create(\"user::\" + username, data);\n\n    try {\n        bucket.insert(doc);\n        JsonObject responseData = JsonObject.create()\n            .put(\"success\", true)\n            .put(\"data\", data);\n        return new ResponseEntity<String>(responseData.toString(), HttpStatus.OK);\n    } catch (Exception e) {\n        JsonObject responseData = JsonObject.empty()\n            .put(\"success\", false)\n            .put(\"failure\", \"There was an error creating account\")\n            .put(\"exception\", e.getMessage());\n        return new ResponseEntity<String>(responseData.toString(), HttpStatus.OK);\n    }\n}\n<\/code>\n<\/pre>\n<p>The username and password included with the request will be added to a JSON object and then the password will be encrypted with the Spring BCrypt library. To keep track of the user data, new users will end up in documents titled <strong>user::{USERNAME_HERE}<\/strong>. Using <strong>bucket.insert(doc)<\/strong>, an attempt to insert the data into the bucket is made. If there are no exceptions thrown, then it succeeded and a response is returned. If there is an exception, then inserting failed and the error will be returned.<\/p>\n<h3>Signing in as an Existing User<\/h3>\n<p>When the user issues a GET to the same <strong>\/api\/user\/login<\/strong> endpoint, the following database function must be called:<\/p>\n<pre>\n<code class=\"language-java\">\npublic static ResponseEntity<String> login(final Bucket bucket, final String username, final String password) {\n   JsonDocument doc = bucket.get(\"user::\" + username);\n   JsonObject responseContent;\n   if(BCrypt.checkpw(password, doc.content().getString(\"password\"))) {\n       responseContent = JsonObject.create().put(\"success\", true).put(\"data\", doc.content());\n   } else {\n       responseContent = JsonObject.empty().put(\"success\", false).put(\"failure\", \"Bad Username or Password\");\n   }\n   return new ResponseEntity<String>(responseContent.toString(), HttpStatus.OK);\n}\n<\/code>\n<\/pre>\n<p>Using <strong>bucket.get(&#8220;user::&#8221; + username)<\/strong> with the provided username, the Java application gets the document from the bucket if it exists. The Spring BCrypt library has a great function to check whethor or not the provided password matches the encrypted password that is stored. If it does, then return a success object, otherwise return a login failed object.<\/p>\n<h3>Extracting the N1QL Result and Making it Java Readable<\/h3>\n<p>N1QL returns a <strong>QueryResult<\/strong> object that may be less desirable if returning data to a requesting front-end. What we really want to do is convert it into a <strong>List<\/strong> object.<\/p>\n<pre>\n<code class=\"language-java\">\nprivate static List<Map<String, Object>> extractResultOrThrow(QueryResult result) {\n    if (!result.finalSuccess()) {\n        throw new DataRetrievalFailureException(\"Query error: \" + result.errors());\n    }\n    List<Map<String, Object>> content = new ArrayList<Map<String, Object>>();\n    for (QueryRow row : result) {\n        content.add(row.value().toMap());\n    }\n    return content;\n}\n<\/code>\n<\/pre>\n<p>This function will be called every time N1QL data is returned.<\/p>\n<h3>Finding All Airports<\/h3>\n<p>Now we&#39;re going to see some of the magic behind N1QL when it comes to searching for airports.<\/p>\n<pre>\n<code class=\"language-java\">\npublic static List<Map<String, Object>> findAllAirports(final Bucket bucket, final String params) {\n    Statement query;\n\n    AsPath prefix = select(\"airportname\").from(i(bucket.name()));\n    if (params.length() == 3) {\n        query = prefix.where(x(\"faa\").eq(s(params.toUpperCase())));\n    } else if (params.length() == 4 && (params.equals(params.toUpperCase()) || params.equals(params.toLowerCase()))) {\n        query = prefix.where(x(\"icao\").eq(s(params.toUpperCase())));\n    } else {\n        query = prefix.where(i(\"airportname\").like(s(params + \"%\")));\n    }\n\n    QueryResult result = bucket.query(Query.simple(query));\n    return extractResultOrThrow(result);\n}\n<\/code>\n<\/pre>\n<p>You can see in the above code we are using the Fluent API with IntelliJ IDEA to create our N1QL query. Essentially, if you were to look at raw SQL, it would look like this:<\/p>\n<pre>\n<code class=\"language-sql\">\nSELECT airportname FROM `travel-sample` WHERE faa = {{PARAMS}}\n<\/code>\n<\/pre>\n<p>In the above, {{PARAMS}} represents an airport like LAX or similar. Of course that is provided the params length is three.<\/p>\n<h3>Finding All Flight Routes<\/h3>\n<p>Finally, we&#39;re left with the method responsible for finding flight routes:<\/p>\n<pre>\n<code class=\"language-java\">\npublic static List<Map<String, Object>> findAllFlightPaths(final Bucket bucket, String from, String to, Calendar leave) {\n    Statement query = select(x(\"faa\").as(\"fromAirport\"))\n        .from(i(bucket.name()))\n        .where(x(\"airportname\").eq(s(from)))\n        .union()\n        .select(x(\"faa\").as(\"toAirport\"))\n        .from(i(bucket.name()))\n        .where(x(\"airportname\").eq(s(to)));\n\n    QueryResult result = bucket.query(Query.simple(query));\n\n    if (!result.finalSuccess()) {\n        throw new DataRetrievalFailureException(\"Query error: \" + result.errors());\n    }\n\n    String fromAirport = null;\n    String toAirport = null;\n    for (QueryRow row : result) {\n        if (row.value().containsKey(\"fromAirport\")) {\n            fromAirport = row.value().getString(\"fromAirport\");\n        }\n        if (row.value().containsKey(\"toAirport\")) {\n            toAirport = row.value().getString(\"toAirport\");\n        }\n    }\n\n    Statement joinQuery = select(\"a.name\", \"s.flight\", \"s.utc\", \"r.sourceairport\", \"r.destinationairport\", \"r.equipment\")\n            .from(i(bucket.name()).as(\"r\"))\n            .unnest(\"r.schedule AS s\")\n            .join(i(bucket.name()).as(\"a\") + \" ON KEYS r.airlineid\")\n            .where(x(\"r.sourceairport\").eq(s(fromAirport)).and(x(\"r.destinationairport\").eq(s(toAirport))).and(x(\"s.day\").eq(leave.get(Calendar.DAY_OF_MONTH))))\n            .orderBy(Sort.asc(\"a.name\"));\n\n    QueryResult otherResult = bucket.query(joinQuery);\n    return extractResultOrThrow(otherResult);\n}\n<\/code>\n<\/pre>\n<p>We&#39;re doing two N1QL queries in this method. The first can easily be translated to the following:<\/p>\n<pre>\n<code class=\"language-sql\">\nSELECT faa AS fromAirport FROM `travel-sample` WHERE airportname = {{PARAMS.FROM}} UNION SELECT faa AS toAirport FROM `travel-sample` WHERE airportname = {{PARAMS.TO}}\n<\/code>\n<\/pre>\n<p>Of course {{PARAMS}} is whatever was passed to your endpoint. In the statement, we&#39;re combining the result sets of all the <strong>from<\/strong> airports and all the <strong>to<\/strong> airports.<\/p>\n<p>After getting both the result sets, we are looping through them to make sure the <strong>to<\/strong> and <strong>from<\/strong> airports exist, otherwise we&#39;re defaulting them to NULL which will prevent the next query from being successful.<\/p>\n<p>The second query can be translated into the following raw query:<\/p>\n<pre>\n<code class=\"language-sql\">\nSELECT a.name, s.flight, s.utc, r.sourceairport, r.destinationairport, r.equipment FROM `travel-sample` AS r UNNEST r.schedule AS s JOIN `travel-sample` AS a ON KEYS r.airlineid WHERE r.sourceairport = {{TO}} AND r.destinationairport = {{TO}} AND s.day = 3 ORDER BY a.name ASC\n<\/code>\n<\/pre>\n<p>We&#39;re getting schedule information about the flights by unnesting it from the JSON document and then joining on the now flattened key.<\/p>\n<h2>Wrapping Up The Application and Database Classes<\/h2>\n<p>We now have our endpoints and database methods, but they are not connected to each other. It is time to revisit the <strong>Application.java<\/strong> class and add some code to the functions we created previously:<\/p>\n<pre>\n<code class=\"language-java\">\n@RequestMapping(value=\"\/user\/login\", method= RequestMethod.GET)\npublic Object login(@RequestParam String user, @RequestParam String password) {\n    return Database.login(bucket(), user, password);\n}\n\n@RequestMapping(value=\"\/user\/login\", method=RequestMethod.POST)\npublic Object createLogin(@RequestBody String json) {\n    JsonObject jsonData = JsonObject.fromJson(json);\n    return Database.createLogin(bucket(), jsonData.getString(\"user\"), jsonData.getString(\"password\"));\n}\n<\/code>\n<\/pre>\n<p>You can see that the two static <strong>Database<\/strong> methods are called from each of the endpoints relating to user accounts. The same process can be done for the other endpoints that we&#39;ve previously created:<\/p>\n<pre>\n<code class=\"language-java\">\n@RequestMapping(\"\/airport\/findAll\")\npublic List<Map<String, Object>> airports(@RequestParam String search) {\n    return Database.findAllAirports(bucket(), search);\n}\n\n@RequestMapping(\"\/flightPath\/findAll\")\npublic List<Map<String, Object>> all(@RequestParam String from, @RequestParam String to, @RequestParam String leave)\n    throws Exception {\n    Calendar calendar = Calendar.getInstance(Locale.US);\n    calendar.setTime(DateFormat.getDateInstance(DateFormat.SHORT, Locale.US).parse(leave));\n    return Database.findAllFlightPaths(bucket(), from, to, calendar);\n}\n<\/code>\n<\/pre>\n<h2>Testing The Sample Endpoints<\/h2>\n<p>There are a few ways to test the endpoints of the application. In this example we&#39;re going to use cURL, but you can certainly use Postman for Google Chrome or something similar.<\/p>\n<p>With cURL installed, open a Terminal or Command Prompt and enter the following:<\/p>\n<pre>\n<code class=\"language-bash\">\ncurl -X GET 'https:\/\/localhost:8080\/api\/airport\/findAll?search=LAX'\n<\/code>\n<\/pre>\n<p>The above cURL command will hit the <strong>api\/airport\/findAll<\/strong> endpoint and pass a parameter of <strong>search=LAX<\/strong>. If successful, you should get a response of:<\/p>\n<pre>\n[{\"airportname\":\"Los Angeles Intl\"}]<\/pre>\n<p>The same kind of testing can done for every other endpoint.<\/p>\n<h2>Conclusion<\/h2>\n<p>We just saw how to get a sample travel application setup that uses Couchbase Server and Spring Boot for Java. Although we didn&#39;t set up a front-end, it is very possible to add one using languages such as AngularJS, jQuery, or ReactJS.<\/p>\n<p>This full project along with an AngularJS front-end can be obtained from the <a href=\"https:\/\/github.com\/couchbaselabs\/try-cb-java\">Couchbase Labs GitHub<\/a> channel.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At Couchbase Connect 2015 we demonstrated an example application that uses N1QL to query data from a sample Couchbase bucket. If you missed the conference, not a problem. We&#39;re going to go through how to reproduce this application and check [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":13873,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[1812,2201],"tags":[1725],"ppma_author":[9032],"class_list":["post-2072","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-n1ql-query","category-tools-sdks","tag-nosql-database"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.0 (Yoast SEO v26.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Traveling with Couchbase using the Java SDK - The Couchbase Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Traveling with Couchbase using the Java SDK\" \/>\n<meta property=\"og:description\" content=\"At Couchbase Connect 2015 we demonstrated an example application that uses N1QL to query data from a sample Couchbase bucket. If you missed the conference, not a problem. We&#039;re going to go through how to reproduce this application and check [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/\" \/>\n<meta property=\"og:site_name\" content=\"The Couchbase Blog\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/thepolyglotdeveloper\" \/>\n<meta property=\"article:published_time\" content=\"2015-07-14T16:37:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-14T06:47:50+00:00\" \/>\n<meta name=\"author\" content=\"Nic Raboy, Developer Advocate, Couchbase\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@nraboy\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nic Raboy, Developer Advocate, Couchbase\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/\"},\"author\":{\"name\":\"Nic Raboy, Developer Advocate, Couchbase\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/bb545ebe83bb2d12f91095811d0a72e1\"},\"headline\":\"Traveling with Couchbase using the Java SDK\",\"datePublished\":\"2015-07-14T16:37:21+00:00\",\"dateModified\":\"2025-06-14T06:47:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/\"},\"wordCount\":1474,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png\",\"keywords\":[\"NoSQL Database\"],\"articleSection\":[\"SQL++ \/ N1QL Query\",\"Tools &amp; SDKs\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/\",\"name\":\"Traveling with Couchbase using the Java SDK - The Couchbase Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png\",\"datePublished\":\"2015-07-14T16:37:21+00:00\",\"dateModified\":\"2025-06-14T06:47:50+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#primaryimage\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png\",\"width\":1800,\"height\":630},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.couchbase.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Traveling with Couchbase using the Java SDK\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"name\":\"The Couchbase Blog\",\"description\":\"Couchbase, the NoSQL Database\",\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\",\"name\":\"The Couchbase Blog\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"width\":218,\"height\":34,\"caption\":\"The Couchbase Blog\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/bb545ebe83bb2d12f91095811d0a72e1\",\"name\":\"Nic Raboy, Developer Advocate, Couchbase\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/8863514d8bed0cf6080f23db40e00354\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g\",\"caption\":\"Nic Raboy, Developer Advocate, Couchbase\"},\"description\":\"Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.\",\"sameAs\":[\"https:\/\/www.thepolyglotdeveloper.com\",\"https:\/\/www.facebook.com\/thepolyglotdeveloper\",\"https:\/\/x.com\/nraboy\"],\"url\":\"https:\/\/www.couchbase.com\/blog\/author\/nic-raboy-2\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Traveling with Couchbase using the Java SDK - The Couchbase Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/","og_locale":"en_US","og_type":"article","og_title":"Traveling with Couchbase using the Java SDK","og_description":"At Couchbase Connect 2015 we demonstrated an example application that uses N1QL to query data from a sample Couchbase bucket. If you missed the conference, not a problem. We&#39;re going to go through how to reproduce this application and check [&hellip;]","og_url":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/","og_site_name":"The Couchbase Blog","article_author":"https:\/\/www.facebook.com\/thepolyglotdeveloper","article_published_time":"2015-07-14T16:37:21+00:00","article_modified_time":"2025-06-14T06:47:50+00:00","author":"Nic Raboy, Developer Advocate, Couchbase","twitter_card":"summary_large_image","twitter_creator":"@nraboy","twitter_misc":{"Written by":"Nic Raboy, Developer Advocate, Couchbase","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#article","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/"},"author":{"name":"Nic Raboy, Developer Advocate, Couchbase","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/bb545ebe83bb2d12f91095811d0a72e1"},"headline":"Traveling with Couchbase using the Java SDK","datePublished":"2015-07-14T16:37:21+00:00","dateModified":"2025-06-14T06:47:50+00:00","mainEntityOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/"},"wordCount":1474,"commentCount":0,"publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","keywords":["NoSQL Database"],"articleSection":["SQL++ \/ N1QL Query","Tools &amp; SDKs"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/","url":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/","name":"Traveling with Couchbase using the Java SDK - The Couchbase Blog","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#primaryimage"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","datePublished":"2015-07-14T16:37:21+00:00","dateModified":"2025-06-14T06:47:50+00:00","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#primaryimage","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","width":1800,"height":630},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/blog\/traveling-with-couchbase-using-the-java-sdk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Traveling with Couchbase using the Java SDK"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/blog\/#website","url":"https:\/\/www.couchbase.com\/blog\/","name":"The Couchbase Blog","description":"Couchbase, the NoSQL Database","publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/blog\/#organization","name":"The Couchbase Blog","url":"https:\/\/www.couchbase.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","width":218,"height":34,"caption":"The Couchbase Blog"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/bb545ebe83bb2d12f91095811d0a72e1","name":"Nic Raboy, Developer Advocate, Couchbase","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/8863514d8bed0cf6080f23db40e00354","url":"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g","caption":"Nic Raboy, Developer Advocate, Couchbase"},"description":"Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.","sameAs":["https:\/\/www.thepolyglotdeveloper.com","https:\/\/www.facebook.com\/thepolyglotdeveloper","https:\/\/x.com\/nraboy"],"url":"https:\/\/www.couchbase.com\/blog\/author\/nic-raboy-2\/"}]}},"authors":[{"term_id":9032,"user_id":63,"is_guest":0,"slug":"nic-raboy-2","display_name":"Nic Raboy, Developer Advocate, Couchbase","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g","author_category":"","last_name":"Raboy","first_name":"Nic","job_title":"","user_url":"https:\/\/www.thepolyglotdeveloper.com","description":"Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand."}],"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/2072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/comments?post=2072"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/2072\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media\/13873"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media?parent=2072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/categories?post=2072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/tags?post=2072"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=2072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}