|
| 1 | +# Recombee API Client |
| 2 | + |
| 3 | +A Java client for easy use of the [Recombee](https://www.recombee.com/) recommendation API. |
| 4 | + |
| 5 | +If you don't have an account at Recombee yet, you can create a free account [here](https://www.recombee.com/). |
| 6 | + |
| 7 | +Documentation of the API can be found at [docs.recombee.com](https://docs.recombee.com/). |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +The client shall be available in the Maven Central Repository within days. |
| 12 | +Until then, you can use [jitpack](https://jitpack.io/#Recombee/java-api-client) for an easy install. |
| 13 | + |
| 14 | +## Examples |
| 15 | + |
| 16 | +### Basic example |
| 17 | + |
| 18 | +Examples are located in [src/examples](https://github.com/Recombee/java-api-client/tree/master/src/examples/java/com/recombee/api_client/examples/). |
| 19 | + |
| 20 | +```java |
| 21 | +package com.recombee.api_client.examples; |
| 22 | + |
| 23 | +import com.recombee.api_client.RecombeeClient; |
| 24 | +import com.recombee.api_client.api_requests.*; |
| 25 | +import com.recombee.api_client.bindings.Recommendation; |
| 26 | +import com.recombee.api_client.exceptions.ApiException; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Random; |
| 30 | + |
| 31 | +public class BasicExample { |
| 32 | + public static void main(String[] args) { |
| 33 | + |
| 34 | + RecombeeClient client = new RecombeeClient("client-test", "jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L"); |
| 35 | + try { |
| 36 | + final int NUM = 100; |
| 37 | + //Create some users and send them to Recombee, use Batch for faster processing |
| 38 | + ArrayList<Request> addUserRequests = new ArrayList<Request>(); |
| 39 | + for (int i = 0; i < NUM; i++) addUserRequests.add(new AddUser(String.format("user-%s", i))); |
| 40 | + |
| 41 | + System.out.println("Send users"); |
| 42 | + client.send(new Batch(addUserRequests)); |
| 43 | + |
| 44 | + //Now create some items |
| 45 | + ArrayList<Request> addItemRequests = new ArrayList<Request>(); |
| 46 | + for (int i = 0; i < NUM; i++) addItemRequests.add(new AddItem(String.format("item-%s", i))); |
| 47 | + |
| 48 | + System.out.println("Send items"); |
| 49 | + client.send(new Batch(addItemRequests)); |
| 50 | + |
| 51 | + // Generate some random purchases of items by users |
| 52 | + final double PROBABILITY_PURCHASED = 0.01; |
| 53 | + Random r = new Random(); |
| 54 | + ArrayList<Request> addPurchaseRequests = new ArrayList<Request>(); |
| 55 | + for (int i = 0; i < NUM; i++) |
| 56 | + for (int j = 0; j < NUM; j++) |
| 57 | + if (r.nextDouble() < PROBABILITY_PURCHASED) |
| 58 | + addPurchaseRequests.add(new AddPurchase(String.format("user-%s", i),String.format("item-%s", j))); |
| 59 | + |
| 60 | + System.out.println("Send purchases"); |
| 61 | + client.send(new Batch(addPurchaseRequests)); |
| 62 | + |
| 63 | + // Get 5 recommendations for user 'user-25' |
| 64 | + Recommendation[] recommended = client.send(new UserBasedRecommendation("user-25", 5)); |
| 65 | + System.out.println("Recommended items:"); |
| 66 | + for(Recommendation rec: recommended) System.out.println(rec.getId()); |
| 67 | + |
| 68 | + } catch (ApiException e) { |
| 69 | + e.printStackTrace(); |
| 70 | + //use fallback |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +``` |
| 76 | + |
| 77 | +### Using property values |
| 78 | +```java |
| 79 | +package com.recombee.api_client.examples; |
| 80 | + |
| 81 | +import com.recombee.api_client.RecombeeClient; |
| 82 | +import com.recombee.api_client.api_requests.*; |
| 83 | +import com.recombee.api_client.bindings.Recommendation; |
| 84 | +import com.recombee.api_client.exceptions.ApiException; |
| 85 | + |
| 86 | +import java.util.ArrayList; |
| 87 | +import java.util.HashMap; |
| 88 | +import java.util.Random; |
| 89 | + |
| 90 | +public class ItemPropertiesExample { |
| 91 | + public static void main(String[] args) { |
| 92 | + |
| 93 | + RecombeeClient client = new RecombeeClient("client-test", "jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L"); |
| 94 | + |
| 95 | + try { |
| 96 | + client.send(new ResetDatabase()); //Clear everything from the database |
| 97 | + |
| 98 | + /* |
| 99 | + We will use computers as items in this example |
| 100 | + Computers have three properties |
| 101 | + - price (floating point number) |
| 102 | + - number of processor cores (integer number) |
| 103 | + - description (string) |
| 104 | + */ |
| 105 | + |
| 106 | + client.send(new AddItemProperty("price", "double")); |
| 107 | + client.send(new AddItemProperty("num-cores", "int")); |
| 108 | + client.send(new AddItemProperty("description", "string")); |
| 109 | + |
| 110 | + // Prepare requests for setting a catalog of computers |
| 111 | + final ArrayList<Request> requests = new ArrayList<Request>(); |
| 112 | + final int NUM = 100; |
| 113 | + final Random rand = new Random(); |
| 114 | + |
| 115 | + for(int i=0; i<NUM; i++) |
| 116 | + { |
| 117 | + final SetItemValues req = new SetItemValues( |
| 118 | + String.format("computer-%s",i), //itemId |
| 119 | + //values: |
| 120 | + new HashMap<String, Object>() {{ |
| 121 | + put("price", 600.0 + 400*rand.nextDouble()); |
| 122 | + put("num-cores", 1 + rand.nextInt(7)); |
| 123 | + put("description", "Great computer"); |
| 124 | + put("!cascadeCreate", true); // Use !cascadeCreate for creating item |
| 125 | + // with given itemId, if it doesn't exist |
| 126 | + }} |
| 127 | + ); |
| 128 | + requests.add(req); |
| 129 | + } |
| 130 | + client.send(new Batch(requests)); // Send catalog to the recommender system |
| 131 | + |
| 132 | + // Generate some random purchases of items by users |
| 133 | + final double PROBABILITY_PURCHASED = 0.02; |
| 134 | + ArrayList<Request> addPurchaseRequests = new ArrayList<Request>(); |
| 135 | + for (int i = 0; i < NUM; i++) |
| 136 | + for (int j = 0; j < NUM; j++) |
| 137 | + if (rand.nextDouble() < PROBABILITY_PURCHASED) { |
| 138 | + AddPurchase req = new AddPurchase(String.format("user-%s", i),String.format("computer-%s", j)) |
| 139 | + .setCascadeCreate(true); //use cascadeCreate to create the users |
| 140 | + addPurchaseRequests.add(req); |
| 141 | + } |
| 142 | + client.send(new Batch(addPurchaseRequests)); // Send purchases to the recommender system |
| 143 | + |
| 144 | + |
| 145 | + // Get 5 recommendations for user-42, who is currently viewing computer-6 |
| 146 | + Recommendation[] recommended = client.send(new ItemBasedRecommendation("computer-6", 5) |
| 147 | + .setTargetUserId("user-42")); |
| 148 | + System.out.println("Recommended items:"); |
| 149 | + for(Recommendation rec: recommended) System.out.println(rec.getId()); |
| 150 | + |
| 151 | + |
| 152 | + // Get 5 recommendations for user-42, but recommend only computers that have at least 3 cores |
| 153 | + recommended = client.send(new ItemBasedRecommendation("computer-6", 5).setTargetUserId("user-42") |
| 154 | + .setFilter(" 'num-cores'>=3 ")); |
| 155 | + System.out.println("Recommended items with at least 3 processor cores:"); |
| 156 | + for(Recommendation rec: recommended) System.out.println(rec.getId()); |
| 157 | + |
| 158 | + // Get 5 recommendations for user-42, but recommend only items that are more expensive then currently viewed item (up-sell) |
| 159 | + recommended = client.send(new ItemBasedRecommendation("computer-6", 5).setTargetUserId("user-42") |
| 160 | + .setFilter(" 'price' > context_item[\"price\"] ")); |
| 161 | + |
| 162 | + System.out.println("Recommended up-sell items:"); |
| 163 | + for(Recommendation rec: recommended) System.out.println(rec.getId()); |
| 164 | + |
| 165 | + } catch (ApiException e) { |
| 166 | + e.printStackTrace(); |
| 167 | + //Use fallback |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +``` |
| 173 | + |
| 174 | +## Exception handling |
| 175 | + |
| 176 | +Various errors can occur while processing request, for example because of adding an already existing item or submitting interaction of nonexistent user without *setCascadeCreate(true)*. These errors lead to throwing the *ResponseException* by the *send* method of the client. Another reason for throwing an exception is a timeout. *ApiException* is the base class of both *ResponseException* and *ApiTimeoutException*. |
| 177 | + |
| 178 | +We are doing our best to provide the fastest and most reliable service, but production-level applications must implement a fallback solution since errors can always happen. The fallback might be, for example, showing the most popular items from the current category, or not displaying recommendations at all. |
0 commit comments