[wso2con eu 2017] introduction to ballerina
TRANSCRIPT
Senior Director - Platform Architecture, WSO2
Introduction to Ballerina
Afkham Azeez
http://ballerinalang.org
• An event-driven, parallel programming language for networked applications
• Textual and graphical syntaxes with sequence diagram metaphor
• Strongly and statically typed with powerful type system• Designed for network with JSON, XML, SQL, MIME and
HTTP, WebSocket, MQTT, etc.• Modular and designed for modern development practices• Microservices, serverless, and container friendly
Ballerina
Make it Easier to Consume and Produce Networked Services and
Applications.
Ballerina has been influenced by Java, Go, C, Node, Javascript, Maven, Tomcat, and a variety of other awesome tools.
Yet Another Programming Language?
• Type system mismatches – no understanding of JSON, XML, SQL
• Generally poor at handling asynchronous programming• Writing network resilient programs is hard/error-prone• DSLs for integration (Camel/ESBs) are not workable at
scale• Heavy – unsuitable for microservices, serverless,
containerized world
Motivation for Ballerina
function main (string[] args) { println("Hello, World!");}
Hello World
$ ballerina run hello-world.bal Hello, World!
Running
Ballerina Knows JSON, XML, and data
• All a part of Ballerina type system• Deeply unified and integrated, both syntactically and
semantically• Data transformation across all of these, graphically and
textually• Transactional data management
• JSON is a union type– Can be an object, an array, or a simple
value (string, number, boolean)
• Literal value syntax same as other record types
JSON
json j = {fname:"John", lname:"Stallone", "age":age};
• New tree-less data model• Deeply merged to language
– Embedded literal syntax– XML qualified names and Ballerina qualified
names unified
XML
function main (string[] args) { xml person1 = xml `<person><fname>John</fname><lname>Doe</lname></person>`; xml person2 = xml `<person><fname>Jane</fname><lname>Doe</lname></person>`;
xml persons = xml `<persons/>`;
persons.setChildren(persons, person1 + person2); println(persons);}
Example
• Represents tabular data– SQL– Cassandra– MongoDB– etc
• SQL connector is transaction-aware and produces streaming datatables that can efficiently move data from database to network
Datatable
endpoint<sql:ClientConnector> empDB { create sql:ClientConnector(sql:MYSQL, "localhost", 3306, "empdb", "root", "root", {maximumPoolSize:5});}
datatable dt = empDB.select("SELECT id,name from employees", params);
var j, _ = <json>dt;println(j);
Example
Ballerina Knows Network Protocols
• HTTP, WebSocket, JMS, FTP, Files, etc. • Popular APIs: Facebook, Twitter, Gmail,
LinkedIn, etc.• Extensible authentication/policy support
import ballerina.net.http;
service<http> helloWorld { resource sayHello (http:request req, http:response resp) { resp.setStringPayload ("Hello, World!"); resp.sendResponse (200, "OK"); }}
Ballerina Services
$ curl http://localhost:8080/helloWorld
Hello, World!
Ballerina Knows Swagger
• Ballerina services’ interface is Swagger and Ballerina syntax, Swagger YAML syntax and graphical syntaxes are interchangeable
• Reduces pains of interface-first design
ballerina swagger connector http://petstore.swagger.io/v2/swagger.json -d swagger-example/petstore/ -p org.example
Ballerina is Naturally Parallel
• Sequence diagram approach makes parallelism innate
• Think parallel first, not sequential first
function main (string[] args) { worker w1 { println ("Hello, World from w1!"); } worker w2 { println ("Hello, World from w2!"); } worker w3 { println ("Hello, World from w3!"); }}
Parallel Hello World
$ ballerina run hello-world-parallel.bal Hello, World from w1!Hello, World from w2!Hello, World from w3!
Running
Ballerina Supports Safety & Resiliency
• Highly structured error and exception handling• Taint checking of network delivered data• Built-in retrying, failover, load balancing, and
more, to make programs more resilient to network failures
transaction [with acid, retry(expression)] { statement;*} [failed { statement;*}] [aborted { statement;*}] [committed { statement;*}]
Ballerina Transactions
• A common requirement in integration• Different systems have different data formats
Data Transformation
Example
transformer <Person p, User u> { u.username = p.first_name; u.category, u.ageCode = getCategory(p.age); u.geoCode = getGeoCode(p.state, p.city); u.name = p.first_name;}
Person p = {first_name: "John", last_name: "Doe", age: 30, city: "London"};User u = <User> p;
Example
IO API
• Endpoints are logical entities that represent remote services
• Connectors are like the type (class): for example an HTTPClient connector
• Connections are instances– e.g. the connector configured with a particular URL
and credentials• Actions are invoked on connections
Endpoints, Connectors, Connections & Actions
Ballerina Knows Docker
• Built in ability to create Docker image with executable Ballerina program package
• Run on any container management platform
ballerina docker hello-world.balx
Ballerina is Open Source
• Patent pending technology• Implementation released under Apache License v2.0
– Fork me on GitHub: https://github.com/ballerinalang/• Community
– Slack: #ballerinalang– Twitter: @ballerinalang– StackOverflow: #ballerinalang– Developers: [email protected]
• Graphical composer in browser/as IDE• IDE plugins - Intellij IDEA, Visual Code, Atom, Eclipse,
etc.• Testerina - testing and mocking tool• Docerina - API doc generation tool• Packerina - library and package management tool
Ballerina is More Than a Language
wso2.com