[flink]#31_扩展库:State Processor API

使用 DataSet 读取作业状态

读取现有的 Savepoint

ExecutionEnvironment bEnv = ExecutionEnvironment.getExecutionEnvironment(); ExistingSavepoint savepoint = Savepoint.load(bEnv, "hdfs://path/", new RocksDBStateBackend());

//读取 Operator State 时,只需指定算子的 uid、状态名称和类型信息。
DataSet<Integer> listState = savepoint.readListState("zhisheng-uid", "list-state", Types.INT);
DataSet<Integer> unionState = savepoint.readUnionState("zhisheng-uid", "unionstate", Types.INT);
DataSet<Tuple2<Integer, Integer>> broadcastState =savepoint.readBroadcastState("zhisheng-uid", "broadcast-state", Types.INT, Types.INT);


//在状态描述符(StateDescriptor)中使用了自定义类型序列化器 TypeSerializer
DataSet<Integer> listState = savepoint.readListState( "zhisheng-uid", "list-state", Types.INT, new MyCustomIntSerializer());


写入新的 Savepoint

public class Account { public int id;

public double amount;

public long timestamp;

}

public class AccountBootstrapper extends KeyedStateBootstrapFunction<Integer, Account> { ValueState<Double> state;

	@Override public void open(Configuration parameters) { 
		ValueStateDescriptor<Double> descriptor = new ValueStateDescriptor<> ("total",Types.DOUBLE); 
		state = getRuntimeContext().getState(descriptor); 
	}

	@Override public void processElement(Account value, Context ctx) throws Exception { 
		state.update(value.amount); 
	}

}


ExecutionEnvironment bEnv = ExecutionEnvironment.getExecutionEnvironment();

DataSet<Account> accountDataSet = bEnv.fromCollection(accounts);

BootstrapTransformation<Account> transformation = OperatorTransformation .bootstrapWith(accountDataSet) .keyBy(acc -> acc.id) .transform(new AccountBootstrapper());

Savepoint

.create(backend, 128) .withOperator("uid1", transformation1) .withOperator("uid2", transformation2) .write(savepointPath);

修改现有的 Savepoint

Savepoint
.load(backend, oldPath) .withOperator("uid", transformation) .write(newPath);


Savepoint
.removeOperator(oldOperatorUid) .withOperator(oldOperatorUid, transformation) .write(path)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值