forked from arvindsh/YelpDatasetSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_ConvertToGraph.sql
More file actions
145 lines (130 loc) · 5.29 KB
/
Copy path2_ConvertToGraph.sql
File metadata and controls
145 lines (130 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
This sample code shows how to import and work with the Yelp Dataset (https://www.yelp.com/dataset_challenge) using Microsoft SQL Server 2017.
Please first review the terms of use for the dataset on the Yelp website (https://www.yelp.com/html/pdf/Dataset_Challenge_Academic_Dataset_Agreement.pdf).
*/
-- Database creation for the Yelp Dataset in SQL Server 2017. Please read the comments at the bottom as well.
Use YelpReviews
GO
-- This is a 'node' table to store users in a graph data structure inside of the YelpReviews SQL Server 2017 database
CREATE TABLE [dbo].[FinalUser](
[user_id] [varchar](100) PRIMARY KEY NOT NULL,
[user_name] [varchar](1000) NULL,
[review_count] [int] NULL,
[yelping_since] [date] NULL,
[is_useful] [bit] NULL,
[is_funny] [bit] NULL,
[is_cool] [bit] NULL,
[fans] [int] NULL,
[elite] [nvarchar](max) NULL,
[average_stars] [float] NULL,
[compliment_hot] [int] NULL,
[compliment_more] [int] NULL,
[compliment_profile] [int] NULL,
[compliment_cute] [int] NULL,
[compliment_list] [int] NULL,
[compliment_note] [int] NULL,
[compliment_plain] [int] NULL,
[compliment_cool] [int] NULL,
[compliment_funny] [int] NULL,
[compliment_writer] [int] NULL,
[compliment_photos] [int] NULL
) AS NODE
GO
-- for performance purposes, disable default indexes on node table prior to such large insert operations
ALTER INDEX ALL ON [FinalUser] disable;
GO
-- Insert existing data from the imported Yelp dataset into a set of nodes, each representing an user
-- The attributes for each user are stored in the Node table itself
INSERT [FinalUser]
SELECT [user_id]
,[user_name]
,[review_count]
,[yelping_since]
,[is_useful]
,[is_funny]
,[is_cool]
,[fans]
,[elite]
,[average_stars]
,[compliment_hot]
,[compliment_more]
,[compliment_profile]
,[compliment_cute]
,[compliment_list]
,[compliment_note]
,[compliment_plain]
,[compliment_cool]
,[compliment_funny]
,[compliment_writer]
,[compliment_photos]
FROM [dbo].[YelpUser]
GO
-- Large insert is completed, enable (rebuild) default index on node table
ALTER INDEX ALL ON [FinalUser] REBUILD;
GO
-- for performance purposes, disable default indexes on edge table prior to such large insert operations
ALTER INDEX ALL ON [FinalFriend] DISABLE;
GO
-- This is an 'edge' table to store the relationships ('friend of') in an edge table
-- For demonstration purposes we store a representative attribute of the *relationship*
-- which in this case is a computed value of the total number of reviews submitted by
-- the two users who are friends with each other
CREATE TABLE [dbo].[FinalFriend]
(
CombinedReviews int
)
AS EDGE
GO
-- Actually insert the edge data. To do this we are using the $node_id values from the
-- node table for each user, and combining the review_count for each of the 2 users
INSERT FinalFriend ($FROM_ID, $TO_ID, CombinedReviews)
SELECT U.$NODE_ID,
F.$NODE_ID,
U.review_count + F.review_count
FROM YelpUserFriend AS UF1
INNER JOIN
FinalUser AS U
ON UF1.user_id = U.user_id
INNER JOIN
FinalUser AS F
ON UF1.friend_user_id = F.user_id;
GO
-- Large insert is completed, enable (rebuild) default index on edge table
ALTER INDEX ALL ON [FinalFriend] REBUILD;
GO
-- Create an additional index to help in the performance of queries later on
CREATE NONCLUSTERED INDEX NC_FinalFriend_From_To
ON [dbo].[FinalFriend] ($from_id, $to_id)
-- A quick look at the data we have in the SQL graph representation
-- Node the $node_id values in the FinalUser table
SELECT TOP 10 *
FROM FinalUser;
GO
-- Note the $edge_id, $from_id and $to_id values in the FinalFriend table
-- the $from_id and $to_id are actually $node_id values in the corresponding Node table
SELECT TOP 10 *
FROM FinalFriend;
GO
-- Note that there is missing User data for some friend_user_id values
-- So this causes the number of 'edge' table entries to be quite low
-- compared to what you will see in YelpUserFriend!
-- This is a dataset limitation and not an artifact of the import process.
SELECT TOP 50 *
FROM YelpUserFriend
WHERE friend_user_id NOT IN (SELECT user_id
FROM YelpUser);
/*
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This sample code is not supported under any Microsoft standard support program or service.
The entire risk arising out of the use or performance of the sample scripts and documentation remains with you.
In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts
be liable for any damages whatsoever (including, without limitation, damages for loss of business profits,
business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability
to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
*/