couchbase_具有Couchbase,Java EE和WildFly的CRUD Java应用程序

couchbase

Couchbase是一个开源的NoSQL文档数据库。 它允许访问,索引和查询JSON文档,同时利用集成的分布式缓存来实现高性能数据访问。

开发人员可以使用不同的语言(Java,Go,.NET,Node,PHP,Python,C) 多种SDK将应用程序编写到Couchbase。 该博客将展示如何使用Java SDK for Couchbase轻松创建CRUD应用程序。

带有Couchbase的REST

该应用程序将使用curl向部署在WildFly上的JAX-RS端点发出REST命令。 然后,这些命令将对Couchbase中的travel-sample存储桶执行CRUD操作。 N1QL(JSONSQL查询语言)将用于与Couchbase通信以检索结果。 “生成器模式”和原始N1QL命令都将使用。

couchbase-crud-javaee-curl

TL; DR

样本的完整源代码和说明可在github.com/arun-gupta/couchbase-javaee中找到 。

让我们开始吧!

运行Couchbase服务器

可以从“ Couchbase服务器下载”页面轻松下载Couchbase服务器 。 在容器化的世界中, 使用Docker启动Couchbase服务器要容易得多 。

如果在您的计算机上配置了Docker,那么最简单的方法是将Docker Compose用于Couchbase :

mycouchbase:name: mycouchbaseimage: couchbase/servervolumes:- ~/couchbase:/opt/couchbase/varports:- 8091:8091- 8092:8092 - 8093:8093 - 11210:11210

启动应用程序服务器显示:

> docker-compose up -d
Creating couchbaseserver_mycouchbase_1

然后日志可以看成是:

> docker-compose logs
Attaching to couchbaseserver_mycouchbase_1
mycouchbase_1 | Starting Couchbase Server -- Web UI available at http://<ip>:8091

该数据库需要配置,并在“ 配置Couchbase服务器”中进行了说明 。 确保安装travel-sample桶。

在WildFly上部署Java EE应用程序

  • 下载WildFly 9.0.2 ,解压缩,然后以./wildfly-9.0.0.Final/bin/standalone.sh启动WildFly应用程序服务器。
  • Git克隆仓库: git clone https://github.com/arun-gupta/couchbase-javaee.git
  • 更改目录cd couchbase-javaee
  • 将应用程序部署到WildFly: mvn install -Pwildfly

该应用程序通过导入以下Maven坐标将Java SDK用于Couchbase :

<dependency><groupId>com.couchbase.client</groupId><artifactId>java-client</artifactId><version>2.2.1</version>
</dependency>

使用cURL调用REST端点

GET航空公司资源(最多10个)

让我们查询数据库以列出10个航空公司资源。

请求

~ > curl -v http://localhost:8080/couchbase-javaee/resources/airline
* Hostname was NOT found in DNS cache
*   Trying ::1...
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /couchbase-javaee/resources/airline HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:8080
> Accept: */*
>

响应

< HTTP/1.1 200 OK
< Connection: keep-alive
< X-Powered-By: Undertow/1
* Server WildFly/9 is not blacklisted
< Server: WildFly/9
< Content-Type: application/octet-stream
< Content-Length: 1415
< Date: Wed, 18 Nov 2015 21:19:15 GMT
< 
* Connection #0 to host localhost left intact
[{"travel-sample":{"country":"France","iata":"SB","callsign":"AIRCALIN","name":"Air Caledonie International","icao":"ACI","id":139,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"WQ","callsign":null,"name":"PanAm World Airways","icao":"PQW","id":13633,"type":"airline"}}, {"travel-sample":{"country":"United Kingdom","iata":"BA","callsign":"SPEEDBIRD","name":"British Airways","icao":"BAW","id":1355,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"FL","callsign":"CITRUS","name":"AirTran Airways","icao":"TRS","id":1316,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"-+","callsign":null,"name":"U.S. Air","icao":"--+","id":13391,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"Q5","callsign":"MILE-AIR","name":"40-Mile Air","icao":"MLA","id":10,"type":"airline"}}, {"travel-sample":{"country":"France","iata":"AF","callsign":"AIRFRANS","name":"Air France","icao":"AFR","id":137,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"K5","callsign":"SASQUATCH","name":"SeaPort Airlines","icao":"SQH","id":10765,"type":"airline"}}, {"travel-sample":{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlinair","icao":"RLA","id":1203,"type":"airline"}}, {"travel-sample":{"country":"United Kingdom","iata":"5W","callsign":"FLYSTAR","name":"Astraeus","icao":"AEU","id":112,"type":"airline"}}]

N1QL查询对此写为:

N1qlQuery query = N1qlQuery.simple(select("*").from(i(database.getBucket().name())).limit(10));

并且也可以写成:

SELECT * FROM `travel-sample` LIMIT 10

您可以选择更新代码以包含ORDER BY子句,如N1QL教程中所示。

获得一份航空公司资源

使用id属性查询单个航空公司资源

请求

~ > curl -v http://localhost:8080/couchbase-javaee/resources/airline/139
* Hostname was NOT found in DNS cache
*   Trying ::1...
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /couchbase-javaee/resources/airline/139 HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:8080
> Accept: */*
>

响应

< HTTP/1.1 200 OK
< Connection: keep-alive
< X-Powered-By: Undertow/1
* Server WildFly/9 is not blacklisted
< Server: WildFly/9
< Content-Type: application/octet-stream
< Content-Length: 148
< Date: Wed, 18 Nov 2015 21:23:34 GMT
< 
* Connection #0 to host localhost left intact
{"travel-sample":{"country":"France","iata":"SB","callsign":"AIRCALIN","name":"Air Caledonie International","icao":"ACI","id":139,"type":"airline"}}

发布新的航空公司资源

了解如何使用CBQ工具从CLI运行N1QL查询并验证现有样本数据:

bin > ./cbq -engine=http://192.168.99.100:8093
Couchbase query shell connected to http://192.168.99.100:8093/ . Type Ctrl-D to exit.
cbq> select * from `travel-sample` where name="Airlinair" limit 10;
{"requestID": "ce2de67b-2c05-47df-afbe-343cb7409d2b","signature": {"*": "*"},"results": [{"travel-sample": {"callsign": "AIRLINAIR","country": "France","iata": "A5","icao": "RLA","id": 1203,"name": "Airlinair","type": "airline"}}],"status": "success","metrics": {"elapsedTime": "3.418285894s","executionTime": "3.418232688s","resultCount": 1,"resultSize": 294}
}

该查询检索航空公司名称为Airlinair文档。 该计数显示在metrics.resultCount

使用POST创建一个新文档。

请求

~ > curl -v -H "Content-Type: application/json" -X POST -d '{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlinair","icao":"RLA","type":"airline"}' http://localhost:8080/couchbase-javaee/resources/airline
* Hostname was NOT found in DNS cache
*   Trying ::1...
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /couchbase-javaee/resources/airline HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:8080
> Accept: */*
> Content-Type: application/json
> Content-Length: 104
>

响应

* upload completely sent off: 104 out of 104 bytes
< HTTP/1.1 200 OK
< Connection: keep-alive
< X-Powered-By: Undertow/1
* Server WildFly/9 is not blacklisted
< Server: WildFly/9
< Content-Type: application/octet-stream
< Content-Length: 117
< Date: Wed, 18 Nov 2015 21:42:51 GMT
< 
* Connection #0 to host localhost left intact
{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlinair","icao":"RLA","id":"19810","type":"airline"}

使用CBQ再次查询,现在结果显示为:

cbq> select * from `travel-sample` where name="Airlinair" limit 10;
{"requestID": "5e79031a-f7ee-4cc9-8c87-4e3b7484f09f","signature": {"*": "*"},"results": [{"travel-sample": {"callsign": "AIRLINAIR","country": "France","iata": "A5","icao": "RLA","id": 1203,"name": "Airlinair","type": "airline"}},{"travel-sample": {"callsign": "AIRLINAIR","country": "France","iata": "A5","icao": "RLA","id": "19810","name": "Airlinair","type": "airline"}}],"status": "success","metrics": {"elapsedTime": "3.342391947s","executionTime": "3.342343455s","resultCount": 2,"resultSize": 591}
}

请注意,返回的是两个JSON文档,而不是发出POST命令之前的一个。

放置现有的航空公司资源

使用HTTP POST更新现有资源。

travel-sample存储区的数据模型需要在有效负载和URI中都包含“ id”属性。

请求

~ > curl -v -H "Content-Type: application/json" -X PUT -d '{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlin Air","icao":"RLA","type":"airline","id": "19810"}' http://localhost:8080/couchbase-javaee/resources/airline/19810
* Hostname was NOT found in DNS cache
*   Trying ::1...
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> PUT /couchbase-javaee/resources/airline/19810 HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:8080
> Accept: */*
> Content-Type: application/json
> Content-Length: 118
> 
* upload completely sent off: 118 out of 118 bytes

航空公司名称从“ Airlinair”更新为“ Airlin Air”,所有其他属性保持不变。

响应

< HTTP/1.1 200 OK
< Connection: keep-alive
< X-Powered-By: Undertow/1
* Server WildFly/9 is not blacklisted
< Server: WildFly/9
< Content-Type: application/octet-stream
< Content-Length: 117
< Date: Wed, 18 Nov 2015 21:46:16 GMT
< 
* Connection #0 to host localhost left intact
{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlin Air","icao":"RLA","id":"19810","type":"airline"}

更新的记录显示在响应中。

查询Airlinair会给出:

cbq> select * from `travel-sample` where name="Airlinair" limit 10;
{"requestID": "a8d72427-9f4b-49ab-a77a-17cd99cdce5f","signature": {"*": "*"},"results": [{"travel-sample": {"callsign": "AIRLINAIR","country": "France","iata": "A5","icao": "RLA","id": 1203,"name": "Airlinair","type": "airline"}}],"status": "success","metrics": {"elapsedTime": "3.372603693s","executionTime": "3.37256091s","resultCount": 1,"resultSize": 294}
}

因此,以前添加的记录现在已更新,因此不会出现在查询结果中。 查询Airlin Air可得到:

cbq> select * from `travel-sample` where name="Airlin Air" limit 10;
{"requestID": "a3797a73-d879-4ca1-be90-e07179aae118","signature": {"*": "*"},"results": [{"travel-sample": {"callsign": "AIRLINAIR","country": "France","iata": "A5","icao": "RLA","id": "19810","name": "Airlin Air","type": "airline"}}],"status": "success","metrics": {"elapsedTime": "3.393649025s","executionTime": "3.393530368s","resultCount": 1,"resultSize": 298}
}

这显示了新更新的文档。

删除现有的航空公司资源

查询唯一的ID:

cbq> select * from `travel-sample` where id="19810" limit 10;
{"requestID": "47a315cd-afe4-45a8-8814-5ab3034e0d0f","signature": {"*": "*"},"results": [{"travel-sample": {"callsign": "AIRLINAIR","country": "France","iata": "A5","icao": "RLA","id": "19810","name": "Airlin Air","type": "airline"}}],"status": "success","metrics": {"elapsedTime": "3.006863656s","executionTime": "3.006821997s","resultCount": 1,"resultSize": 298}
}

请注意,返回了一个文档。

让我们删除此文档。

请求

~ > curl -v -X DELETE http://localhost:8080/couchbase-javaee/resources/airline/19810
* Hostname was NOT found in DNS cache
*   Trying ::1...
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> DELETE /couchbase-javaee/resources/airline/19810 HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:8080
> Accept: */*
>

响应

> HTTP/1.1 200 OK
> Connection: keep-alive
> X-Powered-By: Undertow/1
* Server WildFly/9 is not blacklisted
> Server: WildFly/9
> Content-Type: application/octet-stream
> Content-Length: 136
> Date: Wed, 18 Nov 2015 21:52:47 GMT
> 
* Connection #0 to host localhost left intact
{"travel-sample":{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlin Air","icao":"RLA","id":"19810","type":"airline"}}

删除的文档显示在响应中。

再次查询已删除的ID:

cbq> select * from `travel-sample` where id="19810" limit 10;
{"requestID": "972b0bbd-ba25-4f6c-a30e-ed188bf43588","signature": {"*": "*"},"results": [],"status": "success","metrics": {"elapsedTime": "3.261481199s","executionTime": "3.261431917s","resultCount": 0,"resultSize": 0}
}

而且没有结果返回!

  • 如前所述,完整的代码库位于github.com/arun-gupta/couchbase-javaee 。

请享用!

翻译自: https://www.javacodegeeks.com/2015/11/crud-java-application-couchbase-java-ee-wildfly.html

couchbase

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/336515.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java 对象的态_Java面向对象-------多态总结

1.多态&#xff1a;是同一个行为具有多个不同表现形式或形态的能力。多态就是同一个接口&#xff0c;使用不同的实例而执行不同操作&#xff0c;如图所示&#xff1a;多态性是对象多种表现形式的体现。2.多态作用&#xff1a;1. 消除类型之间的耦合关系2. 可替换性3. 可扩充性4…

netbeans连接数据库_NetBeans Java EE技巧#1 –数据库中的实体类

netbeans连接数据库NetBeans IDE是开发各种应用程序的绝佳选择。 具体来说&#xff0c;我每天都使用它来开发和维护Java EE应用程序。 在过去的几个发行版中&#xff0c;不仅Java EE的生产力提高了&#xff0c;而且NetBeans IDE还减少了开发应用程序的时间……使Java EE和NetBe…

jboss fuse 教程_使用JBoss Fuse和OpenShift进行Cloud Native Camel骑行

jboss fuse 教程红帽公司最近发布了一个微服务集成工具包&#xff0c;用于在OpenShift v3的Docker和Kubernetes环境中运行我们的微服务。 为了帮助人们更好地理解这一点&#xff0c;我将Rider Auto应用程序迁移到一组微服务中&#xff0c;该应用程序已经存在了一段时间&#xf…

javaone_JavaOne 2015 –又一年,又向前迈进了一步

javaoneJavaOne 2015 San Francisco于10月25日至29日举行。 我很自豪地说这是我第九个人参加JavaOne&#xff0c;第七个人是演讲者&#xff0c;第四个人是Oracle员工&#xff0c;第三个人是内容委员会成员&#xff0c;第二个人是项目负责人。 我认为对于JavaOne来说&#xff0c…

lnp和mysql分开安装_毕业设计之LNP+DISCUZ +分离的数据库操作

环境介绍&#xff1a;CentOS6.9最小化安装https://nginx.org/download/nginx-1.16.1.tar.gzhttps://www.php.net/distributions/php-7.3.14.tar.gzhttps://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.29.tar.gznginxphp主机&#xff1a;192.168.111.141mysql主机&…

java中hotspot_Java 8中HotSpot选项的改进文档

java中hotspotOracle的HotSpot Java 8实现中引入的一些小但受欢迎的功能之一是在启动器的文档中添加了许多常见的HotSpot Java启动器 &#xff08; java &#xff09;选项/标志。 过去&#xff0c;甚至对某些相当常见的HotSpot JVM选项都感兴趣的开发人员不得不潜在地寻找多个不…

qchart折现图_Qt开发技术:QCharts(二)QCharts折线图介绍、Demo以及代码详解

若该文为原创文章&#xff0c;未经允许不得转载各位读者&#xff0c;知识无穷而人力有穷&#xff0c;要么改需求&#xff0c;要么找专业人士&#xff0c;要么自己研究敬请期待…红胖子&#xff0c;来也&#xff01;介绍了整体框架&#xff0c;开始动手码代码&#xff0c;按照顺…

各类算法思想

暴力枚举法 使用场景注意事项 1.建立一个简洁的搜索模型&#xff0c;变量尽可能少 2.尽可能减少搜索空间 递归与分治法 分治使用最广泛一类算法。采用递归的思想将较大规模的问题分成小问题来求。如果原问题可以分割成k个子问题&#xff0c;并且这些子问题可以重复利用&am…

eclipse neon_在自定义Java 9映像上运行Eclipse Neon

eclipse neon我已经开始修改自定义Java二进制运行时映像文件。 映像文件是打包为运行时平台的模块的配置。 基本上&#xff0c;默认映像包含组成Java运行时的所有内容。 自定义图像可以包含该图像的一些子集。 例如&#xff0c;我创建了一个仅包含“ compact 3”概要文件的映像…

java json注解_返回json用什么注解

返回json用“ResponseBody”注解&#xff0c;“ResponseBody”是作用在方法上的&#xff0c;“ResponseBody”表示该方法的返回结果直接写入“HTTP response body”中。本篇文章将介绍两种示例进行JSON返回注解方式演示。示例1ResponseBody是作用在方法上的&#xff0c;Respons…

java数据结构博客园_常见数据结构的Java实现

单链表的Java实现首先参考wiki上的单链表说明&#xff0c;单链表每个节点包含数据和指向链表中下一个节点的指针或引用。然后看代码import java.lang.*;public class SinglyLinkeList{Node start;public SinnglyLinkedList(){this.startnull;}public void addFront(Object newD…

jboss4 java_带有JBoss工具的OpenShift 3上的Java EE 7应用程序

jboss4 java您可以使用最新版本的JBoss Tools OpenShift插件在Eclipse中创建和管理OpenShift应用程序。 他们要么预先捆绑了最新的 JBoss Developer Studio&#xff08;9.0.0.GA&#xff09; &#xff0c;也可以将它们安装到现有的Eclipse Mars中。 这篇文章将引导您通过JBoss…

javaslang_使用Javaslang的Java 8中的功能数据结构

javaslangJava 8的lambda&#xff08;λ&#xff09;使我们能够创建出色的API。 它们极大地提高了语言的表达能力。 Javaslang利用lambda来基于功能模式创建各种新功能。 其中之一是功能性集合库&#xff0c;旨在替代Java的标准集合。 &#xff08;这只是鸟瞰图&#xff0c;您…

java调用kafka接口发送数据_Java调用Kafka生产者,消费者Api及相关配置说明

本次的记录内容包括&#xff1a;1.Java调用生产者APi流程2.Kafka生产者Api的使用及说明3.Kafka消费者Api的使用及说明4.Kafka消费者自动提交Offset和手动提交Offset5.自定义生产者的拦截器&#xff0c;分区器那么接下来我就带大家熟悉以上Kafka的知识说明1.Java调用生产者APi流…

java如何模拟请求_单元测试如何模拟用户请求

python web自动化测试设计构工具书40.9元包邮(需用券)去购买 >错误正当我高高兴兴写完后台c层的测试代码准备提交时&#xff0c;测试机器人报了很多401错误&#xff0c;把代码拉下来一看&#xff0c;原来当我写代码时&#xff0c;我的伙伴已经写好后台的拦截器了&#xff0c…

LeetCode 83. 删除排序链表中的重复元素

原题链接 解法&#xff1a;通过一个指针从头到尾进行扫描 class Solution { public:ListNode* deleteDuplicates(ListNode* head) {if(!head)return nullptr;auto p1 head;while(p1->next){if(p1->next->val p1->val)p1->nextp1->next->next;else p1 …

后端 java ee_刷新器-Java EE 7后端十大功能

后端 java ee这是我的小型Java EE 7复习系列的第二部分。 在进行了简要概述的第一篇介绍之后&#xff0c;我决定请Arjan Tijms撰写有关Java EE 7中他最喜欢的后端新功能的信息。如果您关注Java EE领域&#xff0c;您将会知道Arjan。 他是Java EE开发人员&#xff0c;JSF和Secur…

java cucumber_为Java + STANDARD值引入Cucumber

java cucumber作为软件开发人员&#xff0c;我们都有最喜欢的工具来使我们成功。 许多人在开始工作时就很适合这份工作&#xff0c;但很快就不见了。 其他人则需要太多的设置和培训才能“将脚趾浸入水中”&#xff0c;只是为了简单地确定它们是否是正确的工具即可。 Cucumber …

文章id 文章标题点击量php,WordPress如何通过文章ID获取文章标题等信息

如果我们想要在某一个主题的php文件中调用文章的标题&#xff0c;内容等信息&#xff0c;而在WordPress中唯一一直不会改变的就是文章发布时生成的ID&#xff0c;我们只需要获取文章的ID&#xff0c;即可通过文章ID来获取我们想要的文章信息。调用方法php$id // 文章的 id$tit…

javaone_JavaOne 2015:高级模块化开发

javaoneJavaOne 2015看到了Project Jigsaw团队关于Java 9中的模块化的一系列讨论 。它们都是非常有趣的&#xff0c;并且充满了宝贵的信息&#xff0c;我敦促每个Java开发人员都注意它们。 除此之外&#xff0c;我想给社区一种搜索和引用它们的方法&#xff0c;因此我在这里总…