From 6c9f549dcde57a5117c1d774b2809cdf8e765229 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Mon, 18 Sep 2017 15:59:51 +0300 Subject: [PATCH 1/3] Updated client library docs --- docs/client-libraries.md | 320 +++++++++++++++++++++------------------ 1 file changed, 176 insertions(+), 144 deletions(-) diff --git a/docs/client-libraries.md b/docs/client-libraries.md index e896fbab4..e6ed39fcc 100644 --- a/docs/client-libraries.md +++ b/docs/client-libraries.md @@ -13,220 +13,252 @@ nav = [ ### Go -You can find the Go client library for Pilosa at our [Go Pilosa Repository](https://github.com/pilosa/go-client-pilosa). Check out its [README](https://github.com/pilosa/go-client-pilosa/blob/master/README.md) for more information and installation instructions. +You can find the Go client library for Pilosa at our [Go Pilosa Repository](https://github.com/pilosa/go-pilosa). Check out its [README](https://github.com/pilosa/go-pilosa/blob/master/README.md) for more information and installation instructions. -We are going to use the index you have created in the [Getting Started](../getting-started) section. Before carrying on, make sure that example index is created and Pilosa server is running on the default address: `http://localhost:10101`. +We are going to use the index you have created in the [Getting Started](../getting-started) section. Before carrying on, make sure that example index is created, sample stargazer data is imported and Pilosa server is running on the default address: `http://localhost:10101`. Error handling has been omitted in the example below for brevity. ```go -package startrace +package main import ( - "fmt" + "fmt" - pilosa "github.com/pilosa/go-client-pilosa" + "github.com/pilosa/go-pilosa" ) func main() { - // Let's create Index and Frame objects, which will contain the settings - // for the corresponding indexes and frames. - repositoryOptions, := &pilosa.ColumnOptions{ColumnLabel: "repo_id"} - repository, _ := pilosa.NewIndex("repository", repositoryOptions) + // We will just use the default client which assumes the server is at http://localhost:10101 + client := pilosa.DefaultClient() - stargazerOptions := &pilosa.RowOptions{RowLabel: "stargazer_id"} - stargazer, _ := repository.Frame("stargazer", stargazerOptions) + // Let's load the schema from the server. + // Note that, for this example the schema should be created beforehand + // and the stargazer data should be imported. + // See the Getting Started repository: https://github.com/pilosa/getting-started/ + schema, err := client.Schema() + if err != nil { + // Most calls will return an error value. + // You should handle them appropriately. + // We will just terminate the program in this case. + // Error handling was left out for brevity in the rest of the code. + panic(err) + } - languageOptions := &pilosa.RowOptions{RowLabel: "language_id"} - language, _ := repository.Frame("language", languageOptions) + // We need to refer to indexes and frames before we can use them in a query. + repository, _ := schema.Index("repository", nil) + stargazer, _ := repository.Frame("stargazer", nil) + language, _ := repository.Frame("language", nil) - // We will just use the default client which assumes the server is at http://localhost:10101 - client := pilosa.DefaultClient() + var response *pilosa.QueryResponse - var response *pilosa.QueryResponse - var result *pilosa.QueryResult + // Which repositories did user 14 star: + response, _ = client.Query(stargazer.Bitmap(14), nil) + fmt.Println("User 14 starred: ", response.Result().Bitmap.Bits) - // Which repositories did user 8 star: - response, _ = client.Query(stargazer.Bitmap(8), nil) - result = response.Result() - if result != nil { - fmt.Println("User 8 starred: ", result.Bitmap.Bits) - } + // What are the top 5 languages in the sample data? + response, err = client.Query(language.TopN(5), nil) + languageIDs := []uint64{} + for _, item := range response.Result().CountItems { + languageIDs = append(languageIDs, item.ID) + } + fmt.Println("Top 5 languages: ", languageIDs) - // What are the top 5 languages in the sample data: - response, _ = client.Query(language.TopN(5), nil) - if result != nil { - fmt.Println("Top 5 languages: ", result.Bitmap.Bits) - } + // Which repositories were starred by both user 14 and 19: + response, _ = client.Query( + repository.Intersect( + stargazer.Bitmap(14), + stargazer.Bitmap(19)), + nil) + fmt.Println("Both user 14 and 19 starred:", response.Result().Bitmap.Bits) - // Which repositories were starred by user 8 and 18: - response, _ = client.Query( - repository.Intersect( - stargazer.Bitmap(8), - stargazer.Bitmap(18)), - nil) - result = response.Result() - if result != nil { - fmt.Println("Repositories starred by both user 8 and 18: ", result.Bitmap.Bits) - } + // Which repositories were starred by user 14 or 19: + response, _ = client.Query( + repository.Union( + stargazer.Bitmap(14), + stargazer.Bitmap(19)), + nil) + fmt.Println("User 14 or 19 starred:", response.Result().Bitmap.Bits) - // Which repositories were starred by user 8 and 18 and also were written in language 1 - response, _ = client.Query( - repository.Intersect( - stargazer.Bitmap(8), - stargazer.Bitmap(18), - language.Bitmap(1)), - nil) - result = response.Result() - if result != nil { - fmt.Println("Repositories starred by both user 8 and 18 and are in language 1: ", result.Bitmap.Bits) - } + // Which repositories were starred by user 14 or 19 and were written in language 1: + response, _ = client.Query( + repository.Intersect( + repository.Union( + stargazer.Bitmap(14), + stargazer.Bitmap(19), + ), + language.Bitmap(1), + ), nil) + fmt.Println("User 14 or 19 starred, written in language 1:", response.Result().Bitmap.Bits) - // Set user 99999 as a stargazer for repository 77777: - _, err = client.Query(stargazer.SetBit(99999, 77777), nil) - if err != nil { - fmt.Println("Error setting bit: ", err) - } + // Set user 99999 as a stargazer for repository 77777? + client.Query(stargazer.SetBit(99999, 77777), nil) } ``` ### Python -You can find the Python client library for Pilosa at our [Python Pilosa Repository](https://github.com/pilosa/python-pilosa). Check out its [README](https://github.com/pilosa/python-pilosa/blob/master/README.rst) for more information and installation instructions. +You can find the Python client library for Pilosa at our [Python Pilosa Repository](https://github.com/pilosa/python-pilosa). Check out its [README](https://github.com/pilosa/python-pilosa/blob/master/README.md) for more information and installation instructions. -We are going to use the index you have created in the [Getting Started](../getting-started) section. Before carrying on, make sure that example index is created and Pilosa server is running on the default address: `http://localhost:10101`. +We are going to use the index you have created in the [Getting Started](../getting-started) section. Before carrying on, make sure that example index is created, sample stargazer data is imported and Pilosa server is running on the default address: `http://localhost:10101`. Error handling has been omitted in the example below for brevity. ```python from pilosa import Index, Client, PilosaError -# Let's create Index and Frame objects, which will contain the settings -# for the corresponding indexes and frames. -repository = Index("repository", column_label="repo_id") -stargazer = repository.frame("stargazer", row_label="stargazer_id") -language = repository.frame("language", row_label="language_id") - # We will just use the default client which assumes the server is at http://localhost:10101 client = Client() +# Let's load the schema from the server. +# Note that, for this example the schema should be created beforehand +# and the stargazer data should be imported. +# See the Getting Started repository: https://github.com/pilosa/getting-started/ + +# Let's create Index and Frame objects, which will contain the settings +# for the corresponding indexes and frames. +try: + schema = client.schema() +except PilosaError as e: + # Most calls will raise an exception on errors. + # You should handle them appropriately. + # We will just terminate the program in this case. + raise SystemExit(e) + +# We need to refer to indexes and frames before we can use them in a query. +repository = schema.index("repository") +stargazer = repository.frame("stargazer") +language = repository.frame("language") + # Which repositories did user 8 star: -response = client.query(stargazer.bitmap(8)) -if response.result: - print("User 8 starred: ", result.bitmap.bits) +repository_ids = client.query(stargazer.bitmap(14)).result.bitmap.bits +print("User 8 starred: ", repository_ids) # What are the top 5 languages in the sample data: -response = client.query(language.topn(5)) -if response.result: - print("Top 5 languages: ", result.bitmap.bits) +top_languages = client.query(language.topn(5)).result.count_items +print("Top 5 languages: ", [item.id for item in top_languages]) -# Which repositories were starred by user 8 and 18: -response = client.query( - repository.intersect( - stargazer.bitmap(8), - stargazer.bitmap(18))) -if response.result: - print("Repositories starred by both user 8 and 18: ", result.bitmap.bits) +# Which repositories were starred by both user 14 and 19: +query = repository.intersect( + stargazer.bitmap(14), + stargazer.bitmap(19) +) +mutually_starred = client.query(query).result.bitmap.bits +print("Both user 14 and 19 starred:", mutually_starred) -# Which repositories were starred by user 8 and 18 and also were written in language 1 -response = client.query( - repository.intersect( - stargazer.bitmap(8), - stargazer.bitmap(18), - language.bitmap(1))) -if response.result: - print("Repositories starred by both user 8 and 18 and are in language 1: ", result.bitmap.bits) +# Which repositories were starred by user 14 or 19: +query = repository.union( + stargazer.bitmap(14), + stargazer.bitmap(19) +) +either_starred = client.query(query).result.bitmap.bits +print("User 14 or 19 starred:", either_starred) + +# Which repositories were starred by user 14 or 19 and were written in language 1: +query = repository.intersect( + repository.union( + stargazer.bitmap(14), + stargazer.bitmap(19) + ), + language.bitmap(1) +) +mutually_starred = client.query(query).result.bitmap.bits +print("User 14 or 19 starred, written in language 1:", mutually_starred) # Set user 99999 as a stargazer for repository 77777 -try: - client.query(stargazer.setbit(99999, 77777)) -except PilosaError as ex: - print("Error setting bit: ", ex) - -``` +client.query(stargazer.setbit(99999, 77777))` ### Java You can find the Java client library for Pilosa at our [Java Pilosa Repository](https://github.com/pilosa/java-pilosa). Check out its [README](https://github.com/pilosa/java-pilosa/blob/master/README.md) for more information and installation instructions. -We are going to use the index you have created in the [Getting Started](../getting-started) section. Before carrying on, make sure that example index is created and Pilosa server is running on the default address: `http://localhost:10101`. +We are going to use the index you have created in the [Getting Started](../getting-started) section. Before carrying on, make sure that example index is created, sample stargazer data is imported and Pilosa server is running on the default address: `http://localhost:10101`. Error handling has been omitted in the example below for brevity. ```java import com.pilosa.client.*; import com.pilosa.client.orm.*; +import com.pilosa.client.exceptions.PilosaException; + +import java.util.ArrayList; +import java.util.List; public class StarTrace { public static void main(String[] args) { - // Let's create Index and Frame objects, which will contain the settings - // for the corresponding indexes and frames. - IndexOptions repositoryOptions = IndexOptions.builder() - .setColumnLabel("repo_id") - .build(); - Index repository = Index.withName("repository", repositoryOptions); - - FrameOptions stargazerOptions = FrameOptions.builder() - .setRowLabel("stargazer_id") - .build(); - Frame stargazer = repository.frame("stargazer", stargazerOptions); - - FrameOptions languageOptions = FrameOptions.builder() - .setRowLabel("language_id") - .build(); - Frame language = repository.frame("language", languageOptions); - // We will just use the default client which assumes the server is at http://localhost:10101 PilosaClient client = PilosaClient.defaultClient(); + // Let's load the schema from the server. + Schema schema; + try { + schema = client.readSchema(); + } + catch (PilosaException ex) { + // Most calls will return an error value. + // You should handle them appropriately. + // We will just terminate the program in this case. + throw new RuntimeException(ex); + } + + // We need to refer to indexes and frames before we can use them in a query. + Index repository = schema.index("repository"); + Frame stargazer = repository.frame("stargazer"); + Frame language = repository.frame("language"); + QueryResponse response; QueryResult result; + PqlQuery query; + List repositoryIDs; - // Which repositories did user 8 star: - response = client.query(stargazer.bitmap(8)); - result = response.getResult(); - if (result != null) { - System.out.println("User 8 starred: " + result.getBitmap().getBits()); - } + // Which repositories did user 14 star: + response = client.query(stargazer.bitmap(14)); + repositoryIDs = response.getResult().getBitmap().getBits(); + System.out.println("User 14 starred: " + repositoryIDs); // What are the top 5 languages in the sample data: response = client.query(language.topN(5)); - result = response.getResult(); - if (result != null) { - System.out.println("Top 5 languages: " + result.getBitmap().getBits()); + List top_languages = response.getResult().getCountItems(); + List languageIDs = new ArrayList<>(); + for (CountResultItem item : top_languages) { + languageIDs.add(item.getID()); } - // Which repositories were starred by user 8 and 18: - response = client.query( - repository.intersect( - stargazer.bitmap(8), - stargazer.bitmap(18))); - result = response.getResult(); - if (result != null) { - System.out.println("Repositories starred by both user 8 and 18: " - + result.getBitmap().getBits()); - } + System.out.println("Top Languages: " +languageIDs); - // Which repositories were starred by user 8 and 18 and also were written in language 1 - response = client.query( - repository.intersect( - stargazer.bitmap(8), - stargazer.bitmap(18), - language.bitmap(1))); - result = response.getResult(); - if (result != null) { - System.out.println("Repositories starred by both user 8 and 18 and are in language 1: " - + result.getBitmap().getBits()); - } + // Which repositories were starred by both user 14 and 19: + query = repository.intersect( + stargazer.bitmap(14), + stargazer.bitmap(19) + ); + response = client.query(query); + repositoryIDs = response.getResult().getBitmap().getBits(); + System.out.println("Both user 14 and 19 starred: " + repositoryIDs); + + // Which repositories were starred by user 14 or 19: + query = repository.union( + stargazer.bitmap(14), + stargazer.bitmap(19) + ); + response = client.query(query); + repositoryIDs = response.getResult().getBitmap().getBits(); + System.out.println("User 14 or 19 starred: " + repositoryIDs); + + // Which repositories were starred by user 14 or 19 and were written in language 1: + query = repository.intersect( + repository.union( + stargazer.bitmap(14), + stargazer.bitmap(19) + ), + language.bitmap(1) + ); + response = client.query(query); + repositoryIDs = response.getResult().getBitmap().getBits(); + System.out.println("User 14 or 19 starred, written in language 1: " + repositoryIDs); // Set user 99999 as a stargazer for repository 77777: - try { - client.query(stargazer.setBit(99999, 77777)) - } - catch (PilosaException ex) { - System.out.println("Error setting bit: " + ex) - } - + client.query(stargazer.setBit(99999, 77777)); + } } ``` From e1d2bd10872340d1b66c5ecce3eb08314778af18 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Mon, 25 Sep 2017 17:23:11 +0300 Subject: [PATCH 2/3] fix formatting --- docs/client-libraries.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/client-libraries.md b/docs/client-libraries.md index e6ed39fcc..760d2e8ca 100644 --- a/docs/client-libraries.md +++ b/docs/client-libraries.md @@ -167,6 +167,7 @@ print("User 14 or 19 starred, written in language 1:", mutually_starred) # Set user 99999 as a stargazer for repository 77777 client.query(stargazer.setbit(99999, 77777))` +``` ### Java From 5cc05cb0003d2c49959027f6b197d9b542dcc02d Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Mon, 25 Sep 2017 17:32:38 +0300 Subject: [PATCH 3/3] yet another formatting fix --- docs/client-libraries.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/client-libraries.md b/docs/client-libraries.md index 760d2e8ca..70dc73fe5 100644 --- a/docs/client-libraries.md +++ b/docs/client-libraries.md @@ -166,7 +166,7 @@ mutually_starred = client.query(query).result.bitmap.bits print("User 14 or 19 starred, written in language 1:", mutually_starred) # Set user 99999 as a stargazer for repository 77777 -client.query(stargazer.setbit(99999, 77777))` +client.query(stargazer.setbit(99999, 77777)) ``` ### Java @@ -259,7 +259,6 @@ public class StarTrace { // Set user 99999 as a stargazer for repository 77777: client.query(stargazer.setBit(99999, 77777)); - } } ```