2021-05-26

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.statefun.flink.core.StatefulFunctionsConfig;
import org.apache.flink.statefun.flink.core.message.MessageFactoryType;
import org.apache.flink.statefun.flink.core.message.RoutableMessage;
import org.apache.flink.statefun.flink.core.message.RoutableMessageBuilder;
import org.apache.flink.statefun.flink.datastream.StatefulFunctionDataStreamBuilder;
import org.apache.flink.statefun.flink.datastream.StatefulFunctionEgressStreams;
import org.apache.flink.statefun.sdk.Context;
import org.apache.flink.statefun.sdk.FunctionType;
import org.apache.flink.statefun.sdk.StatefulFunction;
import org.apache.flink.statefun.sdk.annotations.Persisted;
import org.apache.flink.statefun.sdk.io.EgressIdentifier;
import org.apache.flink.statefun.sdk.state.PersistedValue;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.PrintSinkFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

import javax.annotation.Nullable;
import java.net.URI;
import java.time.Duration;
import java.util.concurrent.ThreadLocalRandom;

import static org.apache.flink.statefun.flink.datastream.RequestReplyFunctionBuilder.requestReplyFunctionBuilder;

/**
 * @author chenzhuoyu
 * @date 2021/5/26 17:48
 */
public class test {
    private static EgressIdentifier<String> GREETINGS = new EgressIdentifier<>("example", "greetings", String.class);


    public static void main(String[] args) throws Exception {


        FunctionType GREET = new FunctionType("example", "greet");
        FunctionType REMOTE_GREET = new FunctionType("example", "remote-greet");
//        FunctionType REMOTE_GREET = new FunctionType("example", "remote-greet");

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        StatefulFunctionsConfig statefunConfig = StatefulFunctionsConfig.fromEnvironment(env);
        statefunConfig.setGlobalConfiguration("someGlobalConfig", "foobar");
        statefunConfig.setFactoryType(MessageFactoryType.WITH_KRYO_PAYLOADS);

        DataStream<String> names = env.addSource(new NameSource());

        DataStream<RoutableMessage> namesIngress =
                names.map(name ->
                        RoutableMessageBuilder.builder()
                                .withTargetAddress(new FunctionType("example", "greet"), name)
                                .withMessageBody(name)
                                .build()
                );

        StatefulFunctionEgressStreams egresses =
                StatefulFunctionDataStreamBuilder.builder("example")
                        .withDataStreamAsIngress(namesIngress)
                        .withFunctionProvider(GREET,unused -> new MyFunction())
//                        .withRequestReplyRemoteFunction(
//                                requestReplyFunctionBuilder(
//                                        REMOTE_GREET, URI.create("http://localhost:5000/statefun"))
//                                        .withPersistedState("seen_count")
//                                        .withMaxRequestDuration(Duration.ofSeconds(15))
//                                        .withMaxNumBatchRequests(500))
                        .withEgressId(GREETINGS)
                        .build(env);



        DataStream<String> output = egresses.getDataStreamForEgressId(GREETINGS);
//
//    // -----------------------------------------------------------------------------------------
//    // the rest of the pipeline
//    // -----------------------------------------------------------------------------------------
//
        output.map(
                new RichMapFunction<String, String>() {
                    @Override
                    public String map(String value) {
                        return "'" + value + "'";
                    }
                })
                .addSink(new PrintSinkFunction<>());

        env.execute();


    }

    private static class MyFunction implements StatefulFunction {
        @Persisted
        private final PersistedValue<Integer> seenCount = PersistedValue.of("seen", Integer.class);
        @Override
        public void invoke(Context context, Object input) {
            int seen = seenCount.updateAndGet(MyFunction::increment);
            context.send(GREETINGS, String.format("Hello %s at the %d-th time", input, seen));

        }
        private static int increment(@Nullable Integer n) {
            return n == null ? 1 : n + 1;
        }
    }
    private static final class NameSource implements SourceFunction<String> {

        private static final long serialVersionUID = 1;

        private volatile boolean canceled;

        @Override
        public void run(SourceContext<String> ctx) throws InterruptedException {
            String[] names = {"Stephan", "Igal", "Gordon", "Seth", "Marta"};
            ThreadLocalRandom random = ThreadLocalRandom.current();
            while (true) {
                int index = random.nextInt(names.length);
                final String name = names[index];
                synchronized (ctx.getCheckpointLock()) {
                    if (canceled) {
                        return;
                    }
                    ctx.collect(name);
                }
                Thread.sleep(1000);
            }
        }

        @Override
        public void cancel() {
            canceled = true;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2021-03-26 20:54:33,596 - Model - INFO - Epoch 1 (1/200): 2021-03-26 20:57:40,380 - Model - INFO - Train Instance Accuracy: 0.571037 2021-03-26 20:58:16,623 - Model - INFO - Test Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Best Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Save model... 2021-03-26 20:58:16,623 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 20:58:16,698 - Model - INFO - Epoch 2 (2/200): 2021-03-26 21:01:26,685 - Model - INFO - Train Instance Accuracy: 0.727947 2021-03-26 21:02:03,642 - Model - INFO - Test Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Best Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Save model... 2021-03-26 21:02:03,643 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 21:02:03,746 - Model - INFO - Epoch 3 (3/200): 2021-03-26 21:05:15,349 - Model - INFO - Train Instance Accuracy: 0.781606 2021-03-26 21:05:51,538 - Model - INFO - Test Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,538 - Model - INFO - Best Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,539 - Model - INFO - Save model... 2021-03-26 21:05:51,539 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 我有类似于这样的一段txt文件,请你帮我写一段代码来可视化这些训练结果
02-06

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值