简体   繁体   English

数据库中Java图形的实现

[英]Implementation of graph in Java from Database

What I have is 2 tables : 我有2张桌子:

    Users (id, name, lastname) 
    Friends (id1, id2)

Given these 2 tables, I need to be able to find the distance between 2 users d(id1,id2) 给定这两个表,我需要能够找到2个用户d(id1,id2)之间的距离

I defined a User class, that holds each of the user attribute in the table. 我定义了一个User类,该类保存表中的每个user属性。

I need to build a graph. 我需要建立一个图形。 My data structure for the graph is 我的图表数据结构是

     Map<User, Set<User>>

mapping a user to a set of his friends. 将用户映射到他的一组朋友。 How am I building the graph ? 如何建立图表? I queried the db for the ids of all the users in the db. 我向数据库查询数据库中所有用户的ID。 I got an 我有一个

   int[] userids

Then for each int in this array: 然后对于此数组中的每个int:

(1) I build a User object, fetching in the db the attributes of that user (1)我建立一个User对象,在db中获取该用户的属性

(2) I query the Friends table in the db to have the ids of the friends of that user : (2)我查询数据库中的Friends表以获取该用户的朋友的ID:

    int [] friends

(3) for each int in this friends array, I build a User object fetching in the db the attributes of that user and add it to (3)对于这个friends数组中的每个int,我构建一个User对象,在db中获取该用户的属性并将其添加到

    Set<User> friends = new Set<User>();

Question 1 : Any ideas how to do this better ? 问题1:有什么想法可以更好地做到这一点? It takes forever, given that I have 500 users, and 20000 entries in the Friends table ... 假设我有500个用户,并且在Friends表中有20000个条目,这将永远花费...

The big problem here is that when 2 users are the "same" in the database, they are referenced in differents objects in my graph !! 这里最大的问题是,当2个用户在数据库中为“相同”用户时,它们在我的图形的differents对象中被引用!

Which is messing up my distance algorithm. 这弄乱了我的距离算法。 I start with a User u, get his Friends {f1,f2} , and when I wanna get the friends of friends using graph.get(f1) and graph.get(f2) I get null (for the reason stated in my problems, ie 1 db user in many different User objects) 我从用户u开始,得到他的Friends {f1,f2},当我想使用graph.get(f1)和graph.get(f2)成为朋友时,我得到null(由于我的问题中所述的原因) ,即在许多不同的User对象中有1个db用户)

I need to find a way to build my graph such that 1 given user say (1, John, Doe) is referenced in one and only User object in the heap ... 我需要找到一种构建图的方法,以便在一个堆中仅引用一个给定的用户说(1,John,Doe)(仅一个User对象)。

Question 2 : How ?? 问题2:如何?

Thank you so much 非常感谢

Help with Java Graph Implementation Java Graph实施帮助

Reading one user at a time is going to be slow, that's lots of round-trips to the database. 一次读取一个用户将很慢,这是数据库的许多往返。 It will be especially slow if there is no index on the Friends table. 如果Friends表上没有索引,它将特别慢。

You are better off just grabbing all the data from the database and building the graph in Java yourself. 您最好只从数据库中获取所有数据并自己用Java构建图形。

select UserId, ... from Users;

as you are doing now, to build a User for each user. 因为你现在做的,建立一个User为每个用户。 You'll want to create a Map from userId to User for use in step 2 (so you don't get multiple User objects for the same userId ). 您将要创建一个从userIdUserMap ,以在第2步中使用(这样就不会为同一userId获得多个User对象)。

select id1, id2 from Friends;

Then look up the two ids in the Map above, then add each User to the other User 's outbound edge set. 然后在上方的Map查找两个ID,然后将每个User添加到另一个User的出站边缘集中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM