本文经授权转载自 PostgreSQL 中文社区。
Kafka 是分布式消息订阅系统,有非常好的横向扩展性,可实时存储海量数据,是流数据处理中间件的事实标准。当通过 Kafka 和 greenplum 搭建流处理管道时,如何高速可靠的完成流数据加载,成为用户最关心的问题。从 5.10 开始,Greenplum 发布了新的工具 GPKafka,为 Greenplum 提供了流数据加载的能力。
GPkafka 工具:kafka —> Greenplum
一、安装准备
kafka 安装:版本为 kafka_2.11-2.1.0。
greenplum 安装:版本为 5.16
二、Kafka 数据导入 GreenPlum
启动 kafka
# 启动zookeeper
$ /opt/zookeeper-3.4.12/bin/zkServer.sh start
# 启动kafka
$/opt/kafka/kafka_2.11-2.1.0/bin/kafka-server-start.sh -daemon ../config/server.properties
2.创建 gpss 扩展
在将 Kafka 消息数据加载到 Greenplum 数据库之前,必须在将 Kafka 数据写入 Greenplum 表的每个数据库中注册 Greenplum-Kafka 集成格式化程序函数;示例在 lottu 数据库
[gpadmin@oracle166 ~]$ psql
psql (8.3.23)
Type "help" for help.
lottu=# CREATE EXTENSION gpss;
3.创建示例表
kafka 的数据格式 json 形式;样式:
{
"time": 1550198435941,
"type": "type_mobileinfo",
"phone_imei": "861738033581011",
"phone_imsi": "",
"phone_mac": "00:27:1c:95:47:09",
"appkey": "307A5C626E6C2F6472636E6E6A2F736460656473",
"phone_udid": "8F137BFFB2289784A5EA2DCADCE519C2",
"phone_udid2": "744DD04CE29652F4F1D2DFFC8D3204A9",
"appUdid": "D21C76419E54B18DDBB94BF2E6990183",
"phone_resolution": "1280*720",
"phone_apn": "",
"phone_model": "BF T26",
"phone_firmware_version": "5.1",
"phone_softversion": "3.19.0",
"phone_softname": "com.esbook.reader",
"sdk_version": "3.1.8",
"cpid": "blp1375_13621_001",
"currentnetworktype": "wifi",
"phone_city": "",
"os": "android",
"install_path": "\/data\/app\/com.esbook.reader-1\/base.apk",
"last_cpid": "",
"package_name": "com.esbook.reader",
"src_code": "WIFIMAC:00:27:1c:95:47:09"
}
我需要其中的 package_name,appkey ,time, phone_udid,os, idfa,phone_imei,cpid,last_cpid,phone_number 字段;所以我创建的表语句
CREATE TABLE tbl_novel_mobile_log (
package_name text,
appkey text,
ts bigint,
phone_udid text,
os character varying(20),
idfa character varying(64),
phone_imei character varying(20),
cpid text,
last_cpid text,
phone_number character varying(20)
) ;
4.创建 gpkafka.yaml 配置文件
gpkafka_mobile_yaml文件内容:
DATABASE: lottu
USER: gpadmin
HOST: oracle166
PORT: 5432
KAFKA:
INPUT:
SOURCE:
BROKERS: kafkaip:9092
TOPIC: mobile_info
COLUMNS:
- NAME: jdata
TYPE: json
FORMAT: json
ERROR_LIMIT: 10
OUTPUT:
TABLE: tbl_novel_mobile_log
MAPPING:
- NAME: package_name
EXPRESSION: (jdata->>'package_name')::text
- NAME: appkey
EXPRESSION: (jdata->>'appkey')::text
- NAME: ts
EXPRESSION: (jdata->>'time')::bigint
- NAME: phone_udid
EXPRESSION: (jdata->>'phone_udid')::text
- NAME: os
EXPRESSION: (jdata->>'os')::text
- NAME: idfa
EXPRESSION: (jdata->>'idfa')::text
- NAME: phone_imei
EXPRESSION: (jdata->>'phone_imei')::text
- NAME: cpid
EXPRESSION: (jdata->>'cpid')::text
- NAME: last_cpid
EXPRESSION: (jdata->>'last_cpid')::text
- NAME: phone_number
EXPRESSION: (jdata->>'phone_number')::text
COMMIT:
MAX_ROW: 1000
5.创建 mobile_info topic
/opt/kafka/kafka_2.11-2.1.0/bin/kafka-topics.sh --create --zookeeper kafkaIp:2181 --replication-factor 1 --partitions 1 --topic mobile_info
6.创建 kafka 的发布者
执行下列命令;并添加 kafka 记录
[root@oracle166 ~]# /opt/kafka/kafka_2.11-2.1.0/bin/kafka-console-producer.sh --broker-list kafkaIP:9092 --topic mobile_info
>{"time":1550198435941,"type":"type_mobileinfo","phone_imei":"861738033581011","phone_imsi":"","phone_mac":"00:27:1c:95:47:09","appkey":"307A5C626E6C2F6472636E6E6A2F736460656473","phone_udid":"8F137BFFB2289784A5EA2DCADCE519C2","phone_udid2":"744DD04CE29652F4F1D2DFFC8D3204A9","appUdid":"D21C76419E54B18DDBB94BF2E6990183","phone_resolution":"1280*720","phone_apn":"","phone_model":"BFT26","phone_firmware_version":"5.1","phone_softversion":"3.19.0","phone_softname":"com.esbook.reader","sdk_version":"3.1.8","cpid":"blp1375_13621_001","currentnetworktype":"wifi","phone_city":"","os":"android","install_path":"\/data\/app\/com.esbook.reader1\/base.apk","last_cpid":"","package_name":"com.esbook.reader","src_code":"WIFIMAC:00:27:1c:95:47:09"}
{"time":1550198437885,"type":"type_mobileinfo","phone_imei":"862245038046551","phone_imsi":"","phone_mac":"02:00:00:00:00:00","appkey":"307A5C626F2F76646B74606F2F736460656473","phone_udid":"A3BB70A0218AEFC7908B1D79C0C02D77","phone_udid2":"E3976E0453010FC7F32B6143AA3A164E","appUdid":"4FBEF77BC076254ED0407CAD653E6954","phone_resolution":"1920*1080","phone_apn":"","phone_model":"LeX620","phone_firmware_version":"6.0","phone_softversion":"1.9.0","phone_softname":"cn.wejuan.reader","sdk_version":"3.1.8","cpid":"blf1298_14411_001","currentnetworktype":"wifi","phone_city":"","os":"android","install_path":"\/data\/app\/cn.wejuan.reader1\/base.apk","last_cpid":"","package_name":"cn.wejuan.reader","src_code":"ffffffff-9063-8e34-0000-00007efffeff"}
{"time":1550198438311,"type":"type_mobileinfo","phone_number":"","phone_imei":"867520045576831","phone_imsi":"460001122544742","phone_mac":"02:00:00:00:00:00","appkey":"307A5C626E6C2F6472636E6E6A2F736460656473","phone_udid":"A00407EF9D6EBCC207A514CDA452EB76","phone_udid2":"A00407EF9D6EBCC207A514CDA452EB76","appUdid":"1C35633F4EB8218789EFD8666C763485","phone_resolution":"2086*1080","phone_apn":"CMCC","phone_model":"ONEPLUSA6000","phone_firmware_version":"9","phone_softversion":"3.19.0","phone_softname":"com.esbook.reader","sdk_version":"3.1.8","cpid":"blf1298_12242_001","currentnetworktype":"4gnet","phone_city":"","os":"android","install_path":"\/data\/app\/com.esbook.readerTlgFCk6ANgEDRnXDCem8uQ==\/base.apk","last_cpid":"","package_name":"com.esbook.reader","src_code":"460001122544742"}
{"time":1550198433102,"type":"type_mobileinfo","phone_number":"15077113477","phone_imei":"860364049874919","phone_imsi":"460023771256711","phone_mac":"02:00:00:00:00:00","appkey":"307A5C626E6C2F6472636E6E6A2F736460656473","phone_udid":"EEF566CB5253AA62B653347A203815C3","phone_udid2":"0845931539AE39B3B0D4EB42B85D98EC","appUdid":"9570DCA2D574E6B69B24137035209D42","phone_resolution":"2340*1080","phone_apn":"CHINAMOBILE","phone_model":"PBEM00","phone_firmware_version":"8.1.0","phone_softversion":"3.19.0","phone_softname":"com.esbook.reader","sdk_version":"3.1.8","cpid":"blf1298_12242_001","currentnetworktype":"4gnet","phone_city":"","os":"android","install_path":"\/data\/app\/com.esbook.readerNBToXQo14TOeNuPxo_aA4w==\/base.apk","last_cpid":"","package_name":"com.esbook.reader","src_code":"13598c2d-efc4-4957-8d4d-22eb145d15fd"}
{"time":1550198440577,"type":"type_mobileinfo","phone_imei":"869800021106037","phone_imsi":"","phone_mac":"2c:5b:b8:fb:79:af","appkey":"307A5C626E6C2F6472636E6E6A2F736460656473","phone_udid":"2BC16C4AC07070BA9608BBD0EE2EE320","phone_udid2":"A7F9FA4772D31FADEECFDB445BA3BEBB","appUdid":"DC6BEE2F6E5D6A133E26131887AE788A","phone_resolution":"960*540","phone_apn":"","phone_model":"OPPOA33","phone_firmware_version":"5.1.1","phone_softversion":"3.19.0","phone_softname":"com.esbook.reader","sdk_version":"3.1.8","cpid":"blp1375_14526_003","currentnetworktype":"wifi","phone_city":"","os":"android","install_path":"\/data\/app\/com.esbook.reader1\/base.apk","last_cpid":"","package_name":"com.esbook.reader","src_code":"WIFIMAC:2c:5b:b8:fb:79:af"}
{"time":1506944701166,"type":"type_mobileinfo","phone_number":"+8618602699126","phone_imei":"865902038154143","phone_imsi":"460012690618403","phone_mac":"02:00:00:00:00:00","appkey":"307A5C626E6C2F6472636E6E6A2F736460656473","phone_udid":"388015DA70C0AEA6D59D3CE37B0C4BA2","phone_udid2":"388015DA70C0AEA6D59D3CE37B0C4BA2","appUdid":"EC0A105297D55075526018078A4A1B84","phone_resolution":"1920*1080","phone_apn":"中国联通","phone_model":"MIMAX2","phone_firmware_version":"7.1.1","phone_softversion":"3.19.0","phone_softname":"com.esbook.reader","sdk_version":"3.1.8","cpid":"blf1298_10928_001","currentnetworktype":"wifi","phone_city":"","os":"android","install_path":"\/data\/app\/com.esbook.reader1\/base.apk","last_cpid":"","package_name":"com.esbook.reader","src_code":"460012690618403"}
7.执行 gpkafka 加载数据
[gpadmin@oracle166 ~]$ gpkafka load --quit-at-eof ./gpkafka_mobile_yaml
PartitionID StartTime EndTime BeginOffset EndOffset
0 2019-02-27T09:26:27.989312Z 2019-02-27T09:26:27.99517Z 0 5
Job dcd0d159282c0ef39f182cabeef23ee6 stopped normally at 2019-02-27 09:26:29.442874281 +0000 UTC
8.检查加载操作的进度(非必要)
[gpadmin@oracle166 ~]$ gpkafka check ./gpkafka_mobile_yaml
PartitionID StartTime EndTime BeginOffset EndOffset
0 2019-02-27T09:26:27.989312Z 2019-02-27T09:26:27.99517Z 0 5
9.查看表数据
[gpadmin@oracle166 ~]$ psql
psql (8.3.23)
Type "help" for help.
lottu=# select * from tbl_novel_mobile_log ;
package_name | appkey | ts | phone_udid | os | idfa | phone_imei | cpid | last_cpid | p
hone_number
-------------------+------------------------------------------+---------------+----------------------------------+---------+------+-----------------+--
com.esbook.reader | 307A5C626E6C2F6472636E6E6A2F736460656473 | 1550198435941 | 8F137BFFB2289784A5EA2DCADCE519C2 | android | | 861738033581011 | blp1375_13621_001 | |
com.esbook.reader | 307A5C626E6C2F6472636E6E6A2F736460656473 | 1550198438311 | A00407EF9D6EBCC207A514CDA452EB76 | android | | 867520045576831 | blf1298_12242_001 | |
com.esbook.reader | 307A5C626E6C2F6472636E6E6A2F736460656473 | 1550198433102 | EEF566CB5253AA62B653347A203815C3 | android | | 860364049874919 | blf1298_12242_001 | | 1
5077113477
com.esbook.reader | 307A5C626E6C2F6472636E6E6A2F736460656473 | 1550198440577 | 2BC16C4AC07070BA9608BBD0EE2EE320 | android | | 869800021106037 | blp1375_14526_003 | |
cn.wejuan.reader | 307A5C626F2F76646B74606F2F736460656473 | 1550198437885 | A3BB70A0218AEFC7908B1D79C0C02D77 | android | | 862245038046551 | blf1298_14411_001 | |
(5 rows)
三、后记
编辑本文初衷是:公司计划为北京 ES 小说作投放计划,需要类似热云数据平台作为投放数据支持,使投放更加精准可靠。北京小说部门数据存放于 kafka 中,需要将 kafka 中的数据导入深圳后台数据库中,虽然最后平台未采用 gpkafka 方式,但不失一种方案,由于种种原因后台数据库选 PG9.6 版本,采用 java 代码实现 kafka 数据实时导入 PG。最后祝 PG,GP 越来越好,也期待 pgkafka 工具诞生。
四、参考文献
1、gpkafka 更多用法
https://gpdb.docs.pivotal.io/5120/greenplum-kafka/intro.html
2、BottledWater-PG:PostgreSQL 集成 Kafka 的实时数据交换平台
https://www.jianshu.com/p/c3659f49bf94
作者介绍:
Lottu,就职于深圳宜搜科技有限公司,担任数据库 DBA,主要承担 PostgreSQL、Oracle 数据库维护工作以及数据库去 O 工作。
原文链接:
https://mp.weixin.qq.com/s/HuYYvKtV8RfNxtrrJwR6SQ
更多内容推荐
Kafka- 详细笔记
Kafka-详细笔记,浓缩精华
2021-05-23
【工具安装】logstash 的安装
Logstash 它是 ES 下的一款开源软件,它能够同时从多个来源采集数据、转换数据,然后将数据发送到 Eleasticsearch 中创建索引。我们在项目中通常使用 Logstash 将数据库(如 MySQL)中的数据采用到ES索引中。也就是Logstash的工作是从MySQL中读取数据,向ES中
2022-12-10
加餐 05|分布式微服务与智能 SaaS
分布式微服务技术是怎么演进出来的?
2023-01-16
Neat Syntax Design of an ETL Language (Part 2)
How to design a neat ETL programming language that people like to use? Let’s have a look at how Easy SQL does. (This topic is broken into two parts. This is the second part.)
2022-06-25
kafka 核心技术与实战学习笔记(一)
kafka核心技术与实战 学习笔记
2021-08-29
Spring 源码解析 (四)Spring 是怎么处理 BeanDefinition 的?
作者:石臻臻,CSDN博客之星Top5、Kafka Contributor、nacos Contributor、华为云 MVP,腾讯云TVP,滴滴Kafka技术专家、 KnowStreaming。
2022-08-30
Day433
每一个要使用分布式事务的数据库都需要一个 UNDO_LOG 表。
2022-05-08
148|云数据库:将线下数据库同步至线上
2020-12-21
大牛呕心力作——Kafka 开发实战,助你徜徉大数据时代
前言
2021-11-04
12. RocketMQ 发送消息之普通消息
2023-02-16
《零基础》MySQL 连接的使用 (二十),springcloud 开发教程
我们在RUNOOB数据库中有两张表 tcount_tbl 和 runoob_tbl。两张数据表数据如下:
2021-11-10
4 月 22 日,云数据库技术沙龙【杭州站】
4月22日,云数据库技术主办的「MySQL x ClickHouse」技术沙龙将在杭州举办。本次沙龙以“技术进化,让数据更智能”为主题,汇聚字节跳动、阿里云、玖章算术、华为云、腾讯云等众多数据库厂商,围绕MySQL x ClickHouse的实践经验,与广大技术爱好者交流分享。
2023-04-13
19|全栈项目搭建:如何搭建 Vue.js 的前后台全栈项目?
全栈项目的搭建是没有标准答案的,都是根据实际项目情况因地制宜做方案设计的。
2023-01-06
【kafka 运维】TopicCommand 运维脚本 (1)
TopicCommand 1.Topic创建 bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 3 --topic test
2022-10-23
Spark Connecter:MySQL 及 Kafka
2020-12-17
BAT 面试必考 Java 面试题 100+:Kafka,mysql 连接查询原理
Kafka面试答案解析
2021-11-09
数据库在各行各业的应用实践
如今,全社会驶入数字化转型快车道,数字生态正在推动着千行百业的变革。作为数字化转型的基石,数据库的重要性不言而喻。数据库更是成为企业的核心资产,运转和增长的驱动引擎,开发、运维等人员几乎每天都在和数据库接触。那么,在数据规模不断增长的今天,
2021-06-21
【kafka 运维】TopicCommand 运维脚本
TopicCommand 1.Topic创建 bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 3 --topic test
2022-11-05
20|数据库方案设计:如何设计运营搭建平台的数据库?
数据库的设计范式,就是为了让数据的关系清晰,结构精简和减少冗余。不过,实际项目的数据库设计,与范式之间会有一些矛盾。
2023-01-09
三天吃透 Kafka 面试八股文
本文已经收录到Github仓库,该仓库包含计算机基础、Java基础、多线程、JVM、数据库、Redis、Spring、Mybatis、SpringMVC、SpringBoot、分布式、微服务、设计模式、架构、校招社招分享等核心知识点,欢迎star~
2023-03-09
推荐阅读
Milvus 再上新!支持 Upsert、Kafka Connector、集成 Airbyte,助力高效数据流处理
2023-12-01
ClickHouse 收购 PeerDB,通过 Postgres CDC 集成增强实时分析
数据库河北幸福消费金融基于 Apache Doris 构建实时数仓,查询提速 400 倍!
2023-07-28
1. HBase 的基本介绍及表模型
2023-09-08
Linux 系统安装和使用 Kafka 教程。
2023-08-14
04|MongoDB:如何安装和使用 MongoDB 数据库?
2023-04-17
24|提示语工程(六):超越智能,让你的 AI 系统成为全知超人
2023-10-13
电子书
大厂实战PPT下载
换一换 杨诗旻 | 火山引擎 LAS 数据湖团队负责人
赵晨阳 | 矩阵起源 研发副总裁
李欣 | eBay 人工智能平台/首席架构师
评论