当前位置: 首页 > 工具软件 > jMock > 使用案例 >

Flink中如何使用Jmock 进行学习常规业务的学习与测试

马和硕
2023-12-01

1. 在pom中引入jmock 坐标

<dependency>
    <groupId>com.github.a524631266</groupId>
    <artifactId>jmock-core</artifactId>
    <version>1.1.1</version>
</dependency>

2. 参照 Jmock介绍 规则写pojo类型

import com.zhangll.jmock.core.annotation.BasicTokenInfo;
import com.zhangll.jmock.core.annotation.ContainerTokenInfo;
import lombok.Data;
import lombok.ToString;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Set;

@Data
@ToString
public class Person {
    @BasicTokenInfo(min = "1", max = "10")
    private Integer id;

    @BasicTokenInfo(min = "0", max = "20")
    private int age;

	@BasicTokenInfo(min = "1", max = "9")
    private boolean sex;
    
    @BasicTokenInfo(value = {"/[a-zA-Z]\\d{8,10}/", "@First @Middle @Last"})
    private String name;
    @BasicTokenInfo(min = "1990-01-01 00:00:00", max = "2000-01-01 00:00:00")
    private Timestamp birthDay;
    /**
     * 体重
     */
    @BasicTokenInfo(min = "10", max = "100")
    private Double weight;

    /**
     *
     */
    @ContainerTokenInfo(
            innerBasicType = @BasicTokenInfo(value = {"爱情公寓", "乾坤住宿", "瞬间住宿"})
    )
    @BasicTokenInfo(min = "1", max = "3")
    private Set<String> address;
}

如上表示的是通过定义一个Person,

  1. Integer包装类型的 id取值范围在[1,10] 中
  2. int基本类型打age(年龄) 范围在[0-20] 岁之间
  3. 性别为boolean类型,其中为true的概率为9/ (1+9) = 90%,[max/(min+max)]
  4. 姓名可以通过通过 正则表达式和@表达取一来表示
  5. 出生日期,我们用sql.timestamp表示.随机值大小在1990年到2000年之间
  6. 体重为double类型嗯,在10-100直接
  7. 地址,可以包含多个,是以set接口定义的数据,集合的大小由 basicTokenInfo定义,即在1个到3个之间,同时里面的内容为value中选择

3. 编写业务逻辑类

这里主要是做一些业务逻辑代码编写.主要介绍context如何使用,并如何注入到flink中,使用很简单.可以利用richFunction中的open方法注入一个mockContext,并在source中的run方法中定时每秒钟打印.

public class Main3 {
    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.addSource(new SourceFromMock()).print();

        env.execute("Flink add data sourc");
    }

    private static class SourceFromMock extends RichSourceFunction<Person> {
        private AnnotationMockContext mockContext;
        private boolean canRunning = true;
        @Override
        public void open(Configuration parameters) throws Exception {
            super.open(parameters);
            mockContext = new AnnotationMockContext();
        }

        @Override
        public void run(SourceContext<Person> sourceContext) throws Exception {
            while (canRunning){
                Person mock = mockContext.mock(Person.class);
                sourceContext.collect(mock);
                TimeUnit.SECONDS.sleep(1);
            }
        }

        @Override
        public void cancel() {

        }
    }
}

输出结果图

2> Person(id=10, age=3, sex=true, name=陆 柳 担, birthDay=1996-12-16 12:45:32.0, weight=46.9, address=[爱情公寓, 乾坤住宿])
3> Person(id=8, age=9, sex=true, name=酆 付 排, birthDay=1999-05-31 21:16:32.0, weight=30.1, address=[乾坤住宿])
4> Person(id=7, age=18, sex=true, name=时 城 薯, birthDay=1995-04-16 16:58:49.0, weight=51.3, address=[爱情公寓, 乾坤住宿])
1> Person(id=3, age=14, sex=true, name=u757278137, birthDay=1994-01-13 00:02:49.0, weight=29.1, address=[爱情公寓])
2> Person(id=10, age=8, sex=true, name=晏 准 事, birthDay=1996-02-16 09:35:55.0, weight=83.2, address=[爱情公寓, 乾坤住宿])
3> Person(id=9, age=12, sex=true, name=d4809119708, birthDay=1997-01-17 10:52:02.0, weight=78.1, address=[爱情公寓, 乾坤住宿])
4> Person(id=4, age=0, sex=true, name=n340868700, birthDay=1990-03-29 13:01:05.0, weight=51.5, address=[爱情公寓])
1> Person(id=3, age=1, sex=true, name=康 鸽 锤, birthDay=1998-03-30 18:18:17.0, weight=26.5, address=[爱情公寓, 乾坤住宿])
2> Person(id=5, age=3, sex=true, name=j5456751278, birthDay=1993-06-28 12:30:24.0, weight=32.3, address=[爱情公寓])
3> Person(id=8, age=9, sex=false, name=容 两 酒, birthDay=1996-12-09 23:24:23.0, weight=57.6, address=[爱情公寓, 乾坤住宿])
4> Person(id=10, age=10, sex=true, name=a47614199, birthDay=1997-06-07 17:58:56.0, weight=70.3, address=[爱情公寓, 乾坤住宿])
1> Person(id=2, age=13, sex=true, name=t008225619, birthDay=1994-02-20 23:19:14.0, weight=97.1, address=[爱情公寓])
2> Person(id=3, age=15, sex=false, name=唐 吃 游, birthDay=1991-05-28 07:00:08.0, weight=57.6, address=[乾坤住宿])
3> Person(id=3, age=17, sex=true, name=通 炕 贱, birthDay=1991-08-11 18:50:46.0, weight=73.1, address=[爱情公寓, 乾坤住宿])
4> Person(id=4, age=16, sex=true, name=空 湖 卖, birthDay=1999-02-08 17:56:00.0, weight=97.2, address=[爱情公寓])
1> Person(id=1, age=8, sex=true, name=钱 佛 忘, birthDay=1992-05-22 06:18:47.0, weight=38.9, address=[乾坤住宿])
2> Person(id=4, age=0, sex=true, name=q05950086, birthDay=1996-05-14 06:22:13.0, weight=91.7, address=[乾坤住宿])
3> Person(id=2, age=15, sex=true, name=k5833226502, birthDay=1991-05-30 22:10:57.0, weight=12.7, address=[爱情公寓])
4> Person(id=5, age=7, sex=true, name=c50555821, birthDay=1998-11-16 21:44:19.0, weight=20.1, address=[爱情公寓])
1> Person(id=7, age=19, sex=true, name=梅 扫 量, birthDay=1994-06-21 14:58:57.0, weight=78.8, address=[乾坤住宿])
2> Person(id=1, age=12, sex=true, name=g673070877, birthDay=1998-11-23 22:36:18.0, weight=25.5, address=[乾坤住宿])
3> Person(id=8, age=9, sex=true, name=童 楚 豆, birthDay=1994-07-05 15:33:09.0, weight=94.2, address=[爱情公寓, 乾坤住宿])
4> Person(id=3, age=7, sex=true, name=v44070883, birthDay=1995-06-02 16:06:51.0, weight=62.5, address=[乾坤住宿])
1> Person(id=6, age=8, sex=true, name=廉 圈 啊, birthDay=1992-09-30 09:27:59.0, weight=45.3, address=[爱情公寓, 乾坤住宿])
2> Person(id=10, age=9, sex=true, name=i3502080420, birthDay=1997-05-16 17:37:45.0, weight=88.6, address=[乾坤住宿])
3> Person(id=9, age=19, sex=true, name=缑 冈 乔, birthDay=1991-10-09 20:06:37.0, weight=60.9, address=[爱情公寓, 乾坤住宿])
4> Person(id=6, age=1, sex=true, name=蓟 恼 码, birthDay=1992-08-12 23:45:06.0, weight=93.8, address=[爱情公寓, 乾坤住宿])
1> Person(id=4, age=17, sex=false, name=都 岗 仍, birthDay=1997-12-15 06:58:41.0, weight=59.1, address=[爱情公寓])
2> Person(id=5, age=17, sex=true, name=z6323945062, birthDay=1998-08-29 17:52:34.0, weight=55.1, address=[乾坤住宿])
3> Person(id=7, age=18, sex=false, name=m47196631, birthDay=1996-07-15 01:05:19.0, weight=10.6, address=[乾坤住宿])
4> Person(id=4, age=4, sex=true, name=n150323479, birthDay=1997-09-07 19:33:35.0, weight=60.7, address=[乾坤住宿])
1> Person(id=6, age=19, sex=true, name=邓 茫 顺, birthDay=1992-04-29 14:47:15.0, weight=67.3, address=[爱情公寓])
2> Person(id=3, age=0, sex=true, name=鄢 纸 旱, birthDay=1997-10-03 22:43:36.0, weight=28.4, address=[爱情公寓, 乾坤住宿])
3> Person(id=9, age=19, sex=false, name=余 坛 着, birthDay=1997-01-28 11:45:15.0, weight=67.9, address=[乾坤住宿])
4> Person(id=2, age=16, sex=true, name=g20035787, birthDay=1996-09-10 10:25:16.0, weight=48.4, address=[爱情公寓, 乾坤住宿])
1> Person(id=2, age=14, sex=true, name=a66290436, birthDay=1991-09-02 03:44:38.0, weight=75.1, address=[爱情公寓, 乾坤住宿])
2> Person(id=9, age=20, sex=true, name=广 皮 用, birthDay=1991-08-28 07:46:24.0, weight=79.7, address=[乾坤住宿])
3> Person(id=7, age=11, sex=true, name=x9140898266, birthDay=1994-12-15 02:48:55.0, weight=10.1, address=[乾坤住宿])
4> Person(id=1, age=1, sex=false, name=n3544315066, birthDay=1995-11-10 18:28:04.0, weight=69.4, address=[爱情公寓, 乾坤住宿])
1> Person(id=6, age=14, sex=true, name=w8180455198, birthDay=1993-02-12 04:24:58.0, weight=76.7, address=[爱情公寓, 乾坤住宿])
2> Person(id=7, age=1, sex=true, name=全 狡 绵, birthDay=1998-01-26 18:36:02.0, weight=80.7, address=[爱情公寓])
3> Person(id=10, age=14, sex=true, name=窦 循 约, birthDay=1994-03-07 07:36:17.0, weight=89.1, address=[乾坤住宿])
 类似资料: