I got a thinking-problem in DynamoDB.
My structure is looking as following:
primary key = "id"
sort key = "sort"
I have posts, users and "user A following user B" relationships.
Users:
id=1234
sort="USER_USER_1234"
name="max" (for example)
-
id=3245
sort="USER_USER_3245"
name="tom"
Post:
id=9874
sort="POST_POST_1234 (because its created by user id 1234)
createdAt=1560371687
Following:
id=1234
sort="USER_FOLLOW_3245"
--> tom follows max (but max not tom)
How could I design a query to get all posts by the people which tom(id=3245) is following? So in my case the post id 9874?
My approach was to put a GSI where sort is the primary key and id is the sort key (that i can query all people which user A is following), than get all the posts from the users (with help of the same GSI) and sort the result after a second index where createdAt is the sort key. The problem is that this needs much much querys (imagine user A would follow 10000 people and they all make posts). Is there a technique or design thinking approach which you could recommend for this situation? My second approach was to index the whole application table to elastic search and do a nested query. Would this make more sense? Or would you recommend using another type of database like AWS neptune?
There's a hands-on lab on aws about a similar problem - "a mobile application that includes a social network": https://aws.amazon.com/getting-started/hands-on/design-a-database-for-a-mobile-app-with-dynamodb/4/
Brief description:
Users will upload photos through your application
users will want to find and follow friends
By following a friend, a user will receive notifications of the friend’s new photos
user will be able to message their friends
friends can view their photos
users can react to a photo with one of four emojis — a heart, a smiley face, a thumbs up, or a pair of sunglasses.
When looking at a photo, users should be able to see the number of each type of reaction a photo has received
The model has the following entities: User, Photo, Reaction, Friendship.
A User can have many Photos, and a Photo can have many Reactions. Finally, the Friendship entity represents a many-to-many relationship between Users, as a User can follow multiple Users and be followed by multiple other Users.
Access patterns
Based on the business requirements, these are the access patterns identified:
User
Create user profile (Write)
Update user profile (Write)
Get user profile (Read)
Photo
Upload photo for user (Write)
View recent photos for user (Read)
React to a photo (Write)
View photo and reactions (Read)
Friendship
Users can follow friends, view updates on their friends’ activities, and receive recommendations on other friends they may want to follow.
A friendship is a one-way relationship, like Twitter. One user can choose to follow another user, and that user may choose to follow the user back. For our application, we will call the users that follow a user “followers”, and we will call the users that a user is following the “followed”.
Based on this information, we have the following access patterns:
Follow user (Write)
View followers for user (Read)
View followed for user (Read)
On the Friendship entity, we have an access pattern that needs to find all users that follow a particular user as well as an access pattern to find all of the users that a given user follows.
Table Design
Because of this, we’ll use a composite primary key with both a PK and SK value. The composite primary key will give us the Query ability on the PK to satisfy one of the query patterns we need:
Entity PK SK
User USER#<USERNAME> #METADATA#<USERNAME>
Photo USER#<USERNAME>. PHOTO#<USERNAME>#<TIMESTAMP>
Reaction REACTION#<USERNAME>#<TYPE> PHOTO#<USERNAME>#<TIMESTAMP>
Friendship USER#<USERNAME> #FRIEND#<FRIEND_USERNAME>
The Friendship entity uses the same PK as the User entity. This will allow you to fetch both the metadata for a user plus all of the user’s followers in a single query:
KeyConditionExpression="PK = :pk AND SK BETWEEN :metadata AND :photos",
ExpressionAttributeValues={
":pk": { "S": "USER#{}".format(username) },
":metadata": { "S": "#METADATA#{}".format(username) },
":photos": { "S": "PHOTO$" },
},
A secondary (inverted) index is useful to query the “other” side of a many-to-many relationship. This is the case for your Friendship entity. With your primary key structure, you can query all followers for a particular user with a query against the table’s primary key. When you add an inverted index, you will be able to find the users that a user is following (the “followed”) by querying the inverted index:
KeyConditionExpression="SK = :sk",
ExpressionAttributeValues={
":sk": { "S": "#FRIEND#{}".format(username) }
},
Extensions
What would be interesting is to tweak the design to support mega-popular users (having millions of followers).
Another interesting access pattern not mentioned here is the user feed - see all the photos that their friends have recently posted. This could be done with another table to contain this stream of data which gets updated whenever a friend posts something (find his followers, update their feeds...).
In Amazon Neptune, this would be something as simple as:
g.V(3245).E('post')
The above query would return an iterator, to all the vertices connected by the Edge label "post", starting from the vertex with ID "3245". You can firther tighten it up by either projecting specific properties (.property('name')) from those vertices or materializing the whole vertex (.valueMap()). This is just Gremlin syntax, and you can easily do the same using SPARQL as well, and Amazon Neptune supports both of them.
A bigger question for you is to evaluate all the types of queries you wish to perform on your data, and see if modeling it in a graph database makes sense. If it does, then you're better off using Neptune as opposed to something custom using a mix of other products. Querying/Traversing highly connected data, navigating through relationships etc are some of the classic usecases to use a graph data model.
Related
Is it possible to change Django database user based on login user. I'm using postgres db.
I'm writting this answer according to my understanding of the question.
For your question yes, for example if you have an application and its support multiple users login consider 3 to 4 kind of users like..
Normal user
Superuser
And many more...
And if you want to switch between these users you have to made some uniqueness to find them while login. For that you should add an attribute (field) to your database table (for example user_role anyway you can give your own).
Note: you should predefined all the users in this table.
And while signing up, use this user_role(u should insert 4 user_type as already mentioned above ) and make it to foreign key to your users table.
So now you saved users by giving the user_role.
While login u should send the request containing user_role along with user_name and password.
{
"user_role" : 2
"user_name(or email)" : "***#gmail.com",
"password" : "****"
}
If you are not find this answer as more relevant please elaborate your question so someone can help you.
Happy coding!!
I have a DynamoDB table called Posts in my iOS project on AWS Mobile Services. I would like to add Favourites feature to my app, so user can add any post from Posts table to favourites. How can I implement it using DynamoDB with AWS Mobile Services? Thank you!
In NoSQL, the database model is mostly depends on the Query Access Pattern (QAP). I am not sure whether the Mobile front end shows the post and the users who likes it or user page which list their favorite posts.
Case 1 : Post page showing the users who liked the post
You can have a favourites attribute to POST table to have the list of users liked the post.
favourites - SET or LIST data type
Example:-
Post 1 is favorite for user 1 and user 2
post 1
favourites : ["user1", "user2" ...]
I want to know is there any way to get user subscrition / unsubscrition to email campaign ?
Is it saved in one of databases/tables in MSSQL ?
If you use the approach with opting in and out being determined on the fact if user is in role, then it is stored in the aspnet_UsersInRoles table in your core database. This table does not keep the information when role was assigned to the user. That's why you cannot get information when user subscribed or unsubscribed to email campaign.
The only thing you can check is if user is in the role:
user.IsInRole(roleName)
The user's subscription is driven by the users role, but It is possible to get the users subscriptions in ECM, You just have to use the api.
You can get the contact from the email address:
string fullName = commonDomain + "\\" + Util.AddressToUserName(username);
var contact = Contact.FromName(fullName);
var subscriptions = contact.GetSubscriptions();
Once you have a contact you can call the GetSubscriptions() method which will return the recipient lists the user is signed up to. There are a host of other methods you can call on a contact and if there is a a way to get the date unsubscribed/subscribed it will be here.
If not reflect Sitecore.EmailCampaign.dll and keep looking! There might be some extra information in the automation states table in the Analytics database. More info on automation state here:
https://www.sitecore.net/learn/blogs/technical-blogs/sitecore-magnified/posts/2013/09/ecm-automation-states-magic.aspx
Also noticed there is a method GetUnsubscribersStatistics on the Sitecore.Modules.EmailCampaign.Core.Analytics.AnalyticsHelper class. This will have the date of unsubscription.
Before I ask give some information which i am already able to achieve:
I'm able to get the group feed in JSON format
I am accessing a group. I have got a long lived access token and i could access the feed.
Inputs I have are: group_id, acess_token.
URL USED:
https://graph.facebook.com/v2.1/{group_id}/feed?access_token={access_token}
The URL gave JSON formatted output of all the posts etc.
I'm able to achieve the albums of the group
URL USED:
https://graph.facebook.com/v2.1/{group_id}/albums?access_token={access_token}
The URL gave JSON formatted output of all the albums (their IDs, name, etc.).
What I'm NOT able to achieve:
URL USED:
https://graph.facebook.com/v2.1/{album_id}/photos?access_token={access_token}
URL USED:
https://graph.facebook.com/v2.1/{album_id}?fields=photos&access_token={access_token}
URL USED:
https://graph.facebook.com/v2.1/{album_id}/photos?fields=picture,source,name&type=uploaded&access_token={access_token}
{
"data": [
]
}
I even tried the above in the 'Graph API Explorer', I get the same output.
I have tried for various group ids to see if some group ids may be the source of the problem. But I get the same output as shown above.
I have gone through all related questions on Stack Overflow. I am using the same URL formats as mentioned in the posts. They say that they can see the photos' data in JSON.
Due to permissions restrictions it is not possible to retrieve all photos from group albums unless the user uploaded those albums themselves.
The Graph API reference mentions that one of the following is necessary to retrieve a regular album:
Any valid access token if the album is public.
A user access token user_photos permission to retrieve any albums that the session user has uploaded.
Oddly, the /{group_id}/albums edge, is undocumented. Here's what I've learned from experience about accessing group albums:
A user access token with user_photos permission will allow access to album information and photos for albums that user uploaded.
Any attempt to access any album the user did not upload (including public albums in public groups) will return album data, but not photo information.
You were using the correct queries. For completeness I will restate them here.
Album information
https://graph.facebook.com/v2.1/{group_id}/albums?access_token={access_token}
Photos
https://graph.facebook.com/v2.1/{album_id}?fields=photos&access_token={access_token}
You can still access photo attachments (but not albums) off of /{group_id}/feed
I have successfully retrieved up to 50 user's profiles using the batching method. I attempted to use the same process to retrieve these user's profile photos, but get an error 302. I see that there is a URL pointing to the photo returned in the result, but using that to retrieve each photo would defeat the purpose of batching, which is to retrieve all at once and prevent repeated HTTP requests. Is it possible to retrieve these using the batching in the Facebook API?
Try FQL:
SELECT pic_square FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 = me())
...where pic_square can be one of pic_small, pic_big, pic_square, pic
That'll give you URLs for the corresponding user's profile picture at the given size.
e.g.:
https://developers.facebook.com/tools/explorer/?method=GET&path=fql%3Fq%3DSELECT%20pic_square%20FROM%20user%20WHERE%20uid%20IN%28SELECT%20uid2%20FROM%20friend%20WHERE%20uid1%20%3D%20me%28%29%29