Flink DataSet 工具类 DataSetUtils 常用API

DataSetUtils类提供了一些生成index和数据采样和分析的方法,包括countElementsPerPartition、zipWithIndex、zipWithUniqueId、sample、sampleWithSize、summarize等。

首先构造输入数据 

DataSet<Tuple2<Integer,String>> input = env.fromElements(Tuple2.of(1,"a"),Tuple2.of(2,"b"),Tuple2.of(3,"c"),Tuple2.of(4,"d"),Tuple2.of(5,"e"),Tuple2.of(6,"f"));

countElementsPerPartition    统计每个分区中元素的个数

返回Tuple2<partitionID,elementCount>

 DataSetUtils.countElementsPerPartition(input).print();

zipWithIndex    给每个元素生成唯一的、连续的index

返回Tuple2<Long, T>

先调用countElementsPerPartition计算每个分区中元素的数量,然后为分区中的元素生成index

DataSetUtils.zipWithIndex(input).print();

zipWithUniqueId    给每个元素生成唯一的、不连续的ID

与zipWithIndex不同,不需要计算每个分区中的元素数量

DataSetUtils.zipWithUniqueId(input).print();

sample    对输入数据进行采样操作

withReplacement:是否可以重复采样  

fraction:每个元素被选中的概率,不能重复采样时fraction [0,1],可以重复采样时fraction[1,∞]

DataSetUtils.sample(input,false ,0.3 ).print();

可以指定种子的采样操作,seed固定时,生成的样本集不变,上面的无种子采样其实后台调用了随机数种子

DataSetUtils.sample(input,false ,0.3 ,3 ).print();

sampleWithSize    生成指定大小的样本集  

numSamples:样本数量

DataSetUtils.sampleWithSize(input,false ,2 ).print();

生成指定大小的样本集,指定种子

DataSetUtils.sampleWithSize(input,false,2 ,3 ).print();

summarize    统计分析每个列的元素

返回DataSet<TupleSummaryAggregator<R>>  包括最大最小值,均值,方差,标准差等

System.out.println(DataSetUtils.summarize(input));

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值