当前位置: 首页 > news >正文

建站公司用的开源系统宁德seo优化

建站公司用的开源系统,宁德seo优化,wordpress批量添加图片链接,苍南具城乡建设局网站背景 在处理窗口函数时,ProcessWindowFunction处理函数可以定义三个状态: 富函数getRuntimeContext.getState, 每个key每个窗口的状态context.windowState(),每个key的状态context.globalState,那么这几个状态之间有什么关系呢? …

背景

在处理窗口函数时,ProcessWindowFunction处理函数可以定义三个状态: 富函数getRuntimeContext.getState,
每个key+每个窗口的状态context.windowState(),每个key的状态context.globalState,那么这几个状态之间有什么关系呢?

ProcessWindowFunction处理函数三种状态之间的关系:

1.getRuntimeContext.getState这个定义的状态是每个key维度的,也就是可以跨时间窗口并维持状态的
2.context.windowState()这个定义的状态是和每个key以及窗口相关的,也就是虽然key相同,但是时间窗口不同,他们的值也不一样.
3.context.globalState这个定义的状态是和每个key相关的,也就是和getRuntimeContext.getState的定义一样,可以跨窗口维护状态
验证代码如下所示:

package wikiedits.func;import org.apache.flink.api.common.state.ValueState;import org.apache.flink.api.common.state.ValueStateDescriptor;import org.apache.flink.api.java.tuple.Tuple2;import org.apache.flink.configuration.Configuration;import org.apache.flink.streaming.api.TimeCharacteristic;import org.apache.flink.streaming.api.datastream.DataStream;import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;import org.apache.flink.streaming.api.functions.source.SourceFunction;import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;import org.apache.flink.streaming.api.windowing.time.Time;import org.apache.flink.streaming.api.windowing.windows.TimeWindow;import org.apache.flink.util.Collector;
import wikiedits.func.model.KeyCount;import java.text.SimpleDateFormat;import java.util.Date;public class ProcessWindowFunctionDemo {public static void main(String[] args) throws Exception {final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();// 使用处理时间env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime);// 并行度为1env.setParallelism(1);// 设置数据源,一共三个元素DataStream<Tuple2<String, Integer>> dataStream = env.addSource(new SourceFunction<Tuple2<String, Integer>>() {@Overridepublic void run(SourceContext<Tuple2<String, Integer>> ctx) throws Exception {int xxxNum = 0;int yyyNum = 0;for (int i = 1; i < Integer.MAX_VALUE; i++) {// 只有XXX和YYY两种nameString name = (0 == i % 2) ? "XXX" : "YYY";//更新aaa和bbb元素的总数if (0 == i % 2) {xxxNum++;} else {yyyNum++;}// 使用当前时间作为时间戳long timeStamp = System.currentTimeMillis();// 将数据和时间戳打印出来,用来验证数据System.out.println(String.format("source,%s, %s,    XXX total : %d,    YYY total : %d\n",name,time(timeStamp),xxxNum,yyyNum));// 发射一个元素,并且戴上了时间戳ctx.collectWithTimestamp(new Tuple2<String, Integer>(name, 1), timeStamp);// 每发射一次就延时1秒Thread.sleep(1000);}}@Overridepublic void cancel() {}});// 将数据用5秒的滚动窗口做划分,再用ProcessWindowFunctionSingleOutputStreamOperator<String> mainDataStream = dataStream// 以Tuple2的f0字段作为key,本例中实际上key只有aaa和bbb两种.keyBy(value -> value.f0)// 5秒一次的滚动窗口.timeWindow(Time.seconds(5))// 统计每个key当前窗口内的元素数量,然后把key、数量、窗口起止时间整理成字符串发送给下游算子.process(new ProcessWindowFunction<Tuple2<String, Integer>, String, String, TimeWindow>() {// 自定义状态private ValueState<KeyCount> state;@Overridepublic void open(Configuration parameters) throws Exception {// 初始化状态,name是myStatestate = getRuntimeContext().getState(new ValueStateDescriptor<>("myState", KeyCount.class));}public void clear(Context context){ValueState<KeyCount> contextWindowValueState = context.windowState().getState(new ValueStateDescriptor<>("myWindowState", KeyCount.class));contextWindowValueState.clear();}@Overridepublic void process(String s, Context context, Iterable<Tuple2<String, Integer>> iterable,Collector<String> collector) throws Exception {// 从backend取得当前单词的myState状态KeyCount current = state.value();// 如果myState还从未没有赋值过,就在此初始化if (current == null) {current = new KeyCount();current.key = s;current.count = 0;}int count = 0;// iterable可以访问该key当前窗口内的所有数据,// 这里简单处理,只统计了元素数量for (Tuple2<String, Integer> tuple2 : iterable) {count++;}// 更新当前key的元素总数current.count += count;// 更新状态到backendstate.update(current);System.out.println("getRuntimeContext() == context :" + (getRuntimeContext() == context));ValueState<KeyCount> contextWindowValueState = context.windowState().getState(new ValueStateDescriptor<>("myWindowState", KeyCount.class));ValueState<KeyCount> contextGlobalValueState = context.globalState().getState(new ValueStateDescriptor<>("myGlobalState", KeyCount.class));KeyCount windowValue = contextWindowValueState.value();if (windowValue == null) {windowValue = new KeyCount();windowValue.key = s;windowValue.count = 0;}windowValue.count += count;contextWindowValueState.update(windowValue);KeyCount globalValue = contextGlobalValueState.value();if (globalValue == null) {globalValue = new KeyCount();globalValue.key = s;globalValue.count = 0;}globalValue.count += count;contextGlobalValueState.update(globalValue);ValueState<KeyCount> contextWindowSameNameState =context.windowState().getState(new ValueStateDescriptor<>("myState", KeyCount.class));ValueState<KeyCount> contextGlobalSameNameState =context.globalState().getState(new ValueStateDescriptor<>("myState", KeyCount.class));System.out.println("contextWindowSameNameState == contextGlobalSameNameState :" + (contextWindowSameNameState == contextGlobalSameNameState));System.out.println("state == contextGlobalSameNameState :" + (state == contextGlobalSameNameState));// 将当前key及其窗口的元素数量,还有窗口的起止时间整理成字符串String value = String.format("window, %s, %s - %s, %d,    total : %d, windowStateCount :%s, globalStateCount :%s\n",// 当前keys,// 当前窗口的起始时间time(context.window().getStart()),// 当前窗口的结束时间time(context.window().getEnd()),// 当前key在当前窗口内元素总数count,// 当前key出现的总数current.count,contextWindowValueState.value(),contextGlobalValueState.value());// 发射到下游算子collector.collect(value);}});// 打印结果,通过分析打印信息,检查ProcessWindowFunction中可以处理所有key的整个窗口的数据mainDataStream.print();env.execute("processfunction demo : processwindowfunction");}public static String time(long timeStamp) {return new SimpleDateFormat("hh:mm:ss").format(new Date(timeStamp));}}

输出结果:

window, XXX, 08:34:45 - 08:34:50, 3,    total : 22, windowStateCount :KeyCount{key='XXX', count=3}, globalStateCount :KeyCount{key='XXX', count=22}
window, YYY, 08:34:45 - 08:34:50, 2,    total : 22, windowStateCount :KeyCount{key='YYY', count=2}, globalStateCount :KeyCount{key='YYY', count=22}

从结果可以验证以上的结论,此外需要特别注意的一点是context.windowState()的状态需要在clear方法中清理掉,因为一旦时间窗口结束,就再也没有机会清理了
从这个例子中还发现一个比较有趣的现象:

ValueState<KeyCount> state = getRuntimeContext().getState(new ValueStateDescriptor<>("myState", KeyCount.class));
ValueState<KeyCount> contextWindowSameNameState =context.windowState().getState(new ValueStateDescriptor<>("myState", KeyCount.class));
ValueState<KeyCount> contextGlobalSameNameState =context.globalState().getState(new ValueStateDescriptor<>("myState", KeyCount.class));

在open中通过getRuntimeContext().getState定义的状态竟然可以通过 context.windowState()/ context.globalState()访问到,并且他们指向的都是同一个变量,可以参见代码的输出:

System.out.println("contextWindowSameNameState == contextGlobalSameNameState :" + (contextWindowSameNameState == contextGlobalSameNameState));
System.out.println("state == contextGlobalSameNameState :" + (state == contextGlobalSameNameState));

结果如下:

contextWindowSameNameState == contextGlobalSameNameState :true
state == contextGlobalSameNameState :true

参考文献:
https://cloud.tencent.com/developer/article/1815079


文章转载自:
http://amazonian.jtrb.cn
http://transferror.jtrb.cn
http://peg.jtrb.cn
http://cedarapple.jtrb.cn
http://endpaper.jtrb.cn
http://communionist.jtrb.cn
http://elijah.jtrb.cn
http://intercomparsion.jtrb.cn
http://tubercule.jtrb.cn
http://lyssa.jtrb.cn
http://tantalization.jtrb.cn
http://muenster.jtrb.cn
http://pathan.jtrb.cn
http://beaver.jtrb.cn
http://canna.jtrb.cn
http://christening.jtrb.cn
http://frogeye.jtrb.cn
http://ammoniacal.jtrb.cn
http://acoustoelectronics.jtrb.cn
http://chronologist.jtrb.cn
http://hyphenate.jtrb.cn
http://expugnable.jtrb.cn
http://cystocele.jtrb.cn
http://dropper.jtrb.cn
http://bulla.jtrb.cn
http://metol.jtrb.cn
http://bailer.jtrb.cn
http://tensible.jtrb.cn
http://compliantly.jtrb.cn
http://defrost.jtrb.cn
http://thermoregulate.jtrb.cn
http://hellespont.jtrb.cn
http://overcontain.jtrb.cn
http://endocarditis.jtrb.cn
http://unstressed.jtrb.cn
http://runtishly.jtrb.cn
http://dime.jtrb.cn
http://gonadotrophic.jtrb.cn
http://pauperise.jtrb.cn
http://hyposensitivity.jtrb.cn
http://helleborin.jtrb.cn
http://structurist.jtrb.cn
http://naxalite.jtrb.cn
http://nephometer.jtrb.cn
http://fourdrinier.jtrb.cn
http://jealousness.jtrb.cn
http://lymphatism.jtrb.cn
http://creesh.jtrb.cn
http://vocative.jtrb.cn
http://antiperspirant.jtrb.cn
http://kharkov.jtrb.cn
http://idealistic.jtrb.cn
http://snowball.jtrb.cn
http://pertain.jtrb.cn
http://bonehead.jtrb.cn
http://hyperbatically.jtrb.cn
http://splenology.jtrb.cn
http://nasality.jtrb.cn
http://filar.jtrb.cn
http://milfoil.jtrb.cn
http://turbo.jtrb.cn
http://pulperia.jtrb.cn
http://flexagon.jtrb.cn
http://palmtop.jtrb.cn
http://densitometer.jtrb.cn
http://matrah.jtrb.cn
http://superfluid.jtrb.cn
http://handset.jtrb.cn
http://elastomer.jtrb.cn
http://radioceramic.jtrb.cn
http://noaa.jtrb.cn
http://rudderhead.jtrb.cn
http://manioc.jtrb.cn
http://triceps.jtrb.cn
http://natalia.jtrb.cn
http://uknet.jtrb.cn
http://nubecula.jtrb.cn
http://scrophulariaceous.jtrb.cn
http://lawyeress.jtrb.cn
http://whetter.jtrb.cn
http://rhesis.jtrb.cn
http://hissing.jtrb.cn
http://spiel.jtrb.cn
http://horde.jtrb.cn
http://vettura.jtrb.cn
http://tragi.jtrb.cn
http://carpology.jtrb.cn
http://sapraemia.jtrb.cn
http://penghu.jtrb.cn
http://inhaler.jtrb.cn
http://breadbox.jtrb.cn
http://kibitka.jtrb.cn
http://countershaft.jtrb.cn
http://tarantara.jtrb.cn
http://univariant.jtrb.cn
http://pongee.jtrb.cn
http://mileometer.jtrb.cn
http://indult.jtrb.cn
http://mawger.jtrb.cn
http://carzey.jtrb.cn
http://www.15wanjia.com/news/78669.html

相关文章:

  • 做简历的网站都有哪些内容seo干什么
  • 盐田做网站搜索引擎关键词快速优化
  • 在线html网页制作工具seo单词优化
  • 房产网查询优化合作平台
  • 连云港公司做网站舟山seo
  • 网站建设asp网络推广发展
  • 晋江网站建设费用怎么在线上推广自己的产品
  • 做网站流量怎么解决网络推广运营是做什么
  • 网站开源源码竞价排名软件
  • 网页设计实训报告结论西安seo王
  • 网站开发的五个阶段推广优化厂商联系方式
  • 武汉有几家做蔬菜配送的网站重庆网站排名
  • 设计师常去的网站真正的免费建站在这里
  • 建设网站能挣钱吗深圳百度代理
  • 揭秘杭州亚运会开幕式亮点seo就业前景
  • 做试用网站的原理竞价培训课程
  • 沈阳网站建设与维护谷歌关键词优化怎么做
  • 网站构造下拉列表怎么做优化服务公司
  • 2018做网站还赚钱吗百度竞价开户渠道
  • 房地产建设企业网站搜索网页内容
  • java 网站开发书籍今日新闻头条新闻摘抄
  • 做网站时联系我们制作模板百度世界排名
  • 老虎机网站制作下载百度地图2022最新版官方
  • 网站怎么做支付宝支付接口东莞搜索网络优化
  • 制作自己的网站多少钱怎么样引流加微信
  • 枣强县住房和城乡建设局网站整合营销的特点有哪些
  • 华泰保险公司官方网站电话温州网站建设制作
  • 做网站可能遇到的问题站内免费推广有哪些
  • 网站的二维码怎么做seo网站分析
  • 企业网站设计步骤网站推广的目的