0

not an android question.

Method intended to query the 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?
}

I've entered data manually through the web interface. How do I query Firebase? Something analogous to SELECT * FROM <TABLE> is what I'm looking for.

JSON data export:

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

see also:

how to get all child list from Firebase android

Thufir
  • 8,216
  • 28
  • 125
  • 273
  • 1
    In general interacting with the Firebase Database is the same between Android and other Java environments. Reading lists of data is explained here: https://firebase.google.com/docs/database/android/lists-of-data#child-events and here: https://firebase.google.com/docs/database/admin/retrieve-data#section-event-types What does your data structure look like (as JSON text, no screenshots please)? You can get this by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jul 02 '17 at 22:35
  • I'll update momentarily with JSON. If I can get the JSON tree that will get me headed in the right direction. – Thufir Jul 02 '17 at 22:41

1 Answers1

2

Yes. Really.

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();
  }
});

See the Firebase documentation on dealing with lists for more information. I also recommend reading my answer here: https://stackoverflow.com/a/30943084/209103

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807