[wso2con eu 2017] introduction to ballerina

32
Senior Director - Platform Architecture, WSO2 Introduction to Ballerina Afkham Azeez

Upload: wso2-inc

Post on 21-Jan-2018

242 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: [WSO2Con EU 2017] Introduction to Ballerina

Senior Director - Platform Architecture, WSO2

Introduction to Ballerina

Afkham Azeez

Page 2: [WSO2Con EU 2017] Introduction to Ballerina

http://ballerinalang.org

Page 3: [WSO2Con EU 2017] Introduction to Ballerina

• 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

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

• 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

function main (string[] args) { println("Hello, World!");}

Hello World

Page 7: [WSO2Con EU 2017] Introduction to Ballerina

$ ballerina run hello-world.bal Hello, World!

Running

Page 8: [WSO2Con EU 2017] Introduction to Ballerina

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

• 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

• 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

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

• 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

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

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

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

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

Ballerina is Naturally Parallel

• Sequence diagram approach makes parallelism innate

• Think parallel first, not sequential first

Page 18: [WSO2Con EU 2017] Introduction to Ballerina

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
Page 20: [WSO2Con EU 2017] Introduction to Ballerina

$ 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
Page 22: [WSO2Con EU 2017] Introduction to Ballerina

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

transaction [with acid, retry(expression)] { statement;*} [failed { statement;*}] [aborted { statement;*}] [committed { statement;*}]

Ballerina Transactions

Page 24: [WSO2Con EU 2017] Introduction to Ballerina

• A common requirement in integration• Different systems have different data formats

Data Transformation

Page 25: [WSO2Con EU 2017] Introduction to Ballerina

Example

Page 26: [WSO2Con EU 2017] Introduction to Ballerina

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

IO API

Page 28: [WSO2Con EU 2017] Introduction to Ballerina

• 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

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

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

• 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

Page 32: [WSO2Con EU 2017] Introduction to Ballerina

wso2.com