日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2023/12/3 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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命令都將使用。

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

總結

以上是生活随笔為你收集整理的couchbase_具有Couchbase,Java EE和WildFly的CRUD Java应用程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。