@@ -52,6 +52,7 @@ public static void main(String[] args) throws Exception {
52
52
File file = new File (Main .class .getClassLoader ().getResource ("quickstart_dataset.json" ).toURI ());
53
53
List <Book > books = new ObjectMapper ().readValue (new FileInputStream (file ), new TypeReference <>() {});
54
54
55
+ // Populate the Table
55
56
/**
56
57
* Ideally we would do
57
58
* table.insertMany(books);
@@ -69,119 +70,9 @@ public static void main(String[] args) throws Exception {
69
70
}).toList ();
70
71
database .getTable ("quickstart_table" ).insertMany (rows );
71
72
72
- //updateTable(table);
73
- // Filter filter = Filters.and(
74
- // Filters.eq("title", "Hidden Shadows of the Past"),
75
- // Filters.eq("author", "John Anthony"));
76
- // table.find(filter).toList().forEach(row -> System.out.println(row));
77
- }
78
-
79
- public static void populateTable (Table <Row > table ) throws Exception {
80
- // Initialize Jackson ObjectMapper
81
- ObjectMapper objectMapper = new ObjectMapper ();
82
- File file = new File (Main .class .getClassLoader ().getResource ("quickstart_dataset.json" ).toURI ());
83
-
84
- try (FileInputStream stream = new FileInputStream (file )) {
85
- List <Row > rows = objectMapper .readValue (stream , new TypeReference <>() {});
86
- rows .forEach (
87
- row -> {
88
- // Deserialize the "genres" field into a HashSet
89
- row .add ("genres" , new HashSet <>(row .getList ("genres" , String .class )));
90
-
91
- // Deserialize the "metadata" field into a Map
92
- Map <String , String > metadataMap =
93
- objectMapper .convertValue (row .get ("metadata" ), new TypeReference <Map <String , String >>() {});
94
- row .add ("metadata" , metadataMap );
95
-
96
- // Deserialize the "dueDate" field into a Date or null
97
- row .add ("dueDate" , parseDate (row .getText ("dueDate" )));
98
-
99
- // Add a field of text to vectorize
100
- String summary = row .getText ("summary" );
101
- String genres = String .join (", " , row .getList ("genres" , String .class ));
102
- String summaryGenresVector =
103
- String .format ("summary: %s | genres: %s" , summary , genres );
104
- row .add ("summaryGenresVector" , summaryGenresVector );
105
- });
106
-
107
- TableInsertManyResult result = table .insertMany (rows );
108
- System .out .println ("Inserted " + result .getInsertedIds ().size () + " items." );
109
- }
110
- }
111
-
112
- public static void searchTable (Table <Row > table ) {
113
- // Find rows that match a filter
114
- System .out .println ("\n Finding books with rating greater than 4.7..." );
115
-
116
- Filter filter = Filters .gt ("rating" , 4.7 );
117
-
118
- TableFindOptions options = new TableFindOptions ()
119
- .limit (10 )
120
- .projection (include ("title" , "rating" ));
121
-
122
- table
123
- .find (filter , options )
124
- .forEach (
125
- row -> {
126
- System .out .println (row .get ("title" ) + " is rated " + row .get ("rating" ));
127
- });
128
-
129
- // Perform a vector search to find the closest match to a search string
130
- System .out .println ("\n Using vector search to find a single scary novel..." );
131
-
132
- TableFindOneOptions options2 =
133
- new TableFindOneOptions ()
134
- .sort (Sort .vectorize ("summaryGenresVector" , "A scary novel" ))
135
- .projection (include ("title" ));
136
-
137
- table
138
- .findOne (options2 )
139
- .ifPresent (
140
- row -> {
141
- System .out .println (row .get ("title" ) + " is a scary novel" );
142
- });
143
-
144
- // Combine a filter, vector search, and projection to find the 3 books with
145
- // more than 400 pages that are the closest matches to a search string
146
- System .out .println (
147
- "\n Using filters and vector search to find 3 books with more than 400 pages that are set in the arctic, returning just the title and author..." );
148
-
149
- Filter filter3 = Filters .gt ("numberOfPages" , 400 );
150
-
151
- TableFindOptions options3 =
152
- new TableFindOptions ()
153
- .limit (3 )
154
- .sort (Sort .vectorize ("summaryGenresVector" , "A book set in the arctic" ))
155
- .projection (include ("title" , "author" ));
156
-
157
- table
158
- .find (filter3 , options3 )
159
- .forEach (
160
- row -> {
161
- System .out .println (row );
162
- });
163
- }
164
-
165
- public static void updateTable (Table <Row > table ) {
166
- // Update a row
167
- Filter filter = new Filter (Map .of (
168
- "title" , "Hidden Shadows of the Past" ,
169
- "author" , "John Anthony" ));
170
- TableUpdateOperation update = new TableUpdateOperation ()
171
- .set ("rating" , 4.5 )
172
- .set ("genres" , Arrays .asList ("Fiction" , "Drama" ))
173
- .unset ("borrower" );
174
- table .updateOne (filter , update );
175
- }
176
73
74
+ // Next Seach Table
177
75
178
- private static Date parseDate (String date ) {
179
- if (date == null ) return null ;
180
- try {
181
- return new SimpleDateFormat ("yyyy-MM-dd" ).parse (date );
182
- } catch (ParseException e ) {
183
- throw new RuntimeException (e );
184
- }
185
76
}
186
77
187
78
}
0 commit comments