Search code examples
apache-flink

Flink's rich function's open method


My understanding of the open() method is that it is called once upon operator initialization. My question here is wouldn't declaring a static final variable also be essentially the same?


Solution

  • My question here is wouldn't declaring a static final variable also be essentially the same?

    Yes, but it depends what you are statically defining.

    The key difference is that open() function gives you access to the underlying Flink runtime context, which can be necessary depending on your specific function and what you are defining (e.g, accessing runtime parameters, state, metrics, etc).

    For instance, if you wanted to define a database connection using runtime parameters for your job, you’d open the connection within the function itself (likewise, you’d also want to define a corresponding symmetric close() function to handle disposing of those same resources).

    If you are using constant values for all of your static instances (and don’t require any state or runtime parameters), you may not need to initialize them in the open() function. It ultimately all depends on what you are trying to do. Generally, I’d err towards using open() whenever possible.