[wso2con eu 2017] introduction to ballerina
TRANSCRIPT
![Page 1: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/1.jpg)
Senior Director - Platform Architecture, WSO2
Introduction to Ballerina
Afkham Azeez
![Page 3: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/3.jpg)
• 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
![Page 4: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/4.jpg)
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?
![Page 5: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/5.jpg)
• 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
![Page 6: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/6.jpg)
function main (string[] args) { println("Hello, World!");}
Hello World
![Page 7: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/7.jpg)
$ ballerina run hello-world.bal Hello, World!
Running
![Page 8: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/8.jpg)
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
![Page 9: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/9.jpg)
• 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};
![Page 10: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/10.jpg)
• New tree-less data model• Deeply merged to language
– Embedded literal syntax– XML qualified names and Ballerina qualified
names unified
XML
![Page 11: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/11.jpg)
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
![Page 12: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/12.jpg)
• 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
![Page 13: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/13.jpg)
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
![Page 14: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/14.jpg)
Ballerina Knows Network Protocols
• HTTP, WebSocket, JMS, FTP, Files, etc. • Popular APIs: Facebook, Twitter, Gmail,
LinkedIn, etc.• Extensible authentication/policy support
![Page 15: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/15.jpg)
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!
![Page 16: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/16.jpg)
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
![Page 17: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/17.jpg)
Ballerina is Naturally Parallel
• Sequence diagram approach makes parallelism innate
• Think parallel first, not sequential first
![Page 18: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/18.jpg)
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
![Page 19: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/19.jpg)
![Page 20: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/20.jpg)
$ ballerina run hello-world-parallel.bal Hello, World from w1!Hello, World from w2!Hello, World from w3!
Running
![Page 21: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/21.jpg)
![Page 22: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/22.jpg)
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
![Page 23: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/23.jpg)
transaction [with acid, retry(expression)] { statement;*} [failed { statement;*}] [aborted { statement;*}] [committed { statement;*}]
Ballerina Transactions
![Page 24: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/24.jpg)
• A common requirement in integration• Different systems have different data formats
Data Transformation
![Page 25: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/25.jpg)
Example
![Page 26: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/26.jpg)
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
![Page 27: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/27.jpg)
IO API
![Page 28: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/28.jpg)
• 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
![Page 29: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/29.jpg)
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
![Page 30: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/30.jpg)
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]
![Page 31: [WSO2Con EU 2017] Introduction to Ballerina](https://reader036.vdocuments.site/reader036/viewer/2022082218/5a648ff87f8b9a31568b5409/html5/thumbnails/31.jpg)
• 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