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

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(JSON的SQL查询语言)将用于与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

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

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

相关文章

c语言10个人 三向成绩,C语言入门学习精华:这样学习C语言最有效

C语言入门学习精华&#xff1a;这样学习C语言最有效c语言死了吗&#xff1f;本材料描述了使用C语言的高级技能&#xff0c;并努力将您的C语言能力从“基本”提升到“高级”。然而&#xff0c;学习态度比学习方法要好。在正式学习之前&#xff0c;有一个问题是不能抱怨的。也就是…

jQuery UI全教程之一(dialog的使用教程)

jQuery UI目前的版本已经更新到了1.8.7。个人感觉和easyui相比起来&#xff0c;jQuery UI在界面的美观程度和可定制型更强一些。所以再次将一些jQuery UI组件的用法说明一下&#xff0c;方便日后查阅。也方面没接触jQuery UI的人能早日使用jQuery UI套件 (一)首先来说jQuery UI…

akka es/cqrs_在Akka中实现主从/网格计算模式

akka es/cqrs主从模式是容错和并行计算的主要示例。 模式背后的想法是将工作划分为相同的子任务&#xff0c;然后将其委派给从属。 这些从属节点或实例将处理工作任务&#xff0c;并将结果发送回主节点。 然后主节点将编译从所有从节点接收到的结果。关键是从节点仅知道如何处理…

05 HTML字符串转换成jQuery对象、绑定数据到元素上

1 要求 将一段 HTML脚本 封装成一个字符串&#xff0c;将这个字符串转换成一个jQuery对象&#xff1b;然后将这个jQuery对象添加到指定的元素中去 2 步骤 定义字符串 var str <div id"box01">hello world</div>; //定义一个字符串 利用jQuery框架将字符…

c语言求佩尔方程的解设计思路,c语言版 佩尔方程求最小正整数解及第k解(矩阵快速幂)...

佩尔方程讲解连接&#xff1a;若一个丢番图方程具有以下的形式&#xff1a;且为正整数&#xff0c;则称此方程为佩尔方程(英文&#xff1a;Pells equation 德文&#xff1a;Pellsche Gleichung) 若是完全平方数&#xff0c;则这个方程式只有解(实际上对任意的&#xff0c;都是解…

android 9.0 https 适配,如何适配 Android 9.0? 在 Android 9.0 上发生 SSL handshake timed out 异常怎么解决...

Android 9.0 开始&#xff0c;默认不允许明文传输&#xff0c;所以在建立网络连接时会使用 https 连接&#xff0c;同时进行安全认证。如果应用没有做对应处理&#xff0c;即会发生上述异常。解决方法有两种&#xff1a;一. 在应用里声明允许明文传输.1. 在应用的 res/xml 文件…

java 7.函数-递归_带有谓词的Java中的函数样式-第1部分

java 7.函数-递归您一直在听到将要席卷全球的函数式编程&#xff0c;而您仍然坚持使用普通Java&#xff1f; 不用担心&#xff0c;因为您已经可以在日常Java中添加一些功能样式。 此外&#xff0c;它很有趣&#xff0c;可以节省许多代码行并减少错误。 什么是谓词&#xff1f; …

大话oraclerac集群、高可用性、备份与恢复_数腾Oracle RAC数据库灾备解决方案

“一个系统包含很多模块&#xff0c;数据库、前端、缓存、搜索、消息队列等&#xff0c;每个模块都需要做到高可用&#xff0c;才能保证整个系统的高可用。”数据库作为现代信息社会的基石&#xff0c;几乎所有的计算机应用软件都构建于数据库系统之上&#xff0c;对于数据库而…

Android面试题Service,Android面试题-IntentService源码分析

自定义控件联网工具数据库源码分析相关面试题Activity相关面试题Service相关面试题与XMPP相关面试题与性能优化相关面试题与登录相关面试题与开发相关面试题与人事相关面试题人事面试宝典IntentService是继承于Service并处理异步请求的一个类&#xff0c;在IntentService内有一…

OpenGL中的Shader

http://blog.csdn.net/huangcanjun187/article/details/52474365 学习总结自&#xff1a;http://learnopengl.com/#!Getting-started/Hello-Triangle http://learnopengl.com/#!Getting-started/Shaders 继上篇文章中提到&#xff0c;OpenGL是为了在GPU上同时跑成千上万个程序&…

在html中金色怎么写,ps金色数值是多少?

一些常用的金色表示值&#xff1a;R255&#xff0c;G215&#xff0c;B0R205&#xff0c;G127&#xff0c;B50R166&#xff0c;G124&#xff0c;B64R217&#xff0c;G217&#xff0c;B25关于金色rgb值&#xff0c;金色就是黄色&#xff0c;但是我们看到的一些金色效果只是用颜色…

EAP 7 Alpha和Java EE 7入门

红帽JBoss企业应用程序平台7&#xff08;JBoss EAP 7&#xff09;是基于开放标准构建并符合Java Enterprise Edition 7规范的中间件平台。 它基于WildFly等经过验证的创新开源技术之上&#xff0c;它将使Java EE 7的开发变得更加容易。 这是有关如何开始使用最新ALPHA版本的快速…

简单点赞效果html,js实现点赞效果

javascript实现点赞或踩加一&#xff0c;再点一次减一的效果好多新手在网上找不到点赞效果的代码&#xff0c;今天给大家分享一个采用js写的简单方法(有点错误&#xff0c;已修正)效果图如下HTML代码可直接ctrl c复制代码3030CSS代码可直接ctrl c复制代码(注&#xff1a;样式…

html显示和隐藏不占空间的是什么,css怎么设置不占用空间的隐藏?

css怎么设置不占用空间的隐藏&#xff1f;下面本篇文章就来给大家介绍一下使用CSS设置不占用空间隐藏的方法。有一定的参考价值&#xff0c;有需要的朋友可以参考一下&#xff0c;希望对大家有所帮助。在CSS中&#xff0c;可以利用display属性&#xff0c;设置display:none来设…

javame_JavaME:Google静态地图API

javame无论您是需要基于位置的应用程序的地图还是只是出于娱乐目的&#xff0c;都可以使用有史以来最简单的方法&#xff1a;Google Static Maps API。 在这篇文章中&#xff0c;我们将看到如何从纬度和经度获得地图作为图像。 可以使用Location API获得纬度和经度&#xff0c;…

js如何获取html图片,JS/JQuery获取网页或文章或某DIV所有图片

要获取网页所有图片&#xff0c;我们可以通过Javascript就能轻松实现&#xff0c;不过要想获得文章或某容器(如&#xff1a;Div)里所有图片&#xff0c;使用JQuery而不是Javascript来实现就会变得更加简单。本文将给你详细介绍。通过Javascript获取网页所有图片html代码JS/JQue…

带有Netflix Ribbon的Spring Cloud Rest Client-基础知识

在较早的博客文章中&#xff0c;我介绍了Spring Cloud世界中REST客户端的各种选项。 所有选项围绕着基于Netflix OSS的名为Ribbon的组件&#xff0c;该组件处理与承载服务的不同实例之间的调用负载平衡&#xff0c;处理故障转移&#xff0c;超时等有关的方面。在此&#xff0c;…

html中给文章怎么设置行高,css如何设置行距?

在网页的布局中几大段文字挤在一起总归是不好看的&#xff0c;这时候我们就需要来设置行间距来让文字看起来不拥挤&#xff0c;也让整个页面看起来美观整洁&#xff0c;那么&#xff0c;行间距该如何设置呢&#xff1f;本篇文章就来给大家介绍一下css行间距的设置方法。首先我们…

初中数学知识点总结_初中物理 | 最全知识点总结

往期回顾初中物理 | 知识点总结一&#xff1a;机械运动初中物理 | 知识点总结二&#xff1a;声现象初中物理 | 知识点总结三&#xff1a;物态变化初中物理 | 知识点总结四&#xff1a;光现象初中物理 | 知识点总结五&#xff1a;透镜及其应用初中物理 | 知识点总结六&#xff1…

redis版本_全球首发|阿里云正式推出云数据库Redis6.0版本

Redis 6.0更多精彩详情2020年6月23日&#xff0c;阿里云正式推出云数据库Redis 6.0版本。Redis 6.0版本为Redis开源社区于5月2日发布的全新版本&#xff0c;包含多项重大功能更新和大幅度的性能提升。依托于阿里云强大的云服务与管控能力&#xff0c;以及团队的快速跟进&#x…