PostgreSQL integration with pg_matlab

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介:
pg_matlab是一个pg插件, 并非一般的过程语言. 使用pg_matlab创建的函数必须转换成c代码后, 编译并在数据库中使用共享库的方式调用.

pg_matlab下载和安装 : 
wget http://www.cybertec.at/download/pg_matlab/pg_matlab-v2.tar.gz
tar -zxvf pg_matlab-v2.tar.gz
mv pg_matlab /opt/soft_bak/postgresql-9.3beta2/contrib/
[root@db-172-16-3-39 contrib]# cd /opt/soft_bak/postgresql-9.3beta2/contrib/pg_matlab/
[root@db-172-16-3-39 pg_matlab]# . /home/pg93/.bash_profile 
root@db-172-16-3-39-> which pg_config
/opt/pgsql9.3beta2/bin/pg_config
root@db-172-16-3-39-> gmake clean
gmake clean -e -C matcored
gmake[1]: Entering directory `/opt/soft_bak/postgresql-9.3beta2/contrib/pg_matlab/matcored'
rm -f matcored.o mat_wrapper.o matcored
gmake[1]: Leaving directory `/opt/soft_bak/postgresql-9.3beta2/contrib/pg_matlab/matcored'
USE_PGXS=1 gmake clean -e -C libmatpg
gmake[1]: Entering directory `/opt/soft_bak/postgresql-9.3beta2/contrib/pg_matlab/libmatpg'
rm -f matpg.so   libmatpg.a  libmatpg.pc
rm -f create_mat_pg_operators.sql
rm -f mat_pg_funcs.o
gmake[1]: Leaving directory `/opt/soft_bak/postgresql-9.3beta2/contrib/pg_matlab/libmatpg'
root@db-172-16-3-39-> gmake
gmake -e -C matcored
gmake[1]: Entering directory `/opt/soft_bak/postgresql-9.3beta2/contrib/pg_matlab/matcored'
cc -Wall -c -o matcored.o matcored.c -I/extern/include -I../libmatcore/distrib
In file included from matcored.c:15:
../libmatcore/distrib/libmatcore.h:17:22: error: mclmcrrt.h: No such file or directory
In file included from matcored.c:15:
../libmatcore/distrib/libmatcore.h:63: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘MW_CALL_CONV’
../libmatcore/distrib/libmatcore.h:68: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘MW_CALL_CONV’
../libmatcore/distrib/libmatcore.h:71: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘libmatcoreTerminate’
../libmatcore/distrib/libmatcore.h:76: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘libmatcorePrintStackTrace’
../libmatcore/distrib/libmatcore.h:79: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘MW_CALL_CONV’
../libmatcore/distrib/libmatcore.h:82: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘libmatcoreGetMcrID’
../libmatcore/distrib/libmatcore.h:86: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘MW_CALL_CONV’
matcored.c: In function ‘send_matcored_response’:
matcored.c:60: warning: implicit declaration of function ‘strncpy’
matcored.c:60: warning: incompatible implicit declaration of built-in function ‘strncpy’
matcored.c:60: warning: implicit declaration of function ‘strlen’
matcored.c:60: warning: incompatible implicit declaration of built-in function ‘strlen’
matcored.c: In function ‘send_matcored_error’:
matcored.c:74: warning: incompatible implicit declaration of built-in function ‘strncpy’
matcored.c:74: warning: incompatible implicit declaration of built-in function ‘strlen’
matcored.c:81: warning: implicit declaration of function ‘memset’
matcored.c:81: warning: incompatible implicit declaration of built-in function ‘memset’
matcored.c: At top level:
matcored.c:99: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘read_int_param’
matcored.c: In function ‘send_int_param’:
matcored.c:141: warning: incompatible implicit declaration of built-in function ‘strncpy’
matcored.c:141: warning: incompatible implicit declaration of built-in function ‘strlen’
matcored.c: At top level:
matcored.c:150: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘read_binary_param’
matcored.c: In function ‘send_binary_param’:
matcored.c:186: warning: incompatible implicit declaration of built-in function ‘strncpy’
matcored.c:186: warning: incompatible implicit declaration of built-in function ‘strlen’
matcored.c: In function ‘init_matcore’:
matcored.c:197: warning: implicit declaration of function ‘libmatcoreInitialize’
matcored.c: In function ‘terminate_matcore’:
matcored.c:207: warning: implicit declaration of function ‘libmatcoreTerminate’
matcored.c: In function ‘add_vector’:
matcored.c:226: warning: implicit declaration of function ‘read_int_param’
matcored.c:231: warning: implicit declaration of function ‘read_binary_param’
matcored.c: In function ‘handle_client’:
matcored.c:276: warning: implicit declaration of function ‘strncmp’
matcored.c:276: warning: incompatible implicit declaration of built-in function ‘strlen’
matcored.c: In function ‘set_sighandler’:
matcored.c:346: warning: incompatible implicit declaration of built-in function ‘memset’
matcored.c: In function ‘listen_on_port’:
matcored.c:371: warning: incompatible implicit declaration of built-in function ‘memset’
matcored.c: In function ‘connect_on_port’:
matcored.c:435: warning: incompatible implicit declaration of built-in function ‘memset’
matcored.c:447: warning: incompatible implicit declaration of built-in function ‘strncpy’
matcored.c: In function ‘main’:
matcored.c:471: warning: implicit declaration of function ‘strcmp’
gmake[1]: *** [matcored.o] Error 1
gmake[1]: Leaving directory `/opt/soft_bak/postgresql-9.3beta2/contrib/pg_matlab/matcored'
gmake: *** [all] Error 2


错误来自libmatcore/distrib/libmatcore.h
/*
 * MATLAB Compiler: 4.13 (R2010a)
 * Date: Thu Feb 10 10:54:51 2011
 * Arguments: "-B" "macro_default" "-W" "lib:libmatcore" "-T" "link:lib" "-d"
 * "/home/babak/Downloads/pg_matlab/libmatcore/src" "-w"
 * "enable:specified_file_mismatch" "-w" "enable:repeated_file" "-w"
 * "enable:switch_ignored" "-w" "enable:missing_lib_sentinel" "-w"
 * "enable:demo_license" "-g" "-G" "-v" "/home/babak/Downloads/pg_matlab/add.m" 
 */
#include "mclmcrrt.h"

头文件不存在, 因此在安装pg_matlab前需要先安装MCR.
unzip MCR_R2012a_glnxa64_installer.zip
修改输入文件文件
vi installer_input.txt
agreeToLicense=yes
product.MATLAB_Compiler

安装 
./install -mode silent -inputFile ./installer_input.txt

安装正常的输出 : 
添加到LD_LIBRARY_PATH
/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/bin/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/sys/os/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/sys/java/jre/glnxa64/jre/lib/amd64
Next, set the XAPPLRESDIR environment variable to the following value:

/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/X11/app-defaults
(Sep 25, 2013 16:02:28) Exiting with status 0
(Sep 25, 2013 16:02:28) End - Successful.
Finished

mcr默认安装到这里来了
root@digoal-PowerEdge-R610-> ll /usr/local/MATLAB/MATLAB_Compiler_Runtime/v717
total 136K
drwxr-xr-x  5 root root 4.0K Sep 25 16:02 appdata
drwxr-xr-x  5 root root 4.0K Sep 25 16:02 bin
drwxr-xr-x  3 root root 4.0K Sep 25 16:02 etc
drwxr-xr-x  4 root root 4.0K Sep 25 16:02 extern
drwxr-xr-x  3 root root 4.0K Sep 25 16:02 help
drwxr-xr-x  4 root root 4.0K Sep 25 16:02 java
-r--r--r--  1 root root  72K Jan 15  2012 license.txt
-r--r--r--  1 root root 5.1K Dec 21  2011 MCR_license.txt
-r--r--r--  1 root root 3.9K Jan  5  2012 patents.txt
drwxr-xr-x 64 root root 4.0K Sep 25 16:02 resources
drwxr-xr-x  3 root root 4.0K Sep 25 16:02 runtime
drwxr-xr-x  3 root root 4.0K Sep 25 16:02 simulink
drwxr-xr-x  8 root root 4.0K Sep 25 16:02 sys
drwxr-xr-x 10 root root 4.0K Sep 25 16:02 toolbox
-r--r--r--  1 root root  248 Dec 26  2007 trademarks.txt
drwxr-xr-x  4 root root 4.0K Sep 25 16:02 X11

在安装pg_matlab前, 接下来要修改几个地方, 
1. pg_matlab

root@digoal-PowerEdge-R610-> pwd
/data01/soft_bak/postgresql-9.2.4/contrib/pg_matlab/matcored
root@digoal-PowerEdge-R610-> vi Makefile 
修改
PGM_DIR = /opt/pgsql/bin
LIB_DIR = /opt/pgsql/lib
加一行
MATLABROOT=/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717

root@digoal-PowerEdge-R610-> pwd
/data01/soft_bak/postgresql-9.2.4/contrib/pg_matlab
root@digoal-PowerEdge-R610-> vi Makefile 
修改
MATLABROOT=/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717

2. 修改环境变量
su - pg92
把安装mcr时建议的配置加上去
vi .bash_profile
export LD_LIBRARY_PATH=/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/bin/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/sys/os/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/sys/java/jre/glnxa64/jre/lib/amd64:$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export XAPPLRESDIR=/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/X11/app-defaults

3. 安装pg_matlab
root@digoal-PowerEdge-R610-> make clean
make clean -e -C matcored
make[1]: Entering directory `/data01/soft_bak/postgresql-9.2.4/contrib/pg_matlab/matcored'
rm -f matcored.o mat_wrapper.o matcored
make[1]: Leaving directory `/data01/soft_bak/postgresql-9.2.4/contrib/pg_matlab/matcored'
USE_PGXS=1 make clean -e -C libmatpg
make[1]: Entering directory `/data01/soft_bak/postgresql-9.2.4/contrib/pg_matlab/libmatpg'
rm -f matpg.so   libmatpg.a 
rm -f create_mat_pg_operators.sql
rm -f mat_pg_funcs.o
make[1]: Leaving directory `/data01/soft_bak/postgresql-9.2.4/contrib/pg_matlab/libmatpg'
root@digoal-PowerEdge-R610-> make
make -e -C matcored
make[1]: Entering directory `/data01/soft_bak/postgresql-9.2.4/contrib/pg_matlab/matcored'
cc -Wall -c -o matcored.o matcored.c -I/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/extern/include -I../libmatcore/distrib
cc -Wall -c -o mat_wrapper.o mat_wrapper.c -I/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/extern/include -I../libmatcore/distrib
gcc -o matcored matcored.o mat_wrapper.o -L/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/runtime/glnxa64 -L../libmatcore/distrib -lmwmclmcrrt -lmatcore
make[1]: Leaving directory `/data01/soft_bak/postgresql-9.2.4/contrib/pg_matlab/matcored'
USE_PGXS=1 make -e -C libmatpg
make[1]: Entering directory `/data01/soft_bak/postgresql-9.2.4/contrib/pg_matlab/libmatpg'
sed 's,MODULE_PATHNAME,$libdir/create_mat_pg_operators,g' create_mat_pg_operators.sql.in >create_mat_pg_operators.sql
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -fpic -I../matcored -I. -I. -I/opt/pgsql9.2.4/include/server -I/opt/pgsql9.2.4/include/internal -D_GNU_SOURCE -I/usr/include/libxml2   -c -o mat_pg_funcs.o mat_pg_funcs.c
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -fpic -shared -o matpg.so mat_pg_funcs.o -L/opt/pgsql9.2.4/lib -L/usr/lib/x86_64-linux-gnu  -Wl,--as-needed -Wl,-rpath,'/opt/pgsql9.2.4/lib',--enable-new-dtags  
make[1]: Leaving directory `/data01/soft_bak/postgresql-9.2.4/contrib/pg_matlab/libmatpg'
root@digoal-PowerEdge-R610-> make install
cp libmatcore/distrib/libmatcore.so /opt/pgsql/lib
cp matcored/matcored /opt/pgsql/bin
USE_PGXS=1 make install -e -C libmatpg
make[1]: Entering directory `/data01/soft_bak/postgresql-9.2.4/contrib/pg_matlab/libmatpg'
/bin/mkdir -p '/opt/pgsql9.2.4/lib'
/bin/mkdir -p '/opt/pgsql9.2.4/share/contrib'
/bin/sh /opt/pgsql9.2.4/lib/pgxs/src/makefiles/../../config/install-sh -c -m 755  matpg.so '/opt/pgsql9.2.4/lib/matpg.so'
/bin/sh /opt/pgsql9.2.4/lib/pgxs/src/makefiles/../../config/install-sh -c -m 644  create_mat_pg_operators.sql '/opt/pgsql9.2.4/share/contrib/'
make[1]: Leaving directory `/data01/soft_bak/postgresql-9.2.4/contrib/pg_matlab/libmatpg'


4. 修改ld.so.conf
vi /etc/ld.so.conf
/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/bin/glnxa64
/usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/runtime/glnxa64
执行ldconfig, 同时检查是否生效
root@digoal-PowerEdge-R610-> ldconfig
root@digoal-PowerEdge-R610-> ldconfig -p|grep libmwmclmcrrt
        libmwmclmcrrt.so.7.17 (libc6,x86-64) => /usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/runtime/glnxa64/libmwmclmcrrt.so.7.17
        libmwmclmcrrt.so (libc6,x86-64) => /usr/local/MATLAB/MATLAB_Compiler_Runtime/v717/runtime/glnxa64/libmwmclmcrrt.so


启动matcored
root@digoal-PowerEdge-R610-> /opt/pgsql/bin/matcored 54321
pg92@digoal-PowerEdge-R610-> which matcored
/opt/pgsql/bin/matcored
启动matcored时指定监听端口54321
pg92@digoal-PowerEdge-R610-> matcored 54321
pg92@digoal-PowerEdge-R610-> ps -efw|grep matcored
pg92     13041     1  0 16:32 pts/1    00:00:00 matcored 54321
pg92     13043 12932  0 16:32 pts/1    00:00:00 grep --color=auto matcored
pg92@digoal-PowerEdge-R610-> netstat -anp|grep 54321
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 127.0.0.1:54321         0.0.0.0:*               LISTEN      13041/matcored  


在数据库中创建函数
pg92@digoal-PowerEdge-R610-> cd /data01/soft_bak/postgresql-9.2.4/contrib/pg_matlab/
pg92@digoal-PowerEdge-R610-> cat create_mat_pg_operators.sql 
-- + operator

CREATE OR REPLACE FUNCTION mat_pg_add(DOUBLE PRECISION[], DOUBLE PRECISION[]) RETURNS DOUBLE PRECISION[] 
AS 'libmatpg.so', 'mat_pg_add' 
LANGUAGE C STRICT IMMUTABLE;

DROP OPERATOR + (DOUBLE PRECISION[],DOUBLE PRECISION[]);
CREATE OPERATOR + (
        PROCEDURE = mat_pg_add,
        LEFTARG = DOUBLE PRECISION[],
        RIGHTARG = DOUBLE PRECISION[]
);

报错 : 
pg92@digoal-PowerEdge-R610-> psql -f create_mat_pg_operators.sql 
psql:create_mat_pg_operators.sql:5: ERROR:  could not access file "libmatpg.so": No such file or directory
psql:create_mat_pg_operators.sql:7: ERROR:  operator does not exist: double precision[] + double precision[]
psql:create_mat_pg_operators.sql:12: ERROR:  function mat_pg_add(double precision[], double precision[]) does not exist

这个动态库文件不存在
pg92@digoal-PowerEdge-R610-> ll /opt/pgsql/lib/libmatpg*
ls: cannot access /opt/pgsql/lib/libmatpg*: No such file or directory

解决办法 : 
root@digoal-PowerEdge-R610-> cd /opt/pgsql/lib
root@digoal-PowerEdge-R610-> mv matpg.so libmatpg.so
pg92@digoal-PowerEdge-R610-> psql -f create_mat_pg_operators.sql 
CREATE FUNCTION
psql:create_mat_pg_operators.sql:7: ERROR:  operator does not exist: double precision[] + double precision[]
CREATE OPERATOR


[安装说明]
How to compile, install and run
===============================

Before compilation, you need to set a few environment variables.

- PATH (if needed, if PostgreSQL is installed into a custom directory)
- MATLABROOT, so the Makefiles find the proper include and shared library files
- LD_LIBRARY_PATH or modify /etc/ld.so.conf so the libmatcore.so is found.

The main Makefile has the PGM_DIR and LIB_DIR, matcored and libmatcore.so
are installed there, respectively.

The C header file pg_matlab/libmatpg/mat_pg_funcs.h contains the hardcoded
port number (54321 by default) that the PostgreSQL module will connect to.

After setting all the above, run "make" from the main pg_matlab directory.
Then run "make install", you most likely need to be root to do this, but it
depends on the installation directories.

You need to install the SQL script into the database you want to use Matlab,
replace "dbname" with the proper name in the command below:

$ psql -f libmatpg/create_mat_pg_operators.sql dbname

When it's done, you need to run the installed "matcored" with the following
command:

$ matcored NNN

where NNN is the port number. Use the same port number as you used above.
You should use a special user for running matcored and this user must have
"ulimit -c 0" set, so the core dumps from the failing daemon children don't
fill up the disk.
vi /etc/security/limits.conf
* soft    core    unlimited
* hard    core    unlimited

When matcored is running, you can use commands like these in PostgreSQL
to add two vectors:

SELECT mat_pg_add( '{1,2,3,4}' , '{2,3,4,5}' );

or

SELECT '{1,2,3,4}'::float8[] + '{2,3,4,5}'::float8[];

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
SpringCloudAlibaba 安全 Java
SpringCloud版本升级后bootstrap.yml配置不生效
SpringCloud版本升级后bootstrap.yml配置不生效
2311 1
|
3月前
|
人工智能 数据可视化 数据挖掘
Gemini-2.5-pro大模型专业画常用图表保姆级教程:圆角环形图+柱状图排序图+系列按行和按列排布图,国内直接使用
虚竹哥推出AI数据可视化工具《xuzhu-huatu-plus》,支持多种图表类型,如柱状图、饼图、热力图等,输入指令即可生成图表代码,大幅简化绘图流程。用户仅需访问指定平台,选择智能体并输入需求,AI即自动完成图表设计,助力高效数据分析与展示。提供免费授权码体验,国内可直接使用。
134 9
|
JSON JavaScript API
Node.js(nodejs)对本地JSON文件进行增、删、改、查操作(轻车熟路)
Node.js(nodejs)对本地JSON文件进行增、删、改、查操作(轻车熟路)
|
存储 固态存储 关系型数据库
|
4月前
|
SQL 分布式计算 API
Apache Spark详解
Apache Spark 是一个开源、分布式计算引擎,专为大规模数据处理设计。它以高速、易用和通用为核心目标。通过内存计算、DAG 执行引擎和惰性求值等特性,大幅提升数据处理效率。其核心组件包括 Spark Core、Spark SQL、Spark Streaming、MLlib 和 GraphX,支持批处理、实时流处理、机器学习和图计算。Spark 提供统一编程模型,支持多语言(Scala/Java/Python/R),并拥有强大的 Catalyst 优化器和类型安全的 Dataset API,广泛应用于大数据分析和处理场景。
|
4月前
|
SQL 存储 关系型数据库
PostgreSQL窗口函数避坑指南:如何让复杂分析查询提速300%?
本文基于真实企业级案例,深入剖析PostgreSQL窗口函数的执行原理与性能陷阱,提供8大优化策略。通过定制索引、分区裁剪、内存调优及并行处理等手段,将分钟级查询压缩至秒级响应。结合CTE分阶段计算与物化视图技术,解决海量数据分析中的瓶颈问题。某金融客户实践表明,风险分析查询从47秒降至0.8秒,效率提升5800%。文章附带代码均在PostgreSQL 15中验证,助您高效优化SQL性能。
197 0
|
7月前
|
NoSQL MongoDB 微服务
微服务——MongoDB常用命令——文档的分页查询
本文介绍了文档分页查询的相关内容,包括统计查询、分页列表查询和排序查询。统计查询使用 `count()` 方法获取记录总数或按条件统计;分页查询通过 `limit()` 和 `skip()` 方法实现,控制返回和跳过的数据量;排序查询利用 `sort()` 方法,按指定字段升序(1)或降序(-1)排列。同时提示,`skip()`、`limit()` 和 `sort()` 的执行顺序与编写顺序无关,优先级为 `sort()` > `skip()` > `limit()`。
250 1
|
7月前
|
JSON NoSQL MongoDB
微服务——MongoDB常用命令——文档基本CRUD
本文介绍了MongoDB中文档的基本操作,包括插入、查询、更新和删除。单个文档插入使用`insert()`或`save()`方法,批量插入用`insertMany()`。查询所有文档用`find()`,条件查询可在`find()`中添加参数,投影查询控制返回字段。更新文档通过`update()`实现,支持覆盖修改、局部修改(使用`$set`)和批量修改。列值增长可用`$inc`实现。删除文档用`remove()`,需谨慎操作以免误删数据。此外,文档键值对有序,区分大小写,不能有重复键。
133 1
|
9月前
|
存储 监控 NoSQL
【赵渝强老师】MongoDB文档级别的并发控制
MongoDB使用WiredTiger存储引擎在文档级别进行并发控制,允许多个写操作同时修改不同文档,但对同一文档的修改需序列化执行。引擎采用乐观锁和意向锁机制处理冲突。通过视频讲解、插入大量文档示例及使用`mongotop`和`db.serverStatus()`命令,演示了如何监控MongoDB的锁信息和读写统计,展示了数据库和集合级别的写锁情况。
235 29
|
负载均衡 Cloud Native 容灾
阿里云负载均衡SLB价格_ALB、NLB和CLB区别_负载均衡功能和使用场景说明
阿里云负载均衡SLB分为应用型ALB、网络型NLB及传统型CLB。ALB与NLB仅支持按量付费,而CLB则提供包年包月和按量付费选项。ALB专长于7层HTTP/HTTPS/QUIC协议处理,支持丰富的内容路由功能;NLB聚焦于4层TCP/UDP/TCPSSL协议,擅长处理大规模并发连接。两者均基于NFV技术,支持自动弹性伸缩,并与云原生环境如ACK/SAE/K8S深度集成。此外,SLB提供多协议支持、多级容灾、安全防护等功能,确保服务的高可用性和安全性。具体收费方面,ALB的基础版实例费为0.049元/小时起,NLB实例费限时免费,两者还需支付性能容量单位LCU费及公网网络费(仅公网实例)