Search code examples
javafirebasegoogle-cloud-firestoregoogle-cloud-functions

Import firestore DocumentEventData type in Java project


I'm trying to follow this tutorial.

I want to be able to write Cloud Function event triggers in my Visual Studio code Java 17 Runtime project. In the above link, there is a snippet I'm trying to copy. But I'm unable to resolve the import one error.

import com.google.events.cloud.firestore.v1.DocumentEventData; The import com.google.events cannot be resolvedJava(268435846)

The other imports, I was able to fix by modifying the pom.xml file. Below are the snippet and the current pom file. I have tried adding different dependencies on the POM file, still unable to fix it.

import com.google.cloud.functions.CloudEventsFunction;
import com.google.events.cloud.firestore.v1.DocumentEventData;
import com.google.protobuf.InvalidProtocolBufferException;
import io.cloudevents.CloudEvent;
import java.util.logging.Logger;

public class FirebaseFirestore implements CloudEventsFunction {
  private static final Logger logger = Logger.getLogger(FirebaseFirestore.class.getName());

  @Override
  public void accept(CloudEvent event) throws InvalidProtocolBufferException {
    DocumentEventData firestorEventData = DocumentEventData.parseFrom(event.getData().toBytes());

    logger.info("Function triggered by event on: " + event.getSource());
    logger.info("Event type: " + event.getType());

    logger.info("Old value:");
    logger.info(firestorEventData.getOldValue().toString());

    logger.info("New value:");
    logger.info(firestorEventData.getValue().toString());
  }
}
 <dependencies>
    <dependency>
      <groupId>com.google.cloud.functions</groupId>
      <artifactId>functions-framework-api</artifactId>
      <version>1.1.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>4.27.1</version>
    </dependency>
     <dependency>
      <groupId>com.google.firebase</groupId>
      <artifactId>firebase-admin</artifactId>
      <version>9.3.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-core</artifactId>
      <version>2.39.0</version>
    </dependency>
  </dependencies>

Solution

  • Since you're using Java, in order to make it work, as @DazWilkin mentioned in his comment, you have to use google-cloudevent-types which is available in the Maven Repository, and then you'll be able to import the DocumentEventData class.

    Add the following to your pom.xml:

    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloudevent-types</artifactId>
        <version>0.15.0</version>
    </dependency>