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. Jquery 操作DOM元素

    一.文本输入框: text <input type=”text” value=”99.com” size=12 id=”input1” /> 1.获取文本值: $("#input ...

  2. Restful架构API编码规范

    Restful API 目前比较成熟的一套互联网应用程序的API设计理论 一.协议 API与用户的通信协议,总是使用HTTPs协议. 二.域名 应该尽量将API部署在专用域名之下. https://a ...

  3. [SCSS] SASS dynamic class properties

    @mixin generateModifers($property) { #{$property}: $padding; &-large { #{$property}: $padding-la ...

  4. Mysql查看所有表的数据量

    ##查看所有表信息 SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'pcms-zgh20190327' ##查看各个表数据量 ...

  5. 一些VMware vCenter Appliance的默认用户名和密码

    一些VMware vCenter Appliance的默认用户名和密码 2014-03-30 17:30:03 flowershade_21 阅读数 13367更多 分类专栏: vmware   VM ...

  6. 富文本编辑器+可粘贴word内容

    Chrome+IE默认支持粘贴剪切板中的图片,但是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧? 我希望打开文档doc直接复制粘贴到富文本编辑器,直接发布 感觉这个似乎很困难 ...

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

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

  8. 块状链表 codevs 2333弹飞绵羊

    块状链表,分块处理,先预处理每一个点跳到下一个块 跳到哪,步数.然后修改的时候,修该那一个块即可 #include<cstdio>#include<cmath>int a[20 ...

  9. vue中引入Tinymce富文本编辑器

    最近想在项目上引入一个富文本编辑器,之前引入过summernote,感觉并不太适合vue使用, 然后在网上查了查,vue中使用Tinymce比较适合, 首先,我们在vue项目的components文件 ...

  10. OS创建页目录和页

    ;开始创建页目录项(PDE) .create_pde: ; 创建Page Directory Entry mov eax, PAGE_DIR_TABLE_POS ; PAGE_DIR_TABLE_PO ...