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的更多相关文章

  1. InputStream、InputStreamReader和Reader的关系

    InputStream:得到的是字节输入流,InputStream.read("filename")之后,得到字节流 Reader:读取的是字符流 InputStreamReade ...

  2. Distribution1:Distribution Reader

    在transactional replication中,在publication中执行了一个更新,例如:update table set col=? Where ?,如果table中含有大量的数据行, ...

  3. Reader与InputStream两个类中的read()的区别

    InputStream类的read()方法是从流里面取出一个字节,他的函数原型是 int read(); ,Reader类的read()方法则是从流里面取出一个字符(一个char),他的函数原型也是  ...

  4. Ubuntu 12.04安装Adobe Reader

    原本从Adobe 官方网站下载了 Adobe Reader, 是个rpm包,先用agt-get 装了rpm(sudo apt-get install rpm), 一安装(rpm -ivh AdobeR ...

  5. .dtsi .dts dtc dtb 是什么

    基础 .dts: device tree source .dtsi:   device tree source include .dts比作源文件,.dtsi比作头文件. dtc是linux源码 /s ...

  6. SSIS之-DTS对象&事件

    1.Dts 是类 Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel 类的一个实例,Dts 对象有 7 个属性和一个方法,以下是DTS ...

  7. Adobe Reader/Acrobat修改页面底色为豆沙绿

    Adobe Acrobat_Pro_8修改PDF页面底色为豆沙绿保护视力(同样适用于Adobe Reader) http://jingyan.baidu.com/article/9989c746189 ...

  8. 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 ...

  9. AttributeError: '_csv.reader' object has no attribute 'next'

    我在使用pyhon3.4运行以下代码时报错:AttributeError: '_csv.reader' object has no attribute 'next' import csv import ...

随机推荐

  1. BCB 如何让Application收到SendMessage发送来的消息

    一般,都是通过添加一个ApplicationEvent组件就可以接收到PostMessage发来的消息,那么如何收到SendMessage发来的消息呢? https://stackoverflow.c ...

  2. CentOS7.x安装mariadb-10.3

    1.配置mariadb yum源 vim /etc/yum.repos.d/mariadb.repo # 写入如下内容 [mariadb] name = MariaDB baseurl = http: ...

  3. 生成器调试---send方式

    调试 def creat_num(all_num): a, b = 0, 1 current_num = 0 while current_num < all_num: ret = yield a ...

  4. 什么是webpack模块化构建工具

    百度百科模块化:是指解决一个复杂问题时自顶向下逐层把系统划分成若干模块的过程,有多种属性,分别反映其内部特性. 计算机模块化:一般指的是可以被抽象封装的最小/最优代码集合,模块化解决的是功能耦合问题. ...

  5. php面向对象之静态方法使用

    什么是静态方法? 不是所有的变量和方法都要通过创建对象来调用.可以通过给变量和方法加上static关键字来直接调用.无锡大理石测量平台 调用静态成员的语法格式为: 关键字::静态成员 关键字可以是: ...

  6. Linux下安装mysql(离线安装和在线安装)

    一:在线安装mysql 1.首先检测一下,mysql之前有没有被安装 命令:rpm -qa | grep mysql 2.删除mysql的命令: rpm -e --nodeps `rpm -qa | ...

  7. 学习了武沛齐讲的Day11-完

    DAY11 老师讲了很多,理解了一个方法   当有复杂的问题时,先拆解很小的单元(10行),测试通过了,再简化(3行) len range 感悟:其实不用计记代码(主要是记不动),,当遇到问题时:找出 ...

  8. jQuery网页加载的不同方式

    一.window.onload 代码是从上而下执行的,通过window.onload可以使事件在页面加载完毕再执行 注意:window.onload事件多个只会执行最下面的一个,前面的会被覆盖 < ...

  9. Luogu2791 幼儿园篮球题【斯特林数,数学】

    题目链接:洛谷 我一开始不知道$N,M$有什么用处,懵逼了一会儿,结果才发现是输入数据范围... $$\begin{aligned}\binom{n}{k}Ans&=\sum_{i=0}^k\ ...

  10. 数据结构实验之查找一:二叉排序树 (SDUT 3373)

    二叉排序树(Binary Sort Tree),又称二叉查找树(Binary Search Tree),也称二叉搜索树. #include <stdio.h> #include <s ...