How to structure a Firebase Realtime-Database? - java

I am trying to set up a Google Firebase Realtime Database to store user data. I am really new to Java and Firebase and until now user data gets saved just under the Uid. I now want to have different categories like Users, Posts, Messages... How can I add such a structure to my Database? I will link a Screenshot of my Database.
I am completely new to those things​ and don't even know in which file I can add such a structure.

You can structure your database something like this :
Users :
Users -> uid(With users information.)
Posts
Posts -> user_id -> post_id(with post details)
Messages :
Message -> user_id -> messages (with message and receiver id.)

Related

Firebase Realtime Database Download Data Size Problem

1.DatabaseReference db=FirebaseDatabase.getInstance().getReference("users");
1.Query query = db.orderByKey().equalTo(uid);
2.FirebaseDatabase.getInstance().getReference("users").orderByChild("token").equalTo(toKen)
Does any one of the above two codes written in Java Android Studio send the entire "users" node of Realtime Database to the client Android App or Only the the Fetched Records will be send to the client Android App. It Seems like that it sends the complete "users" node to the client Android App, because my realtime database bill is quite high and customers are low.
It seems you are looking for orderByChild(). To get the child node where the value of uid is equal to passed UID, try this:
DatabaseReference db=FirebaseDatabase.getInstance().getReference("users");
Query query = db.orderByChild("uid").equalTo(uid);
The documentation says,
Method
Usage
orderByChild
Order results by the value of a specified child key or nested child path.
orderByKey
Order results by child keys.
The second query looks fine and should fetch the node where the value of token is equal to the supplied token.
If the download size is higher than you'd expect based on the number of query results, check if you've defined an index on the token property. If no such index exists, Firebase will download all data under users to the client, and perform the sort/filter there. If an index is declared, the ordering/filtering is done on the server.

i want to save and retrive all contacts to Firebase Firestore, need structure to save and retrive contacts

I am developing an app, there's a functionality to get all the contacts of the user and upload it to server. Here I'm using Firebase Firestore as my backend. I can able to get all contacts without duplicates. Can someone please help, in which document structure I need to save the contact details of the user? Please tell me appropriate format so that it wont take much space in Firestore.
And then..
Once all the contacts are saved, I must be able to get the name of a contact using number what user is entering. This is my requirement. in short I would like to know how to save 2500 contact details for Firestore and how to read single contact name with help of contact number what user types in the edittext. (note : I heard like we cant save 2500 contact details of the user in one shot, something like batch should be used)
I tried this below code but it is saving only first 800 contacts only.
`private void doUploadContactstoCloud() {
dialog.setCancelable(false);
dialog.setMessage("Please wait we are configuring your request ...");
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.show();
listContacts = new ContactPresenter(this).fetchAllContacts();
int j = listContacts.size();
Log.d("sizejhfg",j+"");
double size = listContacts.size();
double ind = 0;
Map<String, Object> contact = new HashMap<>();
// Get a new write batch
WriteBatch batch = db.batch();
for (Contact con : listContacts) {
for (int i = 0; i < con.numbers.size(); i++) {
String number = formatnum(con.numbers.get(i).number);
if (number != null) {
if (!number.equals("0") && number.length() < 5) {
Map<String, Object> contactnumber = new HashMap<>();
contactnumber.put("name", con.name);
contactnumber.put("number", number);
contact.put(number,contactnumber);
DocumentReference item = db.collection(tonumber).document(number);
batch.set(item,contactnumber);
ind++;
if(ind==500){
doCommit(batch);
batch=db.batch();
ind=0;
}
}
}
}
//dialog.setProgress((int) (ind / size) * 100);
}
if(ind>0){
doCommit(batch);
}
prefManager.setisContactUpload(true);
doDismissDialog();
}`
Please tell how I should save data in Firestore (structure)
please tell me to read single contact name with help of number
Please tell how I should save data in Firestore (structure)
If you are using authentication, a possible database schema for you app use-case might be:
Firestore-root
|
--- users (collection)
|
--- uid (document)
|
--- // user properties
|
--- contacts (subcollection)
|
--- contactId (document)
|
--- phoneNumber: 123456789
|
--- // other contact properties
To get all contacts a user has, simply attach a listener on contacts reference:
db.collection("users").document(uid).collection("contacts");
please tell me to read single contact name with help of number
To get the details of single contact having the phone number, please use the following query:
db.collection("users").document(uid).collection("contacts").whereEqualTo("phoneNumber", 123456789);
According to the official documentation regarding the maximum number of documents that can be added in single batch:
Each transaction or batch of writes can write to a maximum of 500 documents.
So you have to split your contacts in parts of 500 documents in order to be able to add all those contacts to Firestore.
Edit:
if I assume one user is having 4000 contacts in his phone (just mobile number and name*4000 = 8000 data from a single user).
That's incorrect, if you have only 2 properties (within a contact document), you'll only have 4000 write operations and not 8000 because both properties are apart of the same document. So you must write them together using a batch write or using a POJO class.
if the number of users become 5 million then it will be huge data that needs to be stored on in the Firestore.
That's correct but in the same time it means that you'll have a very successful application and you can afford all those write operations. 5 million believe me, it's much.
so I want a schema which must the best which should accommodate less space as possible
The problem is not about the space, the problem is about the number of reads and writes you perform. Evrything in Firestore is about the number of reads and writes.
I am expecting a very efficient way to save data. please help
In my optinion, the schema above can you help you solve the problem.
considering this still your answer is the best solution?
Considering your request from your question and comments, yes.
is it possible to ignore uid, contact id and can just have only these parameters
Only if you use Firebase realtime database, which is a different product. There are no collections and documents, just a JSON database structure.

How to get data from custom UID in firebase?

I have data structure like this for my android app. That can login and only see their profile. I dont make add content or do saving data to the database. So i created the data manually and the users only read and display the data.
How to make login,refer with username = "71140011" and password = "123456" which is inside real time database and the 71140011 only can see their profile only. thanks

How to graph multiple data sets in Android using Firebase database?

I'm trying to display certain data in my app using a graph.
The data is saved in Firebase database under multiple nodes, like the highlighted data in this database.
Is there any library that can be used to make real-time graphs from Firebase?
You might need to restructure your data, instead of storing the workout data in the user object, try breaking it out into it's own object.
/workouts {
/{refPush} {
date: '19-2-2018'
name: 'Test Graph 2',
set: '20',
uid: 'someUid',
}
}
Then you could access this information like;
firebase.database().ref('/workouts')
.orderByChild('set')
.equalTo('20');

how to delete multiple data using check box in google app engine (JAVA)

In Google app engine, how can i delete multiple data selected in check box. For a sample i had attached the image below. here i had selected the multiple check boxes and the data shown are stored in the Google app engine.
i have my jsp code for check box like this,
<input name="delete" type="checkbox"/>
can anyone suggest me how to select the data and delete it from Google app engine.
Edited:
For storing the data i used,
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity employee = new Entity("Employee");
employee.setProperty("First Name", fname);
datastore.put(employee);
For retrieving the data i used,
Query query = new Query("Employee");
List<Entity> emp = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(20));
for (Entity user : emp){
// inside the table
user.getProperty("File Name")}
By this i can retrieve the data.
First of all, I recommend that you use two frameworks to make your life easier:
Stripes
Objectify
OK, here you go: I'm not sure to which degree your question is a basic JSP/Servlet question or a specific Appengine question. However, you have to do the follwoing:
Have a list of checkboxes on your JSP page, like this: <stripes:checkbox name="employees[${loop.index}].id" value="${employee.id}"/> (see: http://www.stripesframework.org/display/stripes/Indexed+Properties)
Pass a list of IDs (of the entities that should be deleted) to the Servlet.
Delete the entities based on these IDs.
Here's the catch: Appengine can terminate the request before all entities are deleted. That can happen if the request takes too long. Therefore you should delete the entities asynchronously, using chunks of data. See this answer for further information.

Categories