如何利用/导入Flink的TestHarness类和bazel?


0

我的直觉可能是正确的使用包装和/或分类器。为了找到构建文件的正确导入标记,我建议访问.cache/bazel/bazel_user/../external/maven/Build文件

将帖子编辑为 job 设置

我想用Bazel构建工具 test我自己的async flink流操作符richayncfunction。

这与如何利用Flink的TestHarness类基本相同?

主要问题是我找不到进口货org.apache.flink网站.streaming.util.OneInputStreamOperatorTestHarness

我试图遵循提供的答案,但我对bazel语法感到困惑,问自己是否可以将这些导入与bazel一起使用。

我正在使用最新的Bazel版本和intellij2019.3.4和Bazel插件。

chaining接的答案假设这个Maven依赖关系:

<dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-test-utils-junit</artifactId>
    <version>${flink.version}</version>
</dependency>

<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_2.11</artifactId>
<version>${flink.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>

<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-runtime_2.11</artifactId>
<version>${flink.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>

我的 job 区看起来像这样

...
http_archive(
    name = "rules_jvm_external",
    sha256 = RULES_JVM_EXTERNAL_SHA,
    strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
    url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)

load("@rules_jvm_external//:defs.bzl", "maven_install")
load("@rules_jvm_external//:defs.bzl", "artifact")
load("@rules_jvm_external//:specs.bzl", "maven")
load("@io_grpc_grpc_java//:repositories.bzl", "IO_GRPC_GRPC_JAVA_ARTIFACTS")
load("@io_grpc_grpc_java//:repositories.bzl", "IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS")

maven_install(
artifacts = [
...
"org.apache.commons:commons-lang3:3.9",
"org.javatuples:javatuples:1.2",
"junit:junit:4.13",
"org.testcontainers:testcontainers:1.14.1",
"org.testcontainers:kafka:1.14.1",
"org.testcontainers:postgresql:1.14.1",
"commons-io:commons-io:2.6",
"com.google.code.findbugs:jsr305:1.3.9",
"com.google.errorprone:error_prone_annotations:2.0.18",
"com.google.j2objc:j2objc-annotations:1.1",
"com.google.protobuf:protobuf-java:3.11.1",
"com.google.protobuf:protobuf-java-util:3.6.1",
"info.picocli:picocli:4.1.0",
"org.slf4j:slf4j-log4j12:1.7.5",
"org.slf4j:slf4j-api:1.7.28",
"com.github.jasync-sql:jasync-postgresql:1.0.11",
"com.github.jasync-sql:jasync-common:1.0.11",
"org.postgresql:postgresql:42.2.5",
"org.mongodb:mongodb-driver-reactivestreams:4.0.2",
"org.mongodb:mongodb-driver-core:4.0.2",
"org.mongodb:bson:4.0.2",
"org.reactivestreams:reactive-streams:1.0.3",
"joda-time:joda-time:2.9.7",
"org.apache.kafka:kafka-clients:2.4.0",
# "io.grpc:grpc-netty-shaded:%s" % GRPC_JAVA_VERSION,
# "io.grpc:grpc-protobuf:%s" % GRPC_JAVA_VERSION,
# "io.grpc:grpc-stub:%s" % GRPC_JAVA_VERSION,
"org.apache.flink:flink-core:%s" % FLINK_VERSION,
"org.apache.flink:flink-java:%s" % FLINK_VERSION,
"org.apache.flink:flink-streaming-java_%s:%s" % (SCALA_VERSION, FLINK_VERSION),
"org.apache.flink:flink-connector-kafka-0.11_%s:%s" % (SCALA_VERSION, FLINK_VERSION),
"org.apache.flink:flink-cep_2.11:%s" % FLINK_VERSION,
] + IO_GRPC_GRPC_JAVA_ARTIFACTS,
generate_compat_repositories = True,
override_targets = IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS,
repositories = [
"https://jcenter.bintray.com/",
"https://maven.google.com",
"https://repo1.maven.org/maven2",
],

)

maven_install(
name = "testing",
artifacts = [
maven.artifact(
group = "org.apache.flink",
artifact = "flink-runtime_2.11",
version = FLINK_VERSION,
classifier = "tests",
packaging = "test-jar",
),
maven.artifact(
group = "org.apache.flink",
artifact = "flink-streaming-java_2.11",
version = FLINK_VERSION,
classifier = "tests",
packaging = "test-jar",
),
maven.artifact(
group = "org.apache.flink",
artifact = "flink-test-utils-junit",
version = FLINK_VERSION,
),
"org.apache.flink:flink-tests:%s" % FLINK_VERSION,
],
repositories = [
"https://jcenter.bintray.com/",
"https://maven.google.com",
"https://repo1.maven.org/maven2",
],
)

load("@maven//:compat.bzl", "compat_repositories")

compat_repositories()

...

testmaven install“group”是我试图弄清楚如何导入所需的依赖项,因为据我所知,Bazels-maven语法中没有明确的“type”和“scope”。

test生成文件如下所示

load("@rules_java//java:defs.bzl", "java_test")
load("@rules_jvm_external//:specs.bzl", "maven")
load("@rules_jvm_external//:defs.bzl", "artifact", "maven_install")

java_test(
name = "some_module",
size = "medium",
srcs = ["DataOperatorTests.java"],
tags = [
"docker",
"integration",
],
test_class = "org.some.project.DataOperatorTests",
deps = [

    "@maven//:com_google_protobuf_protobuf_java",
    "@maven//:junit_junit_4_13",
    "@maven//:org_apache_flink_flink_connector_kafka_0_11_2_11",
    "@maven//:org_apache_flink_flink_core",
    "@maven//:org_apache_flink_flink_java",
    "@maven//:org_javatuples_javatuples",
    "@maven//:org_mongodb_bson",
    "@maven//:org_mongodb_mongodb_driver_core",
    "@maven//:org_mongodb_mongodb_driver_reactivestreams",
    "@maven//:org_testcontainers_kafka",
    "@maven//:org_testcontainers_testcontainers",
    "@maven//:org_apache_flink_flink_streaming_java_2_11",
    "@testing//:org_apache_flink_flink_runtime_2_11_tests",
    "@testing//:org_apache_flink_flink_streaming_java_2_11_tests",
    "@testing//:org_apache_flink_flink_test_utils_junit",
    "@testing//:org_apache_flink_flink_tests",
],

)

test_suite(name = "smoke",tags = ["-docker","-internal",],)

test_suite(name = "unit",tags = ["-internal","unit",],)

test_suite(name = "integration",tags = ["-internal","integration",],)

test_suite(name = "internal")

使用上述设置,以下错误不再发生-请留待参考

包似乎存在,但在实际的 test构建文件中,我在同步过程中得到了这个错误。

Error:(5, 1) no such target '@testing//:org_apache_flink_flink_streaming_java_2_11': target 'org_apache_flink_flink_streaming_java_2_11' not declared in package '' defined by /home/user/.cache/bazel/_bazel_user/7b62e4e31c70ee640c6d33972433da28/external/testing/BUILD and referenced by '//core/src/test/java/org/some/project/some_module:some_module'

1 答案


0

我自己解决了这个问题。将上述代码调整为合适的解决方案。

总而言之。

当maven存储库如下所示:

<dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-test-utils-junit</artifactId>
    <version>${flink.version}</version>
</dependency>

<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_2.11</artifactId>
<version>${flink.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>

<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-runtime_2.11</artifactId>
<version>${flink.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>

相应的Bazel job 区文件必须具有如下条目:

load("@rules_jvm_external//:defs.bzl", "maven_install")
load("@rules_jvm_external//:defs.bzl", "artifact")
load("@rules_jvm_external//:specs.bzl", "maven")

maven_install(
...
artifacts = [
maven.artifact(
group = "org.apache.flink",
artifact = "flink-runtime_2.11",
version = FLINK_VERSION,
classifier = "tests",
packaging = "test-jar",
),
maven.artifact(
group = "org.apache.flink",
artifact = "flink-streaming-java_2.11",
version = FLINK_VERSION,
classifier = "tests",
packaging = "test-jar",
),
maven.artifact(
group = "org.apache.flink",
artifact = "flink-test-utils-junit",
version = FLINK_VERSION,
),
"org.apache.flink:flink-tests:%s" % FLINK_VERSION,
],
repositories = [
"https://jcenter.bintray.com/",
"https://maven.google.com",
"https://repo1.maven.org/maven2",
],
)

详细的依赖关系 explain可以在这里找到:

要在构建文件中找到要导入的结果目标,请使用@Jin提到的方法

bazel query @maven//:all | grep flink_flink_streaming

在大多数情况下,目标可能只有后缀 test

以下是生成文件所需的导入:

load("@rules_jvm_external//:specs.bzl", "maven")
load("@rules_jvm_external//:defs.bzl", "artifact", "maven_install")

java_test(
name = "some_module",
size = "medium",
srcs = ["DataOperatorTests.java"],
tags = [
"docker",
"integration",
],
test_class = "org.some.project.DataOperatorTests",
deps = [
...
"@maven//:org_apache_flink_flink_runtime_2_11_tests",
"@maven//:org_apache_flink_flink_streaming_java_2_11_tests",
"@maven//:org_apache_flink_flink_test_utils_junit",
"@maven//:org_apache_flink_flink_tests",
],
)


我来回答

写文章

提问题

面试题