1.查看PostgreSQL表结构和数据信息

edbstore=# \d customers
Table "edbstore.customers"
Column | Type | Modifiers
----------------------+-----------------------+----------------------------------------------------------------
customerid | integer | not null default nextval('customers_customerid_seq'::regclass)
firstname | character varying(50) | not null
lastname | character varying(50) | not null
address1 | character varying(50) | not null
address2 | character varying(50) |
city | character varying(50) | not null
state | character varying(50) |
zip | integer |
country | character varying(50) | not null
region | smallint | not null
email | character varying(50) |
phone | character varying(50) |
creditcardtype | integer | not null
creditcard | character varying(50) | not null
creditcardexpiration | character varying(50) | not null
username | character varying(50) | not null
password | character varying(50) | not null
age | smallint |
income | integer |
gender | character varying(1) |
Indexes:
"customers_pkey" PRIMARY KEY, btree (customerid)
"ix_cust_username" UNIQUE, btree (username)
Referenced by:
TABLE "cust_hist" CONSTRAINT "fk_cust_hist_customerid" FOREIGN KEY (customerid) REFERENCES customers(customerid) ON DELETE CASCADE
TABLE "orders" CONSTRAINT "fk_customerid" FOREIGN KEY (customerid) REFERENCES customers(customerid) ON DELETE SET NULL edbstore=# select count(1) from customers;
count
-------
20000
(1 row)

2.利用PostgreSQL的row_to_json函数将表结构导出并保存为json格式

edbstore=# \t
Tuples only is on.
edbstore=# \o customer.json
edbstore=# select row_to_json(r) from customers as r;
edbstore=# \q [postgres@sht-sgmhadoopcm-01 dba]$ ls -lh customer.json
-rw-r--r-- 1 postgres appuser 7.7M Dec 7 22:37 customer.json $ head -1 customer.json
{"customerid":1,"firstname":"VKUUXF","lastname":"ITHOMQJNYX","address1":"4608499546 Dell Way","address2":null,"city":"QSDPAGD","state":"SD","zip":24101,"country":"US","region":1,"email":"ITHOMQJNYX@dell.com","phone":"4608499546","creditcardtype":1,"creditcard":"1979279217775911","creditcardexpiration":"2012/03","username":"user1","password":"password","age":55,"income":100000,"gender":"M"}

此时customer表虽然转储为json格式文件,但是并不能直接导入到elasticsearch,否则会报错如下

$ curl -H "Content-Type: application/json" -XPOST "172.16.101.55:9200/bank/_bulk?pretty&refresh" --data-binary "@customer.json"
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "Malformed action/metadata line [1], expected START_OBJECT or END_OBJECT but found [VALUE_NUMBER]"
}
],
"type" : "illegal_argument_exception",
"reason" : "Malformed action/metadata line [], expected START_OBJECT or END_OBJECT but found [VALUE_NUMBER]"
},
"status" : 400
}

根据文档https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html说明,我们的json数据里并未指定每行数据唯一的文档id值

3.为json格式的表数据添加id字段

因为之前我们看到该customer表共有2000行,所以我们需要生成对应的20000个id值,我们借助python实现,新建build_id.py文件,并写入如下内容,看清楚是20001,因为包头不包尾原则,1-20000实际打印出来是1-19999,所以我们写1-20001

for i in range(1,20001):
print('{"index":{"_id":"%s"}}' %i ) 

为该文件添加可执行权限,然后执行即可

$ python build_id.py > build_id.txt

$ head -3 build_id.txt
{"index":{"_id":""}}
{"index":{"_id":""}}
{"index":{"_id":""}}

利用linux “paste"命令,将id文件和表文件合并

$ paste -d'\n' build_id.txt customer.json > customer_new.json

$ head - customer_new.json
{"index":{"_id":""}}
{"customerid":,"firstname":"VKUUXF","lastname":"ITHOMQJNYX","address1":"4608499546 Dell Way","address2":null,"city":"QSDPAGD","state":"SD","zip":,"country":"US","region":,"email":"ITHOMQJNYX@dell.com","phone":"","creditcardtype":,"creditcard":"","creditcardexpiration":"2012/03","username":"user1","password":"password","age":,"income":,"gender":"M"}
{"index":{"_id":""}}
{"customerid":,"firstname":"HQNMZH","lastname":"UNUKXHJVXB","address1":"5119315633 Dell Way","address2":null,"city":"YNCERXJ","state":"AZ","zip":,"country":"US","region":,"email":"UNUKXHJVXB@dell.com","phone":"","creditcardtype":,"creditcard":"","creditcardexpiration":"2012/11","username":"user2","password":"password","age":,"income":,"gender":"M"}

4.此时处理过的json格式的表文件就可以正常导入到elasticsearch中了,测试

$ curl -H "Content-Type: application/json" -XPOST "172.16.101.55:9200/customer/_bulk?pretty&refresh" --data-binary "@customer_new.json"
$ curl http://172.16.101.55:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open customer DvLoM7NjSYyjTwD5BSkK3A 10mb 10mb

将PostgreSQL数据库的表导入到elasticsearch中的更多相关文章

  1. 使用Logstash把MySQL数据导入到Elasticsearch中

    总结:这种适合把已有的MySQL数据导入到Elasticsearch中 有一个csv文件,把里面的数据通过Navicat Premium 软件导入到数据表中,共有998条数据 文件下载地址:https ...

  2. oracle 表导入到powerDesigner 中

    最近不忙,之前一直是用powerDesigner看表结构,还没自己导入过,今天试试 oracle 表导入到powerDesigner 中步骤: 1.File--->reverse Enginne ...

  3. Logstash:把MySQL数据导入到Elasticsearch中

    Logstash:把MySQL数据导入到Elasticsearch中 前提条件 需要安装好Elasticsearch及Kibana. MySQL安装 根据不同的操作系统我们分别对MySQL进行安装.我 ...

  4. sqoop将关系型的数据库得数据导入到hbase中

    1.sqoop将关系数据库导入到hbase的参数说明

  5. Logstash:解析 JSON 文件并导入到 Elasticsearch 中

    转载自:https://elasticstack.blog.csdn.net/article/details/114383426 在今天的文章中,我们将详述如何使用 Logstash 来解析 JSON ...

  6. sqoop将关系型数据库的表导入hive中

    1.sqoop 将关系型数据库的数据导入hive的参数说明:

  7. pg_dumpall - 抽出一个 PostgreSQL 数据库集群到脚本文件中

    SYNOPSIS pg_dumpall [ option...] DESCRIPTION 描述 pg_dumpall 是一个用于写出("转储")一个数据库集群里的所有 Postgr ...

  8. 将Mongodb的表导入到Hive中

    1.官方文档:https://docs.mongodb.com/ecosystem/tools/hadoop/ 2.Hive介绍: Hive特点: 1.hive是一个数据仓库,和oracle,mysq ...

  9. 如何将数据库中的表导入到PowerDesigner中

    1.        打开PowerDesigner12,在菜单中按照如下方式进行操作file->Reverse Engineer->DataBase 点击后,弹出 New Physical ...

随机推荐

  1. Swoole:PHP7安装Swoole的步骤

    下载 swoole 首先下载swoole的源码包,这个操作很简单,没有太多说的.(没有wget:brew install wget--mac) wget -c https://github.com/s ...

  2. [Python]token的生成及验证

    hmac模块(仅在python3中可以使用) 简介: HMAC是密钥相关的哈希运算消息认证码,HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出. 典型应用: HMAC的一 ...

  3. URAL 2092 Bolero 贪心

    C - Bolero Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit S ...

  4. cogs1355. 读书

    1355. 读书 ★   输入文件:reading.in   输出文件:reading.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述] 放暑假了,CHH想趁假期提高一下 ...

  5. 关于windows 设备驱动重要的事实

    1. windows采用设备树描述所有挂在系统总线上的设备,每个设备对应一个节点. 2.每个设备有自己的device object stack/driver stack. 一个物理上的设备对应多个de ...

  6. windows 安装多个版本的jdk后修改 环境变量不起作用

    本机已经安装了jdk1.6,而比较早期的项目需要依赖jdk1.5,于是同时在本机安装了jdk1.5和jdk1.6. 安装jdk1.5前,执行java -version得到java version &q ...

  7. ZooKeeper分布式锁的实现原理

    七张图彻底讲清楚ZooKeeper分布式锁的实现原理[石杉的架构笔记] 文章转载自:https://juejin.im/post/5c01532ef265da61362232ed#comment(写的 ...

  8. Bug集锦-Spring Cloud Feign调用其它接口报错

    问题描述 Spring Cloud Feign调用其它服务报错,错误提示如下:Failed to instantiate [java.util.List]: Specified class is an ...

  9. MySort作业与IO-Myhead

    一.MySort 1.题目要求 模拟实现Linux下Sort -t : -k 2的功能. 要有伪代码,产品代码,测试代码(注意测试用例的设计) 参考 Sort的实现.提交博客链接. 2.产品代码 im ...

  10. android canvas drawtext 字高

    Paint pFont = new Paint(); Rect rect = new Rect(); pFont.getTextBounds("豆", 0, 1, rect); L ...