Springboot + ReactJS + Firebase的实时数据库

huangapple go评论78阅读模式
英文:

Springboot + ReactJS + Firebase's Realtime Database

问题

Back-end: Springboot
Front-end: ReactJS
CloudRepo: Firebase Realtime Database

  1. 我有一个使用 Springboot 构建的应用,从 Firebase 实时数据库中获取数据。
  2. 然后通过 ReactJS 发出 GET 请求来提供这些数据。

我在 ReactJS 中获取的 JSON 对象如下:

Springboot + ReactJS + Firebase的实时数据库

我正在尝试将 Firebase 快照映射到 Java 集合。以下是我目前已完成的部分:

DatabaseReference ref = FirebaseService.getFirebaseDatabase().getReference("/devices/device1");
ref.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
            DeviceData message = messageSnapshot.getValue(DeviceData.class);
            System.out.println(message);
        }

        Map<String, DeviceData> document = (Map<String, DeviceData>) dataSnapshot.getValue();
        setUpdatedDocumentData(document);
    }

    public void onCancelled(DatabaseError error) {
        System.out.print("-----Error-----:\n" + error.getMessage());
    }
});

我的 POJO(普通 Java 对象)如下:

public class DeviceData {
    String sensor_1;
    String sensor_2;

    public DeviceData() {}

    public DeviceData(String sensor_1, String sensor_2) {
        this.sensor_1 = sensor_1;
        this.sensor_2 = sensor_2;
    }

    public String getSensor_1() {
        return sensor_1;
    }

    public void setSensor_1(String sensor_1) {
        this.sensor_1 = sensor_1;
    }

    public String getSensor_2() {
        return sensor_2;
    }

    public void setSensor_2(String sensor_2) {
        this.sensor_2 = sensor_2;
    }

    @Override
    public String toString() {
        return "DeviceData{" +
                "sensor_1='" + sensor_1 + '\'' +
                ", sensor_2='" + sensor_2 + '\'' +
                '}';
    }
}

但是我的日志中 sensor_1 和 sensor_2 的值都是 null。我应该如何将上述 Firebase 结构映射到集合中?

英文:

Back-end: Springboot
Front-end: ReactJS
CloudRepo: Firebase Realtime Database

  1. I have a Springboot application that GETs data from the Firebase Realtime Database.
  2. This data is then served via a GET request from reactJS.

I get the JSON object in reactJS as :

Springboot + ReactJS + Firebase的实时数据库

What I am trying to do is to map the firebase snapshot to a Java Collection. Here is What I have done so far:

DatabaseReference ref = FirebaseService.getFirebaseDatabase().getReference(&quot;/devices/device1&quot;);
ref.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
            DeviceData message = messageSnapshot.getValue(DeviceData.class);
            System.out.println(message);
        }

        Map&lt;String, DeviceData&gt; document = (Map&lt;String, DeviceData&gt;) dataSnapshot.getValue();
        setUpdatedDocumentData(document);
    }

    public void onCancelled(DatabaseError error) {
        System.out.print(&quot;-----Error-----:\n&quot; + error.getMessage());
    }
});

My POJO looks like this:

public class DeviceData {
    String sensor_1;
    String sensor_2;

    public DeviceData() {}

    public DeviceData(String sensor_1, String sensor_2) {
        this.sensor_1 = sensor_1;
        this.sensor_2 = sensor_2;
    }

    public String getSensor_1() {
        return sensor_1;
    }

    public void setSensor_1(String sensor_1) {
        this.sensor_1 = sensor_1;
    }

    public String getSensor_2() {
        return sensor_2;
    }

    public void setSensor_2(String sensor_2) {
        this.sensor_2 = sensor_2;
    }

    @Override
    public String toString() {
        return &quot;DeviceData{&quot; +
                &quot;sensor_1=&#39;&quot; + sensor_1 + &#39;\&#39;&#39; +
                &quot;, sensor_2=&#39;&quot; + sensor_2 + &#39;\&#39;&#39; +
                &#39;}&#39;;
    }
}

I am getting null in my sensor_1 and sensor_2 log. How can I map the above firebase structure to a collection?

答案1

得分: 2

在每个 /devices/$device 节点下,您有两个嵌套级别:

  1. 日期
  2. 时间

您的代码只在设备节点的子项上循环一次,因此您的 messageSnapshot 变量实际上是一个特定日期所有时间戳的所有数据的快照。

要正确处理您的结构,您需要两个嵌套循环:

DatabaseReference ref = FirebaseService.getFirebaseDatabase().getReference("/devices/device1");
ref.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot dateSnapshot: dataSnapshot.getChildren()) {
            for (DataSnapshot timeSnapshot: dateSnapshot.getChildren()) {
                DeviceData message = timeSnapshot.getValue(DeviceData.class);
                System.out.println(message);
            }
        }

        Map<String, DeviceData> document = (Map<String, DeviceData>) dataSnapshot.getValue();
        setUpdatedDocumentData(document);
    }

    public void onCancelled(DatabaseError error) {
        System.out.print("-----Error-----:\n" + error.getMessage());
    }
});

如果您只关心最新日期上的最新时间戳,您可以通过仅获取最新日期来减少从数据库中读取的数据量:

DatabaseReference ref = FirebaseService.getFirebaseDatabase().getReference("/devices/device1");
ref.orderByKey().limitToLast(1).addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot dateSnapshot: dataSnapshot.getChildren()) {
            for (DataSnapshot timeSnapshot: dateSnapshot.getChildren()) {
                DeviceData message = timeSnapshot.getValue(DeviceData.class);
                System.out.println(message);
            }
        }

        Map<String, DeviceData> document = (Map<String, DeviceData>) dataSnapshot.getValue();
        setUpdatedDocumentData(document);
    }

    public void onCancelled(DatabaseError error) {
        System.out.print("-----Error-----:\n" + error.getMessage());
    }
});

您仍然需要读取该日期的所有时间戳,但至少现在您只读取最近一天的数据。

英文:

Under each /devices/$device node, you have two nested levels:

  1. For the date
  2. For the time.

Your code only has once loop over the children of the device node, so your messageSnapshot variable is actually a snapshot with all data for all timestamps for a specific date.

To handle your structure correctly, you need two nested loops:

DatabaseReference ref = FirebaseService.getFirebaseDatabase().getReference(&quot;/devices/device1&quot;);
ref.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot dateSnapshot: dataSnapshot.getChildren()) {
            for (DataSnapshot timeSnapshot: dataSnapshot.getChildren()) {
                DeviceData message = timeSnapshot.getValue(DeviceData.class);
                System.out.println(message);
            }
        }

        Map&lt;String, DeviceData&gt; document = (Map&lt;String, DeviceData&gt;) dataSnapshot.getValue();
        setUpdatedDocumentData(document);
    }

    public void onCancelled(DatabaseError error) {
        System.out.print(&quot;-----Error-----:\n&quot; + error.getMessage());
    }
});

If you only care about the latest timestamp on the latest date, you can reduce the amount of data you read from the database by only getting the latest date:

DatabaseReference ref = FirebaseService.getFirebaseDatabase().getReference(&quot;/devices/device1&quot;);
ref.orderByKey().limitToLast(1).addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot dateSnapshot: dataSnapshot.getChildren()) {
            for (DataSnapshot timeSnapshot: dataSnapshot.getChildren()) {
                DeviceData message = timeSnapshot.getValue(DeviceData.class);
                System.out.println(message);
            }
        }

        Map&lt;String, DeviceData&gt; document = (Map&lt;String, DeviceData&gt;) dataSnapshot.getValue();
        setUpdatedDocumentData(document);
    }

    public void onCancelled(DatabaseError error) {
        System.out.print(&quot;-----Error-----:\n&quot; + error.getMessage());
    }
});

You'll still have to read all timestamps for that date, but at least you're now only reading data for the most recent day.

huangapple
  • 本文由 发表于 2020年10月18日 20:16:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64413173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定