Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #47 from wso2-ballerina/df_endpoint_change
Browse files Browse the repository at this point in the history
Merge df_endpoint_change to master
  • Loading branch information
Manuri authored Dec 10, 2018
2 parents d9978e4 + b2118ba commit 5939e89
Show file tree
Hide file tree
Showing 18 changed files with 441 additions and 487 deletions.
63 changes: 31 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[![Build Status](https://travis-ci.org/wso2-ballerina/module-cassandra.svg?branch=master)](https://travis-ci.org/wso2-ballerina/module-cassandra)

# Ballerina Cassandra Client Endpoint
# Ballerina Cassandra Client

Ballerina Cassandra Client Endpoint is used to connect Ballerina with Cassandra data source. With the Ballerina Cassandra client endpoint following actions are supported.
Ballerina Cassandra Client is used to connect Ballerina with Cassandra data source. With the Ballerina Cassandra client following operations are supported.

1. update - To execute a data or schema update query
2. select - To select data from the datasource
Expand Down Expand Up @@ -38,7 +38,7 @@ type Person record {
};
public function main() {
endpoint c:Client conn {
c:Client conn = new({
host: "localhost",
port: 9042,
username: "cassandra",
Expand All @@ -48,7 +48,7 @@ public function main() {
protocolOptionsConfig: { sslEnabled: false },
socketOptionsConfig: { connectTimeoutMillis: 500, readTimeoutMillis: 1000 },
poolingOptionsConfig: { maxConnectionsPerHostLocal: 5, newConnectionThresholdLocal: 10 } }
};
});
var returned = conn->update("CREATE KEYSPACE testballerina WITH replication = {'class':'SimpleStrategy',
'replication_factor' : 3}");
Expand All @@ -67,45 +67,43 @@ public function main() {
pID, pName, pSalary, pIncome, pMarried);
handleUpdate(returned, "Insert One Row to Table person");
table<Person> dt;
var selectRet = conn->select("select id, name, salary from testballerina.person where id = ?", Person, pID);
match selectRet {
table tableReturned => dt = tableReturned;
error e => io:println("Select data from person table failed: " + e.message);
}
foreach row in dt {
io:println("Person:" + row.id + "|" + row.name + "|" + row.salary);
if (selectRet is table<Person>) {
foreach var row in selectRet {
io:println("Person:" + row.id + "|" + row.name + "|" + row.salary);
}
} else {
io:println("Select data from person table failed: " + <string>selectRet.detail().message);
}
selectRet = conn->select("select id, name, salary from testballerina.person where id = ? and name = ?
ALLOW FILTERING", Person, pID, pName);
match selectRet {
table tableReturned => dt = tableReturned;
error e => io:println("Select data from person table failed: " + e.message);
}
var jsonRet = <json>dt;
match jsonRet {
json j => {
if (selectRet is table<record {}>) {
var jsonRet = json.convert(selectRet);
if (jsonRet is json) {
io:print("JSON: ");
io:println(io:sprintf("%s", j));
io:println(io:sprintf("%s", jsonRet));
} else {
io:println("Error in table to json conversion");
}
error e => io:println("Error in table to json conversion");
} else {
io:println("Select data from person table failed: " + <string>selectRet.detail().message);
}
selectRet = conn->select("select id, name, salary from testballerina.person where salary = ? ALLOW FILTERING",
Person, pSalary);
match selectRet {
table tableReturned => dt = tableReturned;
error e => io:println("Select data from person table failed: " + e.message);
}
var xmlRet = <xml>dt;
match xmlRet {
xml x => {
if (selectRet is table<record {}>) {
var xmlRet = xml.convert(selectRet);
if (xmlRet is xml) {
io:print("XML: ");
io:println(io:sprintf("%s", x));
io:println(io:sprintf("%s", xmlRet));
} else {
io:println("Error in table to xml conversion");
}
error e => io:println("Error in table to xml conversion");
} else {
io:println("Select data from person table failed: " + <string>selectRet.detail().message);
}
returned = conn->update("DROP KEYSPACE testballerina");
Expand All @@ -116,9 +114,10 @@ public function main() {
}
function handleUpdate(()|error returned, string message) {
match returned {
() => io:println(message + " success ");
error e => io:println(message + " failed: " + e.message);
if (returned is ()) {
io:println(message + " success ");
} else {
io:println(message + " failed: " + <string>returned.detail().message);
}
}
```
20 changes: 0 additions & 20 deletions component/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@
<groupId>org.ballerinalang</groupId>
<artifactId>ballerina-log-api</artifactId>
</dependency>
<dependency>
<groupId>org.ballerinax.docker</groupId>
<artifactId>docker-extension</artifactId>
</dependency>
<dependency>
<groupId>org.ballerinax.kubernetes</groupId>
<artifactId>kubernetes-extension</artifactId>
</dependency>
<dependency>
<groupId>org.ballerinalang</groupId>
<artifactId>ballerina-builtin</artifactId>
Expand All @@ -96,18 +88,6 @@
<type>zip</type>
<classifier>ballerina-binary-repo</classifier>
</dependency>
<dependency>
<groupId>org.ballerinax.docker</groupId>
<artifactId>docker-extension</artifactId>
<type>zip</type>
<classifier>ballerina-binary-repo</classifier>
</dependency>
<dependency>
<groupId>org.ballerinax.kubernetes</groupId>
<artifactId>kubernetes-extension</artifactId>
<type>zip</type>
<classifier>ballerina-binary-repo</classifier>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
Expand Down
30 changes: 16 additions & 14 deletions component/src/main/ballerina/cassandra/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

This module provides the functionality required to access and manipulate data stored in an Cassandra datasource.

### Endpoint
### Client

To access a Cassandra datasource, you must first create an `endpoint`, which is a virtual representation of the physical endpoint of the Cassandra database that you are trying to connect to. Create an endpoint of the cassandra client type (i.e., `cassandra:Client`) and provide the necessary connection parameters. This will create a pool of connections to the given Cassandra database. A sample for creating an endpoint with a Cassandra client can be found below.
To access a Cassandra datasource, you must first create a `client` object. Create a client object of the cassandra client type (i.e., `cassandra:Client`) and provide the necessary connection parameters. This will create a pool of connections to the given Cassandra database. A sample for creating a client with a Cassandra client can be found below.

### Database operations

Once the endpoint is created, database operations can be executed through that endpoint. This module provides support for updating data/schema and select data.
Once the client is created, database operations can be executed through that client. This module provides support for updating data/schema and select data.

## Samples

### Creating an endpoint
### Creating a Client
```ballerina
endpoint c:Client conn {
cassandra:Client conn = new({
host: "localhost",
port: 9042,
username: "cassandra",
Expand All @@ -24,31 +24,33 @@ endpoint c:Client conn {
protocolOptionsConfig: { sslEnabled: false },
socketOptionsConfig: { connectTimeoutMillis: 500, readTimeoutMillis: 1000 },
poolingOptionsConfig: { maxConnectionsPerHostLocal: 5, newConnectionThresholdLocal: 10 } }
};
});
```
For the full list of available configuration options refer the API docs of the endpoint.
For the full list of available configuration options refer the API docs of the client.

### Update data

```ballerina
var returned = conn->update("CREATE TABLE testballerina.person(id int PRIMARY KEY,name text,salary float,income double,
married boolean)");
match returned {
() => io:println("Table creation success ");
error e => io:println("Table creation failed: " + e.message);
if (returned is ()) {
io:println("Table creation success ");
} else {
io:println("Table creation failed: " + returned.reason());
}
```

### Select data

```ballerina
table dt;
var selectRet = conn->select("select id, name, salary from testballerina.person where salary = ? ALLOW FILTERING",
Person, pSalary);
match selectRet {
table tableReturned => dt = tableReturned;
error e => io:println("Select data from person table failed: " + e.message);
if (selectRet is table) {
table dt = selectRet;
// Processing logic
} else {
io:println("Select data from person table failed: " + selectRet.reason());
}
```
72 changes: 0 additions & 72 deletions component/src/main/ballerina/cassandra/actions.bal

This file was deleted.

Loading

0 comments on commit 5939e89

Please sign in to comment.