具有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: mycouchbase
  image: couchbase/server
  volumes:
    - ~/couchbase:/opt/couchbase/var
  ports:
    - 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
    }
}

而且没有结果返回!

请享用!

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值