Postgresql - Foreign data wrappers - ElasticSearch

Postgresql的Foreign data wrapper不仅可以读取数据库,还可以读取文件系统,大数据存储等。

这一篇我们看一下PG该如何建立ES的外部表。

插件代码

https://github.com/matthewfranglen/postgres-elasticsearch-fdw

 

安装

# git clone https://github.com/Kozea/Multicorn /tmp/multicorn # cd Multicorn # make install

 

# git clone https://github.com/matthewfranglen/postgres-elasticsearch-fdw.git

# cd postgres-elasticsearch-fdw/

# python setup.py install

 

使用

1. create extension

mytest=# create extension multicorn ;

CREATE EXTENSION

 

2. Create Server

mytest=# create server pg_es_server foreign data wrapper multicorn options( wrapper 'pg_es_fdw.ElasticsearchFDW' );

CREATE SERVER

 

3. Create Foreign Table

mytest=# CREATE FOREIGN TABLE test_es

mytest-# (

mytest(# id BIGINT,

mytest(# name TEXT,

mytest(# age TEXT,

mytest(# tel numeric(11),

mytest(# query TEXT,

mytest(# score NUMERIC

mytest(# )

mytest-# SERVER multicorn_es

mytest-# OPTIONS

mytest-# (

mytest(# host 'localhost',

mytest(# port '9200',

mytest(# index 'test',

mytest(# type 'test',

mytest(# rowid_column 'id',

mytest(# query_column 'query',

mytest(# score_column 'score'

mytest(# );

CREATE FOREIGN TABLE

 

4. Insert Data

mytest=# INSERT INTO test_es

mytest-# (

mytest(# id,

mytest(# name,

mytest(# age,

mytest(# tel

mytest(# )

mytest-# VALUES

mytest-# (

mytest(# 1,

mytest(# 'chen',

mytest(# '1',

mytest(# '13812345678'

mytest(# );

INSERT 0 1

 

5. 从ES中查看数据

# curl -H "Content-Type: application/json" -XPOST 'localhost:9200/test/_search?pretty'

{

"took" : 3,

"timed_out" : false,

"_shards" : {

"total" : 5,

"successful" : 5,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : 1,

"max_score" : 1.0,

"hits" : [

{

"_index" : "test",

"_type" : "test",

"_id" : "1",

"_score" : 1.0,

"_source" : {

"tel" : 1.3812345678E10,

"name" : "chen",

"age" : "1",

"score" : null,

"query" : null

}

}

]

}

}

 

6. 从ES中插入数据

# curl -H "Content-Type: application/json" -XPUT 'localhost:9200/test/test/2?pretty' -d '{"name":"chen", "age": 2, "tel":"13812345678"}'

{

"_index" : "test",

"_type" : "test",

"_id" : "2",

"_version" : 1,

"result" : "created",

"_shards" : {

"total" : 2,

"successful" : 1,

"failed" : 0

},

"_seq_no" : 0,

"_primary_term" : 1

}

 

7. 查看 ES和PG

# curl -H "Content-Type: application/json" -XPOST 'localhost:9200/test/_search?pretty'

{

"took" : 5,

"timed_out" : false,

"_shards" : {

"total" : 5,

"successful" : 5,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : 2,

"max_score" : 1.0,

"hits" : [

{

"_index" : "test",

"_type" : "test",

"_id" : "2",

"_score" : 1.0,

"_source" : {

"name" : "chen",

"age" : 2,

"tel" : "13812345678"

}

},

{

"_index" : "test",

"_type" : "test",

"_id" : "1",

"_score" : 1.0,

"_source" : {

"tel" : 1.3812345678E10,

"name" : "chen",

"age" : "29",

"score" : null,

"query" : null

}

}

]

}

}

 

mytest=# select * from test_es;

id | name | age | tel | query | score

----+------+-----+-------------+-------+-------

2 | chen | 2 | 13812345678 | | 1.0

1 | chen | 1 | 13812345678 | | 1.0

(2 rows)

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值