dts reader
package com.aliyun.dts.connect; import com.alibaba.fastjson.JSONObject;
import com.aliyun.drc.client.message.DataMessage;
import com.aliyun.drc.clusterclient.ClusterClient;
import com.aliyun.drc.clusterclient.ClusterListener;
import com.aliyun.drc.clusterclient.DefaultClusterClient;
import com.aliyun.drc.clusterclient.RegionContext;
import com.aliyun.drc.clusterclient.message.ClusterMessage; import java.io.UnsupportedEncodingException;
import java.util.List; public class DTSReader { private final ClusterClient dts_client; public DTSReader(String accessKey, String accessSecret, String subscribeInstanceID) throws Exception {
this.dts_client = initClusterClient(accessKey, accessSecret, subscribeInstanceID);
} private ClusterClient initClusterClient(String accessKey, String accessSecret, String subscribeInstanceID)
throws Exception {
// 创建一个context,仅是属性设置
RegionContext context = new RegionContext();
// 运行SDK的服务器是否使用公网IP连接DTS(若使用内网IP访问,需要设置false)
// 在订阅任务启动时,需要网络连接时需要关注该设置项
context.setUsePublicIp(true);
// 设置用户accessKey secret
context.setAccessKey(accessKey);
context.setSecret(accessSecret); // 创建消费者
ClusterClient client = new DefaultClusterClient(context);
ClusterListener listener = new ClusterListener() {
// @Override
public void noException(Exception e) {
// TODO Auto-generated method stub
e.printStackTrace();
} // @Override
public void notify(List<ClusterMessage> messages) throws Exception {
//处理订阅任务收到的消息
for (ClusterMessage message : messages) {
replicateMessage(message);
}
}
}; client.askForGUID(subscribeInstanceID);
client.addConcurrentListener(listener); return client;
} private void replicateMessage(final ClusterMessage message) {
// 处理消息
try {
// 转换消息格式并处理
convertRecord(message);
// 确认消息以消费
message.ackAsConsumed();
} catch (Exception e) {
// TODO 根据业务需求进行必要的重试
e.printStackTrace();
}
} private void convertRecord(ClusterMessage message) throws UnsupportedEncodingException {
DataMessage.Record record = message.getRecord();
System.out.println("Record Op type:" + record.getOpt().toString());
JSONObject jsonRecord;
String key = null;
switch (record.getOpt()) {
case INSERT: // 数据插入
jsonRecord = convertFields(record, 0, 1);
key = record.getPrimaryKeys();
System.out.println("Record Insert:Json format:" + jsonRecord.toJSONString());
break;
case UPDATE:// 数据更新
case REPLACE:// replace操作
JSONObject oldJsonRecord = convertFields(record, 0, 2);
System.out.println("Record Update Before:Json format:" + oldJsonRecord.toJSONString());
jsonRecord = convertFields(record, 1, 2);
System.out.println("Record Update Before:Json format:" + jsonRecord.toJSONString());
key = record.getPrimaryKeys();
break;
case DELETE:// 数据删除
jsonRecord = convertFields(record, 0, 1);
System.out.println("Record Delete:Json format:" + jsonRecord.toJSONString());
key = record.getPrimaryKeys();
break;
default:
return;
}
//数据表中对主Key列名
System.out.println("PrimaryKey Column Name:" + key);
//drds中物理数据库名和物理数据表名
System.out.println("Record DB Name:"+record.getDbname()+",Table Name:"+record.getTablename());
//drds中逻辑数据库名和逻辑表名
System.out.println("Record Logical DB Name:"+record.getLogicalDbname()+",Table Name:"+record.getLogicalTablename()); } // 将消息组成JSON格式输出
private JSONObject convertFields(DataMessage.Record record, int start, int step)
throws UnsupportedEncodingException {
List<DataMessage.Record.Field> fields = record.getFieldList();
JSONObject ret = new JSONObject();
for (int i = start; i < fields.size(); i += step) {
DataMessage.Record.Field field = fields.get(i);
JSONObject object = new JSONObject();
object.put("type", field.getType().toString());
object.put("encoding", field.getEncoding());
if (field.getValue() != null) {
object.put("value", field.getValue().toString(field.getEncoding()));
} else {
object.put("value", null);
}
ret.put(field.getFieldname(), object);
}
return ret;
} public void start() throws Exception {
System.out.println("Start DTS subscription client...");
dts_client.start();
} public void stop() throws Exception {
System.out.println("Stop DTS Subscription Client...");
dts_client.stop();
} }
dts reader的更多相关文章
- InputStream、InputStreamReader和Reader的关系
InputStream:得到的是字节输入流,InputStream.read("filename")之后,得到字节流 Reader:读取的是字符流 InputStreamReade ...
- Distribution1:Distribution Reader
在transactional replication中,在publication中执行了一个更新,例如:update table set col=? Where ?,如果table中含有大量的数据行, ...
- Reader与InputStream两个类中的read()的区别
InputStream类的read()方法是从流里面取出一个字节,他的函数原型是 int read(); ,Reader类的read()方法则是从流里面取出一个字符(一个char),他的函数原型也是 ...
- Ubuntu 12.04安装Adobe Reader
原本从Adobe 官方网站下载了 Adobe Reader, 是个rpm包,先用agt-get 装了rpm(sudo apt-get install rpm), 一安装(rpm -ivh AdobeR ...
- .dtsi .dts dtc dtb 是什么
基础 .dts: device tree source .dtsi: device tree source include .dts比作源文件,.dtsi比作头文件. dtc是linux源码 /s ...
- SSIS之-DTS对象&事件
1.Dts 是类 Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel 类的一个实例,Dts 对象有 7 个属性和一个方法,以下是DTS ...
- Adobe Reader/Acrobat修改页面底色为豆沙绿
Adobe Acrobat_Pro_8修改PDF页面底色为豆沙绿保护视力(同样适用于Adobe Reader) http://jingyan.baidu.com/article/9989c746189 ...
- multithreading - Reader/Writer Locks in C++
You Only Need To Note This: only 1 single thread can acquire an upgrade_lock at one time. others are ...
- AttributeError: '_csv.reader' object has no attribute 'next'
我在使用pyhon3.4运行以下代码时报错:AttributeError: '_csv.reader' object has no attribute 'next' import csv import ...
随机推荐
- git 在不同服务器主机上同步 git 仓库
git 在不同服务器主机上同步 git 仓库 参考链接:https://opentechguides.com/how-to/article/git/177/git-sync-repos.html 1. ...
- flask+uwsgi+supervisor部署流程
背景: 小鱼最近搞了个工程,python用的2.7(用3也可以),后端使用的是flask,服务器用的linux,使用 flask+uwsgi+supervisor部署 ,查阅相关博客.调试.实操,已经 ...
- postgresql —— 查看索引
查索引 语句: SELECT tablename, indexname, indexdef FROM pg_indexes WHERE tablename = 'user_tbl' ORDER BY ...
- 国内可用的python源
国内可用的python源 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple/ 阿里云:http://mirrors.aliyun.com/pypi/simpl ...
- plsql工具使用
作者:lhrbest 作者:张冲andy 作者:jack_孟
- Vue项目中v-bind动态绑定src路径不成功
问题: 在做Vue项目的时候,由于项目需求,需要动态绑定img的src时,突然发现如果说是直接请求后台接口的图片地址就能显示, 但是直接动态绑定img的src的图片的相对路径或者是绝对路径的时候,图片 ...
- 洛谷 P1309 瑞士轮 题解
每日一题 day4 打卡 Analysis 暴力+快排(其实是归并排序) 一开始天真的以为sort能过,结果光荣TLE,由于每次只更改相邻的元素,于是善于处理随机数的快排就会浪费很多时间.于是就想到归 ...
- 【源码拾遗】从vue-router看前端路由的两种实现
本文由浅入深观摩vue-router源码是如何通过hash与History interface两种方式实现前端路由,介绍了相关原理,并对比了两种方式的优缺点与注意事项.最后分析了如何实现可以直接从文件 ...
- [HNOI2002]营业额统计 II
https://www.luogu.org/problemnew/show/2234 将权值离散化,以权值为下标建立权值线段树 #include <bits/stdc++.h> using ...
- 块状链表 codevs 2333弹飞绵羊
块状链表,分块处理,先预处理每一个点跳到下一个块 跳到哪,步数.然后修改的时候,修该那一个块即可 #include<cstdio>#include<cmath>int a[20 ...