rethinkdb入门(python/node/php)

1 安装

获取镜像

docker pull rethinkdb

后台启动

docker run --name my-rethinkdb -d -p 18080:8080 -p 28015:28015 -p 29015:29015 rethinkdb

查看启动情况

docker ps | grep "my-rethinkdb"
a3b5d625a19f        rethinkdb           "rethinkdb --bind all"   32 seconds ago      Up 30 seconds       0.0.0.0:28015->28015/tcp, 0.0.0.0:29015->29015/tcp, 0.0.0.0:18080->8080/tcp                                                my-rethinkdb

http访问

rethinkdb入门(python/node/php)_第1张图片
image.png

2 客户端操作

python

# Python 3
$ python3 -m venv ./venv
$ source venv/bin/activate
$ pip install rethinkdb

代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from rethinkdb import r

# 连接server
r.connect('localhost', 28015).repl()
# 创建
r.db('test').table_create('table_python_test').run()
# 新增
r.table('table_python_test').insert({ 'name': '王五' }).run()
# 打印
cursor = r.table('table_python_test').run()
for doc in cursor:
    print(doc)

输出

image.png

node

安装rethinkdb

npm install rethinkdb

示例代码:

r = require('rethinkdb')

// 连接server
r.connect({host: 'localhost', port: 28015}, function (err, conn) {
    if (err) throw err;
    
    // 创建集合
    r.db('test').tableCreate('table_js_test').run(conn, function (err, res) {
        if (err) throw err;
        console.log(res);
        
        // 插入
        r.table('table_js_test').insert({name: '李四'}).run(conn, function (err, res) {
            if (err) throw err;
            console.log(res);
        });
    });
});

执行结果

rethinkdb入门(python/node/php)_第2张图片
image.png

php

安装:

composer require danielmewes/php-rql

代码

tableCreate("table_php_test")->run($conn);

// 插入文档
$document = array('name' => '张三');
$result = r\table("table_php_test")->insert($document)
    ->run($conn);
echo "Insert result:\n";
print_r($result);

// 获取count
$result = r\table("table_php_test")->count()->run($conn);
echo "Count: $result\n";

// 获取列表
$result = r\table("table_php_test")->map(function($x) {
    return $x('name');
})->run($conn);

foreach ($result as $doc) {
    print_r($doc);
}

// 删除表
//r\db("test")->tableDrop("table_php_test")->run($conn);

执行结果

rethinkdb入门(python/node/php)_第3张图片
image.png

3 查看

这里执行了3个语言的脚本,创建了3张表.

rethinkdb入门(python/node/php)_第4张图片
image.png

这里我们使用界面方式查询一条刚才的数据

rethinkdb入门(python/node/php)_第5张图片
image.png

你可能感兴趣的:(rethinkdb入门(python/node/php))