【大厂技术内幕】字节跳动原来是这么做数据迁移的

return apArticleContentMapper.selectByArticleIds(ids);

}

@Override

public ApArticleContent getByArticleIds(Integer id) {

return apArticleContentMapper.selectByArticleId(id);

}

}

7.4 文章接口


7.4.1 mapper定义

ApArticleMapper新增方法

/**

  • 查询

  • @param apArticle

  • @return

*/

List selectList(ApArticle apArticle);

/**

  • 更新

  • @param apArticle

*/

void updateSyncStatus(ApArticle apArticle);

ApArticleMapper.xml

and title = #{title}

and author_id = #{authorId}

and author_name = #{authorName}

and channel_id = #{channelId}

and channel_name = #{channelName}

and layout = #{layout}

and flag = #{flag}

and views = #{views}

and sync_status = #{syncStatus}

select

from ap_article

UPDATE ap_article SET sync_status = #{syncStatus} WHERE id=#{id}

7.4.2 service

对ApArticle操作的Service

接口位置:com.heima.migration.service.ApArticleService

public interface ApArticleService {

public ApArticle getById(Long id);

/**

  • 获取未同步的数据

  • @return

*/

public List getUnsyncApArticleList();

/**

  • 更新同步状态

  • @param apArticle

*/

void updateSyncStatus(ApArticle apArticle);

}

ApArticleServiceImpl

对ApArticleService相关的操作

代码位置:com.heima.migration.service.impl.ApArticleServiceImpl

@Log4j2

@Service

public class ApArticleServiceImpl implements ApArticleService {

@Autowired

private ApArticleMapper apArticleMapper;

public ApArticle getById(Long id) {

return apArticleMapper.selectById(id);

}

/**

  • 获取未同步的数据

  • @return

*/

public List getUnsyncApArticleList() {

ApArticle apArticleQuery = new ApArticle();

apArticleQuery.setSyncStatus(false);

return apArticleMapper.selectList(apArticleQuery);

}

/**

  • 更新数据同步状态

  • @param apArticle

*/

public void updateSyncStatus(ApArticle apArticle) {

log.info(“开始更新数据同步状态,apArticle:{}”, apArticle);

if (null != apArticle) {

apArticle.setSyncStatus(true);

apArticleMapper.updateSyncStatus(apArticle);

}

}

}

7.5 文章作者接口


7.5.1 mapper定义

ApAuthorMapper

List selectByIds(List ids);

ApAuthorMapper.xml

select * from ap_author

where id in

#{item}

7.5.2 service

对ApAuthor操作的Service

接口位置:com.heima.migration.service.ApAuthorService

public interface ApAuthorService {

List queryByIds(List ids);

ApAuthor getById(Long id);

}

ApAuthorServiceImpl

对ApAuthor相关的操作

代码位置:com.heima.migration.service.impl.ApAuthorServiceImpl

@Service

public class ApAuthorServiceImpl implements ApAuthorService {

@Autowired

private ApAuthorMapper apAuthorMapper;

@Override

public List queryByIds(List ids) {

return apAuthorMapper.selectByIds(ids);

}

@Override

public ApAuthor getById(Long id) {

if (null != id) {

return apAuthorMapper.selectById(id.intValue());

}

return null;

}

}

7.6 综合迁移接口


ArticleQuantityService

操作ArticleQuantity对象的Service ArticleQuantity对象封装了文章相关的数据

接口位置:com.heima.migration.service.ArticleQuantityService

public interface ArticleQuantityService {

/**

  • 获取ArticleQuantity列表

  • @return

*/

public List getArticleQuantityList();

/**

  • 根据ArticleId获取ArticleQuantity

  • @param id

  • @return

*/

public ArticleQuantity getArticleQuantityByArticleId(Long id);

/**

  • 根据ByArticleId从Hbase中获取ArticleQuantity

  • @param id

  • @return

*/

public ArticleQuantity getArticleQuantityByArticleIdForHbase(Long id);

/**

  • 数据库到Hbase的同步

*/

public void dbToHbase();

/**

  • 根据articleId 将数据库的数据同步到Hbase

  • @param articleId

*/

public void dbToHbase(Integer articleId);

}

ArticleQuantityServiceImpl

对ArticleQuantity的相关操作

代码位置:com.heima.migration.service.impl.ArticleQuantityServiceImpl

/**

  • 查询未同步的数据,并封装成ArticleQuantity 对象

*/

@Service

@Log4j2

public class ArticleQuantityServiceImpl implements ArticleQuantityService {

@Autowired

private ApArticleContenService apArticleContenService;

@Autowired

private ApArticleConfigService apArticleConfigService;

@Autowired

private ApAuthorService apAuthorService;

@Autowired

private HBaseStorageClient hBaseStorageClient;

@Autowired

private ApArticleService apArticleService;

/**

  • 查询位同步数据的列表

  • @return

*/

public List getArticleQuantityList() {

log.info(“生成ArticleQuantity列表”);

//查询未同步的庶数据

List apArticleList = apArticleService.getUnsyncApArticleList();

if (apArticleList.isEmpty()) {

return null;

}

//获取ArticleId 的list

List apArticleIdList = apArticleList.stream().map(apArticle -> String.valueOf(apArticle.getId())).collect(Collectors.toList());

//获取AuthorId 的 list

List apAuthorIdList = apArticleList.stream().map(apAuthor -> apAuthor.getAuthorId() == null ? null : apAuthor.getAuthorId().intValue()).filter(x -> x != null).collect(Collectors.toList());

//根据apArticleIdList 批量查询出内容列表

List apArticleContentList = apArticleContenService.queryByArticleIds(apArticleIdList);

//根据apArticleIdList 批量查询出配置列表

List apArticleConfigList = apArticleConfigService.queryByArticleIds(apArticleIdList);

//根据apAuthorIdList 批量查询出作者列

List apAuthorList = apAuthorService.queryByIds(apAuthorIdList);

//将不同的对象转换为 ArticleQuantity 对象

List articleQuantityList = apArticleList.stream().map(apArticle -> {

return new ArticleQuantity() {{

//设置apArticle 对象

setApArticle(apArticle);

// 根据apArticle.getId() 过滤出符合要求的 ApArticleContent 对象

List apArticleContents = apArticleContentList.stream().filter(x -> x.getArticleId().equals(apArticle.getId())).collect(Collectors.toList());

if (null != apArticleContents && !apArticleContents.isEmpty()) {

setApArticleContent(apArticleContents.get(0));

}

// 根据 apArticle.getId 过滤出 ApArticleConfig 对象

List apArticleConfigs = apArticleConfigList.stream().filter(x -> x.getArticleId().equals(apArticle.getId())).collect(Collectors.toList());

if (null != apArticleConfigs && !apArticleConfigs.isEmpty()) {

setApArticleConfig(apArticleConfigs.get(0));

}

// 根据 apArticle.getAuthorId().intValue() 过滤出 ApAuthor 对象

List apAuthors = apAuthorList.stream().filter(x -> x.getId().equals(apArticle.getAuthorId().intValue())).collect(Collectors.toList());

if (null != apAuthors && !apAuthors.isEmpty()) {

setApAuthor(apAuthors.get(0));

}

//设置回调方法 用户方法的回调 用于修改同步状态 插入Hbase 成功后同步状态改为已同步

setHBaseInvok(new ArticleHBaseInvok(apArticle, (x) -> apArticleService.updateSyncStatus(x)));

}};

}).collect(Collectors.toList());

if (null != articleQuantityList && !articleQuantityList.isEmpty()) {

log.info(“生成ArticleQuantity列表完成,size:{}”, articleQuantityList.size());

} else {

log.info(“生成ArticleQuantity列表完成,size:{}”, 0);

}

return articleQuantityList;

}

public ArticleQuantity getArticleQuantityByArticleId(Long id) {

if (null == id) {

return null;

}

ArticleQuantity articleQuantity = null;

ApArticle apArticle = apArticleService.getById(id);

if (null != apArticle) {

articleQuantity = new ArticleQuantity();

articleQuantity.setApArticle(apArticle);

ApArticleContent apArticleContent = apArticleContenService.getByArticleIds(id.intValue());

articleQuantity.setApArticleContent(apArticleContent);

ApArticleConfig apArticleConfig = apArticleConfigService.getByArticleId(id.intValue());

articleQuantity.setApArticleConfig(apArticleConfig);

ApAuthor apAuthor = apAuthorService.getById(apArticle.getAuthorId());

articleQuantity.setApAuthor(apAuthor);

}

return articleQuantity;

}

public ArticleQuantity getArticleQuantityByArticleIdForHbase(Long id) {

if (null == id) {

return null;

}

ArticleQuantity articleQuantity = null;

List typeList = Arrays.asList(ApArticle.class, ApArticleContent.class, ApArticleConfig.class, ApAuthor.class);

List objectList = hBaseStorageClient.getStorageDataEntityList(HBaseConstants.APARTICLE_QUANTITY_TABLE_NAME, DataConvertUtils.toString(id), typeList);

if (null != objectList && !objectList.isEmpty()) {

articleQuantity = new ArticleQuantity();

for (Object value : objectList) {

if (value instanceof ApArticle) {

articleQuantity.setApArticle((ApArticle) value);

} else if (value instanceof ApArticleContent) {

articleQuantity.setApArticleContent((ApArticleContent) value);

} else if (value instanceof ApArticleConfig) {

articleQuantity.setApArticleConfig((ApArticleConfig) value);

} else if (value instanceof ApAuthor) {

articleQuantity.setApAuthor((ApAuthor) value);

}

}

}

return articleQuantity;

}

/**

  • 数据库到Hbase同步

*/

public void dbToHbase() {

long cutrrentTime = System.currentTimeMillis();

List articleQuantitList = getArticleQuantityList();

if (null != articleQuantitList && !articleQuantitList.isEmpty()) {

log.info(“开始进行定时数据库到HBASE同步,筛选出未同步数据量:{}”, articleQuantitList.size());

if (null != articleQuantitList && !articleQuantitList.isEmpty()) {

List hbaseStorageList = articleQuantitList.stream().map(ArticleQuantity::getHbaseStorage).collect(Collectors.toList());

hBaseStorageClient.addHBaseStorage(HBaseConstants.APARTICLE_QUANTITY_TABLE_NAME, hbaseStorageList);

}

} else {

log.info(“定时数据库到HBASE同步为筛选出数据”);

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
img

最后总结

ActiveMQ+Kafka+RabbitMQ学习笔记PDF

image.png

  • RabbitMQ实战指南

image.png

  • 手写RocketMQ笔记

image.png

  • 手写“Kafka笔记”

image

关于分布式,限流+缓存+缓存,这三大技术(包含:ZooKeeper+Nginx+MongoDB+memcached+Redis+ActiveMQ+Kafka+RabbitMQ)等等。这些相关的面试也好,还有手写以及学习的笔记PDF,都是啃透分布式技术必不可少的宝藏。以上的每一个专题每一个小分类都有相关的介绍,并且小编也已经将其整理成PDF啦

一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!

AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算

果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)**
[外链图片转存中…(img-NJNINgSn-1712275207536)]

最后总结

ActiveMQ+Kafka+RabbitMQ学习笔记PDF

[外链图片转存中…(img-HQCKD0hc-1712275207537)]

  • RabbitMQ实战指南

[外链图片转存中…(img-iMn5OfbN-1712275207537)]

  • 手写RocketMQ笔记

[外链图片转存中…(img-OemQDMZt-1712275207538)]

  • 手写“Kafka笔记”

[外链图片转存中…(img-5QMXQgp4-1712275207538)]

关于分布式,限流+缓存+缓存,这三大技术(包含:ZooKeeper+Nginx+MongoDB+memcached+Redis+ActiveMQ+Kafka+RabbitMQ)等等。这些相关的面试也好,还有手写以及学习的笔记PDF,都是啃透分布式技术必不可少的宝藏。以上的每一个专题每一个小分类都有相关的介绍,并且小编也已经将其整理成PDF啦

一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!

AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值