在eclipse上运行时,Google Dataflow作业运行正常但在使用mvn编译它时会出错,并使用java -cp命令运行该文件

时间:2021-03-25 15:38:13

I am migrating from Dataflow sdk 1.x to 2.x which uses apache beam.The code runs properly when i run it on eclipse using Run as java application.But when i compile it using mvn clean compile assembly:single and then run the class file using the command java -cp I get these logs followed by an exception

我正在从Dataflow sdk 1.x迁移到使用apache beam的2.x.当我使用Run as java application在eclipse上运行它时,代码运行正常。但是当我使用mvn clean编译程序集编译它时:单个然后运行使用命令java -cp的类文件我得到这些日志后跟一个异常

INFO: PipelineOptions.filesToStage was not specified. Defaulting to files 
from the classpath: will stage 1 files. Enable logging at DEBUG level to see 
which files will be staged.
Apr 25, 2018 11:17:01 AM com.example.LogParser parse
INFO: Currently processing file: File1.gz
Exception in thread "main" java.lang.NoSuchMethodError: 
com.google.common.base.Preconditions.checkArgument(ZLjava/lang/
String;Ljava/lang/Object;Ljava/lang/Object;)V at 
org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO$Write.expand(BigQueryIO.java:1512)
    at org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO$Write.expand(BigQueryIO.java:1041)
at org.apache.beam.sdk.Pipeline.applyInternal(Pipeline.java:537)
    at org.apache.beam.sdk.Pipeline.applyTransform(Pipeline.java:472)
    at org.apache.beam.sdk.values.PCollection.apply(PCollection.java:286)
    at com.example.LogParser.parse(LogParser.java:541)
    at 
 com.example.LogParser.main(LogParser.java:167)

I tried updating guava version but it didn't help.

我尝试更新番石榴版本,但它没有帮助。

I also noticed that when i run my class file using eclipse i get this log that differs when i run it using jar file

我还注意到,当我使用eclipse运行我的类文件时,我得到的日志在使用jar文件运行时会有所不同

INFO: PipelineOptions.filesToStage was not specified. Defaulting to files 
from the classpath: will stage 147 files. Enable logging at DEBUG level to 
see which files will be staged.

1 个解决方案

#1


1  

I was able to solve the error by adding these maven dependencies instead of maven-assembly-plugin and then using mvn package instead of mvn assembly

我能够通过添加这些maven依赖项而不是maven-assembly-plugin然后使用mvn包而不是mvn程序集来解决错误

      <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven-compiler-plugin.version}</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven-surefire-plugin.version}</version>
    <configuration>
      <parallel>all</parallel>
      <threadCount>4</threadCount>
      <redirectTestOutputToFile>true</redirectTestOutputToFile>
    </configuration>
    <dependencies>
      <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-junit47</artifactId>
        <version>${maven-surefire-plugin.version}</version>
      </dependency>
    </dependencies>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>${maven-jar-plugin.version}</version>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>${maven-shade-plugin.version}</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <finalName>${project.artifactId}-bundled-${project.version}</finalName>
          <filters>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/LICENSE</exclude>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
              </excludes>
            </filter>
          </filters>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>   

#1


1  

I was able to solve the error by adding these maven dependencies instead of maven-assembly-plugin and then using mvn package instead of mvn assembly

我能够通过添加这些maven依赖项而不是maven-assembly-plugin然后使用mvn包而不是mvn程序集来解决错误

      <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven-compiler-plugin.version}</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven-surefire-plugin.version}</version>
    <configuration>
      <parallel>all</parallel>
      <threadCount>4</threadCount>
      <redirectTestOutputToFile>true</redirectTestOutputToFile>
    </configuration>
    <dependencies>
      <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-junit47</artifactId>
        <version>${maven-surefire-plugin.version}</version>
      </dependency>
    </dependencies>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>${maven-jar-plugin.version}</version>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>${maven-shade-plugin.version}</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <finalName>${project.artifactId}-bundled-${project.version}</finalName>
          <filters>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/LICENSE</exclude>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
              </excludes>
            </filter>
          </filters>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>