查询Firebase DatabaseReference的惯用法是什么?

时间:2017-07-02 22:16:44

标签: java firebase firebase-realtime-database

不是一个安卓问题。

用于查询数据库的方法:

private void db() throws FileNotFoundException, IOException {
    FileInputStream serviceAccount = new FileInputStream("serviceAccountKey.json");
    FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
            .setDatabaseUrl(stringURL)
            .build();

    log.info(options.toString());

    FirebaseApp firebaseApp = FirebaseApp.initializeApp(options);
    FirebaseDatabase f = FirebaseDatabase.getInstance(firebaseApp, stringURL);

    DatabaseReference databaseNonRoot = f.getReference();
    DatabaseReference databaseReference = databaseNonRoot.getRoot();

    log.info(databaseReference.toString());

    //    databaseReference.addValueEventListener(new ValueEventListener(){
    //really?
}

我通过网络界面输入了数据manually。如何查询Firebase?我正在寻找与SELECT * FROM <TABLE>类似的东西。

JSON数据导出:

{
  "abc" : 123,
  "bar" : 4342342,
  "foo" : 432434
}

另见:

how to get all child list from Firebase android

1 个答案:

答案 0 :(得分:2)

是。真。

databaseReference.addValueEventListener(new ValueEventListener(){
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
      System.out.println("Key="+childSnapshot.getKey()+" Value="+childSnapshot.getValue());
    }
  }

  @Override
  public void onCancelled(DatabaseError databaseError) {
    throw databaseError.toException();
  }
});

有关详细信息,请参阅Firebase documentation on dealing with lists。我还建议在这里阅读我的答案:https://stackoverflow.com/a/30943084/209103