|
|
Line 1: |
Line 1: |
| [[Media:Exploration_of_the_Darts_dataset_using_statistics.pdf]] | | [[Media:Exploration_of_the_Darts_dataset_using_statistics.pdf]] |
− |
| |
− |
| |
− | <br />
| |
− | ==Big data introduction==
| |
− | '''What Is Big Data?''':
| |
− | Big Data is such a huge and complex data that cannot be stored or processed easily using traditional data processing techniques/tools.
| |
− |
| |
− |
| |
− |
| |
− | '''Unstructured data''' comes from information that is not organized or easily interpreted by traditional databases or data models, and typically, it’s text-heavy. Metadata, Twitter tweets, and other social media posts are good examples of unstructured data.
| |
− |
| |
− |
| |
− | '''Multi-structured data''' refers to a variety of data formats and types and can be derived from interactions between people and machines, such as web applications or social networks. A great example is web log data, which includes a combination of text and visual images along
| |
− |
| |
− |
| |
− | <br />
| |
− | ==Scaling a database==
| |
− | * '''Typical problems encountered when scaling a traditional database:'''
| |
− | :* More than 30,000 gigabytes of data are generated every second.
| |
− | ::* The data is diverse nowadays
| |
− | :* Traditional database systems, such as relational databases, have been pushed to the limit.
| |
− | :* A new breed of technologies has emerged under the term NoSQL.
| |
− | ::* These systems can scale to vastly larger sets of data.
| |
− |
| |
− |
| |
− | :* Applications using normalized database schema require the use of join's, which doesn't perform well under lots of data and/or nodes.
| |
− | :* Existing RDBMS clustering solutions require scale-up, which is limited & not really scalable when dealing with exponential growth.
| |
− | :* Machines have upper limits on capacity, & sharding the data & processing across machines is very complex & app-specific.
| |
− |
| |
− |
| |
− | * '''Scaling with a Queue:'''
| |
− | :* Timeout error on inserting to the database. The database can't keep up with the load, so write requests are timing out.
| |
− | :* Instead of having the web server hit the database directly, you insert a queue between the web server and the database.
| |
− | :* If the database ever gets overloaded again, the queue will just get bigger instead of timing out to the web server and potentially losing data.
| |
− | :* Unfortunately, if the growing continues, adding a queue and doing batch updates was only a band-aid for the scaling problem.
| |
− |
| |
− |
| |
− | * '''Scaling by Sharding the Database:'''
| |
− | :* '''Sharding''' is a type of database partitioning that separates very large databases into smaller, faster, more easily managed parts called data shards.
| |
− | :* To use multiple database servers and spread the table across all the servers. Each server will have a subset of the data for the table.
| |
− | :* You have to modify your top-100-URLs query to get the top 100 URLs from each shard and merge those together for the global top 100 URLs.
| |
− | :* As the application gets more and more popular, you keep having to reshard the database into more shards to keep up with the write load.
| |
− | :* Eventually you have so many shards that it becomes a challenge to manage all requests in a single transaction in case of any error or problems.
| |
− |
| |
− |
| |
− | :* As the simple web analytics application evolved, the system continued to get more and more complex: queues, shards, replicas, resharding scripts, and so on.
| |
− | :* One problem is that your database is not self-aware of its distributed nature, so it can’t help you to deal with shards, replication, and distributed queries. All that complexity got pushed to you both in operating the database and developing the application code.
| |
− | :* The system is not engineered for human mistakes. Quite the opposite, the system keeps getting more and more complex, making it more and more likely that a mistake will be made.
| |
− | :* Human-fault tolerance is not optional. It’s essential, especially when Big Data adds so many more complexities to building applications.
| |
− |
| |
− |
| |
− | * '''How will Big Data Techniques help?:'''
| |
− | :* Databases and computation systems used for Big Data are aware of their distributed nature. So things like sharding and replication are handled for you.
| |
− | :* When it comes to scaling, you’ll just add nodes, and the systems will automatically rebalance onto the new nodes.
| |
− | :* Immutable data: when you make a mistake, you might write bad data, but at least you won’t destroy good data. This is a much stronger human-fault tolerance guarantee than in a traditional system based on mutation.
| |
− | :* With traditional databases, you’d be wary of using immutable data because of how fast such a dataset would grow. But because Big Data techniques can scale to so much data, you have the ability to design systems in different ways.
| |
− | :* Immutable - unchanging over time or unable to be changed.
| |
− |
| |
− |
| |
− | <br />
| |
− | ==NoSQL==
| |
− | A NoSQL (originally referring to "non SQL" or "non relational") database provides a mechanism for the storage and retrieval of data which is modelled other than the tabular relations used in the relational databases.
| |
− |
| |
− | * Such databases have existed since the late 1960s, but did not obtain the name as "NoSQL".
| |
− | * Facebook, Google and eBay initiated the need and usage of NoSQL databases in big data and real-time web applications.
| |
− | * NoSQL systems are also called “Not only SQL” to emphasize that they may support SQL-like query languages.
| |
− |
| |
− | * NoSQL is for situations when the user is unsure of what structure to use for the data.
| |
− | * One common physical structure used by NoSQL is a string of key-value pairs.
| |
− | * A NoSQL technology adds a special application programming interface (API) to SQL to allow SQL to work with data not stored in the traditional relational table format.
| |
− | * NoSQL has the ability to handle large volumes of unstructured data faster and more efficiently than traditional relational database management systems.
| |
− |
| |
− |
| |
− | <br />
| |
− | ===Why NoSQL is not a panacea===
| |
− | * Hadoop and databases such as Cassandra and Riak systems can handle very large amounts of data, but with serious trade-offs.
| |
− |
| |
− | * Hadoop, for example, can parallelize large-scale batch computations on very large amounts of data, but the computations have high latency. You don’t use Hadoop for anything where you need low-latency results.
| |
− |
| |
− | * NoSQL databases like Cassandra achieve their scalability by offering you a much more limited data model than you’re used to with something like SQL. Squeezing your application into these limited data models can be very complex.
| |
− |
| |
− | * These tools on their own are not a panacea. But when intelligently used in conjunction with one another, you can produce scalable systems for arbitrary data problems with human-fault tolerance and a minimum of complexity.
| |
− |
| |
− |
| |
− | <br />
| |
− | ==Apache Hadoop==
| |
− | https://hadoop.apache.org/docs/r2.7.7/
| |
− |
| |
− | Hadoop is an open-source software framework for storing data and running applications on clusters of commodity hardware. It provides massive storage for any kind of data, enormous processing power and the ability to handle virtually limitless concurrent tasks or jobs. https://www.sas.com/en_ie/insights/big-data/hadoop.html
| |
− |
| |
− | Apache Hadoop is a collection of open-source software utilities that facilitate using a network of many computers to solve problems involving massive amounts of data and computation. It provides a software framework for distributed storage and processing of big data using the MapReduce programming model. https://en.wikipedia.org/wiki/Apache_Hadoop
| |
− |
| |
− |
| |
− | <br />
| |
− | ===Single Node Setup===
| |
− | https://hadoop.apache.org/docs/r1.2.1/single_node_setup.html
| |
− |
| |
− | This document describes how to set up and configure a single-node Hadoop installation so that you can quickly perform simple operations using Hadoop MapReduce and the Hadoop Distributed File System (HDFS).
| |
− |
| |
− |
| |
− | <br />
| |
− | ===MapReduce===
| |
− | https://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html
| |
− |
| |
− | Hadoop MapReduce is a software framework for easily writing applications which process vast amounts of data (multi-terabyte data-sets) in-parallel on large clusters (thousands of nodes) of commodity hardware in a reliable, fault-tolerant manner.
| |
− |
| |
− | A MapReduce job usually splits the input data-set into independent chunks which are processed by the map tasks in a completely parallel manner. The framework sorts the outputs of the maps, which are then input to the reduce tasks. Typically both the input and the output of the job are stored in a file-system. The framework takes care of scheduling tasks, monitoring them and re-executes the failed tasks.
| |
− |
| |
− |
| |
− | <br />
| |
− | ====Inputs and Outputs0====
| |
− | The MapReduce framework operates exclusively on <key, value> pairs, that is, the framework views the input to the job as a set of <key, value> pairs and produces a set of <key, value> pairs as the output of the job, conceivably of different types.
| |
− |
| |
− | The key and value classes have to be serializable by the framework and hence need to implement the Writable interface. Additionally, the key classes have to implement the WritableComparable interface to facilitate sorting by the framework.
| |
− |
| |
− | Input and Output types of a MapReduce job:
| |
− |
| |
− | (input) <k1, v1> -> map -> <k2, v2> -> combine -> <k2, v2> -> reduce -> <k3, v3> (output)
| |
− |
| |
− |
| |
− | <br />
| |
− | ====Example - WordCount====
| |
− | Before we jump into the details, lets walk through an example MapReduce application to get a flavour for how they work.
| |
− |
| |
− | WordCount is a simple application that counts the number of occurences of each word in a given input set.
| |
− |
| |
− | This works with a local-standalone, pseudo-distributed or fully-distributed Hadoop installation (Single Node Setup).
| |
− |
| |
− |
| |
− | <br />
| |
− | ===Hadoop Architecture===
| |
− | * Distributed, with some centralization.
| |
− |
| |
− | * Main nodes of cluster are where most of the computational power and storage of the system lies.
| |
− |
| |
− | * Main nodes run TaskTracker to accept and reply to MapReduce tasks, and also DataNode to store needed blocks closely as possible.
| |
− |
| |
− | * Central control node runs NameNode to keep track of HDFS directories & files, and JobTracker to dispatch compute tasks to TaskTracker.
| |
− |
| |
− | * Written in Java, also supports Python and Ruby.
| |
− |
| |
− |
| |
− | '''Hadoop framework includes following four modules:'''
| |
− | * '''Hadoop Common:''' These are Java libraries and utilities required by other Hadoop modules. These libraries provide the file system and OS level abstractions and contain the necessary Java files and scripts required to start Hadoop.
| |
− | * '''Hadoop Distributed File System (HDFS):''' A distributed file system that provides high-throughput access to the application data.
| |
− | * '''Hadoop YARN:''' This is a framework for the job scheduling and cluster resource management.
| |
− | * '''Hadoop MapReduce:''' This is a YARN-based system for parallel processing of large data sets.
| |
− |
| |
− |
| |
− | '''Hadoop Operation Modes:'''
| |
− | * '''Local/ Standalone Mode:''' After downloading Hadoop in your system, by default, it is configured in a standalone mode and can be run as a single java process.
| |
− | * '''Pseudo Distributed Mode:''' It is a distributed simulation on a single machine. Each Hadoop daemon such as hdfs, yarn, MapReduce etc., will run as a separate java process. This mode is useful for development.
| |
− | * '''Fully Distributed Mode:''' This mode is fully distributed with minimum two or more machines as a cluster. This is what you would get from AWS.
| |
− |
| |
− |
| |
− | '''Hadoop Framework Tools:'''
| |
− | :* '''Hbase:''' HBase is an open-source, non-relational, distributed database modelled after Google's Bigtable and written in Java.
| |
− |
| |
− | :* '''Chukwa:''' Chukwa is a Hadoop subproject devoted to large-scale log collection and analysis. Chukwa is built on top of the Hadoop distributed filesystem (HDFS) and MapReduce framework and inherits Hadoop’s scalability and robustness.
| |
− |
| |
− | :* '''Flume:''' Flume is a distributed, reliable, and available service for efficiently collecting, aggregating, and moving large amounts of log data. It has a simple and flexible architecture based on streaming data flows.
| |
− |
| |
− | :* '''Apache ZooKeeper:''' Apache ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.
| |
− |
| |
− | :* '''Apache Pig:''' Apache Pig is a platform for analyzing large data sets that consists of a high-level language for expressing data analysis programs, coupled with infrastructure for evaluating these programs.
| |
− |
| |
− | :* '''Apache Hive:''' Apache Hive is a data warehouse software project built on top of Apache Hadoop for providing data query and analysis. Hive gives a SQL-like interface to query data stored in various databases and file systems that integrate with Hadoop.
| |
− |
| |
− | :* '''Apache Sqoop(TM)''' is a tool designed for efficiently transferring bulk data between Apache Hadoop and structured datastores such as relational databases.
| |
− |
| |
− | :* '''Apache Avro''' is a remote procedure call and data serialization framework developed within Apache's Hadoop project. It uses JSON for defining data types and protocols, and serializes data in a compact binary format.
| |
− |
| |
− |
| |
− | <br />
| |
− | ===HDFS===
| |
− | * HDFS is a file system written in Java based on the Google’s file system (GFS).
| |
− | * Provides the redundant storage for massive amounts of data.
| |
− | * All HDFS communication protocols are layered on top of the TCP/IP protocol.
| |
− | * A client establishes a connection to a configurable TCP port on the Namenode machine. It talks to ClientProtocol with the Namenode.
| |
− | * Namenode keeps the directory tree of all files in the file system, and tracks where across the cluster the file data is kept.
| |
− |
| |
− |
| |
− | * '''Chunking:'''
| |
− | :* When you upload a file to HDFS, the file is first chunked into blocks of a fixed size, typically between 64MB and 256MB.
| |
− |
| |
− |
| |
− | * '''Replication & Distribution:'''
| |
− | :* Each block is replicated across multiple datanodes (typically 3) that are chosen at random.
| |
− | :* A DataNode stores data in the [HadoopFileSystem]. A functional filesystem has more than one DataNode, with data replicated across them.
| |
− |
| |
− |
| |
− | * '''Features of HDFS:'''
| |
− | :* It is suitable for the distributed storage and processing.
| |
− |
| |
− | :* Hadoop provides a command line interface to interact with HDFS.
| |
− |
| |
− | :* The built-in servers of namenode and datanode help users to easily check the status of a cluster.
| |
− |
| |
− | :* Streaming access to file system data.
| |
− |
| |
− | :* HDFS provides file permissions and authentication.
| |
− |
| |
− |
| |
− | * '''HDFS Commands:'''
| |
− | :* The syntax of the commands is similar to bash.
| |
− |
| |
− | :* Make a directory:
| |
− | $ hadoop fs -mkdir user
| |
− |
| |
− | :* List contents of directory
| |
− | $ hadoop fs -ls user
| |
− |
| |
− | :* List contents of a sub directory
| |
− | $ hadoop fs -ls user/subDir
| |
− |
| |
− |
| |
− | * '''Files Stored :'''
| |
− | :* Files are divided into blocks.
| |
− | :* Blocks are divided across many machines at load time.
| |
− | :* Different blocks from the same file will be stored on different machines.
| |
− | :* Blocks are replicated across multiple machines.
| |
− | :* The NameNode keeps track of which blocks make up a file and where they are stored.
| |
− |
| |
− |
| |
− | * '''Namenode:'''
| |
− | :* The system having the namenode acts as the master server and it does the following tasks:
| |
− | ::* Manages the file system namespace.
| |
− | ::* Regulates client’s access to files.
| |
− | ::* It also executes file system operations, such as renaming, closing, and opening files and directories.
| |
− |
| |
− |
| |
− | * '''Datanode:'''
| |
− | :* For every node (Commodity hardware/ System) in a cluster, there will be a datanode. These nodes manage the data storage of their system.
| |
− | :* Datanodes perform read-write operations on the file systems, as per client request.
| |
− | :* They also perform operations, such as block creation, deletion, and replication according to the instructions of the namenode.
| |
− |
| |
− |
| |
− | <br />
| |
− | ===MapReduce===
| |
− | :* MapReduce is a processing technique and a program model for distributed computing based on java.
| |
− | :* The MapReduce algorithm contains two important tasks, namely Map and Reduce.
| |
− | :* Map takes a set of data and converts it into another set of data, where individual elements are broken down into tuples (key/value pairs).
| |
− | :* Secondly, reduce task, which takes the output from a map as an input and combines those data tuples into a smaller set of tuples.
| |
− |
| |
− | :* As the sequence of the name MapReduce implies, the reduce task is always performed after the map job.
| |
− | :* The major advantage of MapReduce is that it is easy to scale data processing over multiple computing nodes.
| |
− | :* Scaling the application to run over hundreds, thousands, or even tens of thousands of machines in a cluster is merely a configuration change.
| |
− | :* This simple scalability is what has attracted many programmers to use the MapReduce model.
| |
− |
| |
− |
| |
− | :* '''Map Stage:''' The map or mapper’s job is to process the input data. Generally the input data is in the form of file or directory and is stored in the Hadoop file system (HDFS). The input file is passed to the mapper function line by line. The mapper processes the data and creates several small chunks of data.
| |
− |
| |
− |
| |
− | :* '''Reduce Stage:''' This stage is the combination of the Shuffle stage and the Reduce stage. The Reducer’s job is to process the data that comes from the mapper. After processing, it produces a new set of output, which will be stored in the HDFS.
| |
− |
| |
− |
| |
− | :* '''MapReduce Steps:'''
| |
− | ::* '''Splitting''' – The splitting parameter can be anything, e.g. splitting by space, comma, semicolon, or even by a new line (‘\n’).
| |
− | ::* '''Mapping''' – It takes a set of data and converts it into another set of data, where individual elements are broken down into tuples (Key-Value pair).
| |
− | ::* '''Intermediate splitting''' – The entire process is performed in parallel on different clusters. In order to group them in “Reduce Phase” the similar KEY data should be on same cluster.
| |
− | ::* '''Reduce''' – It is mostly a group by phase.
| |
− | ::* '''Combining''' – The last phase where all the data (individual result set from each cluster) is combined together to form a Result.
| |
− |
| |
− |
| |
− | * '''Example map (I):'''
| |
− | <syntaxhighlight lang="java">
| |
− | function map(values, f) {
| |
− | var results = [];
| |
− | for (var i = 0 ; i < values.length ; i++) {
| |
− | results.push(f(values[i]));
| |
− | }
| |
− | return results;
| |
− | }
| |
− | </syntaxhighlight>
| |
− |
| |
− | :* Here's a Javascript implementation of map
| |
− | ::* Create a new array
| |
− | ::* Apply the function f to each element of the input array (values) and append (push in Javascript) the result to the output array
| |
− |
| |
− |
| |
− | <br />
| |
− | ===Three main applications of Hadoop===
| |
− | :* Advertisement (Mining user behavior to generate recommendations)
| |
− |
| |
− | :* Searches (group related documents)
| |
− |
| |
− | :* Security (search for uncommon patterns)
| |
− |
| |
− |
| |
− | <br />
| |
− | ==Service-Oriented Architecture (SOA)==
| |
− |
| |
− | *A service-oriented architecture (SOA) is a style of software design where services are provided to the other components by application components, through a communication protocol over a network.
| |
− |
| |
− | *A service is a discrete unit of functionality that can be accessed remotely and acted upon and updated independently, such as retrieving a credit card statement online.
| |
− |
| |
− | *SOA provides access to reusable Web services over a TCP/IP network,
| |
− |
| |
− |
| |
− | <br />
| |
− |
| |
− | ==Web services==
| |
− |
| |
− | *A software component stored on one computer that can be accessed via method calls by an application (or other software component) on another computer over a network
| |
− |
| |
− | *Web services communicate using such technologies as:
| |
− | **XML, JSON and HTTP
| |
− | **'''Simple Object Access Protocol (SOAP):''' An XML-based protocol that allows web services and clients to communicate in a platform-independent manner
| |
− |
| |
− |
| |
− | '''Why Web services:'''
| |
− |
| |
− | *By using web services, companies can spend less time developing new applications and can create new applications in an innovative way in small amount of time.
| |
− | *Amazon, Google, eBay, PayPal and many others make their server-side applications available to their partners via web services.
| |
− |
| |
− |
| |
− | '''Basic concepts:'''
| |
− |
| |
− | *'''Remote machine or server:''' The computer on which a web service resides
| |
− | *'''A client application''' that accesses a web service sends a method call over a network to the remote machine, which processes the call and returns a response over the network to the application
| |
− | *'''Publishing (deploying) a web service:''' Making a web service available to receive client requests.
| |
− | *'''Consuming a web service:''' Using a web service from a client application.
| |
− | *In Java, a web service is implemented as a class that resides on a server.
| |
− |
| |
− |
| |
− | '''An application that consumes a web service (client) needs:'''
| |
− |
| |
− | *An object of a '''''proxy class''''' for interacting with the web service.
| |
− | *The ''proxy object'' handles the details of communicating with the web service on the client's behalf
| |
− |
| |
− | [[File:Interaction_between_a_web_service_client_and_a_web_service.png|709x709px|thumb|center]]
| |
− |
| |
− |
| |
− | '''JAX-WS:'''
| |
− |
| |
− | *The Java API for XML Web Services (JAX-WS) is a Java programming language API for creating web services, particularly SOAP services. JAX-WS is one of the Java XML programming APIs. It is part of the Java EE platform.
| |
− | **Requests to and responses from web services are typically transmitted via SOAP.
| |
− | **Any client capable of generating and processing SOAP messages can interact with a web service, regardless of the language in which the web service is written.
| |
− |
| |
− |
| |
− | '''Traditional Web-Based Systems vs Web Services-Based Systems:'''
| |
− |
| |
− | [[File:Traditional_Web_Based_Systems.jpg|596x596px|thumb|center|Traditional Web-Based Systems]]
| |
− |
| |
− | [[File:Web_services_based_systems.jpg|597x597px|thumb|center|Web services based systems]]
| |
− |
| |
− |
| |
− | <br />
| |
− | ===Creating - Deploying and Testing a Java Web Service using NetBeans===
| |
− | Esta sección fue realizada a partir de la siguiente presentación, la cual forma parte del material del curso de Big Data del Prof. Muhammad Iqbal: [[:File:Lecture_4-Big_Data_integration-Web_Service.pptx]]
| |
− |
| |
− | El proyecto NetBeans que contiene el Java Web Service resultante de este tutorial: [[:File:CalculatorWSApplication.zip]]
| |
− |
| |
− |
| |
− | Netbeans 6.5 - 9 and Java EE enable programmers to "publish (deploy)" and/or "consume (client request)" web services In Netbeans, you focus on the logic of the web service and let the IDE handle the web service's infrastructure.
| |
− |
| |
− |
| |
− | *We first need to to do some configuration in NetBeans:
| |
− | **Go to /usr/local/netbeans-8.2/etc/netbeans.conf:
| |
− | ***Find the line: ''netbeans_default_options''
| |
− | ***If ''-J-Djavax.xml.accessExternalSchema=all'' is not between the quotes then paste it in.
| |
− |
| |
− |
| |
− | *If you are deploying to the ''GlassFish'' Server you need to modify the configuration file of the ''GlassFish'' Server (''domain.xml''):
| |
− | **/usr/local/glassfish-4.1.1/glassfish/domains/domain1/config/domain.xml
| |
− | ***Find : ''<java-config''
| |
− | ***Check the ''jvm-options'' for the following configuration: '''''<jvm-options>-Djavax.xml.accessExternalSchema=all</jvm-options>'''''
| |
− | ***It should be there by default, if not paste it in, save file and exit
| |
− | ***You can now start Netbeans IDE
| |
− |
| |
− | <br />
| |
− |
| |
− | *'''Create a Web Service in NetBeans- Locally'''
| |
− | **Choose File > New Project:
| |
− | **Select Web Application from the Java Web category
| |
− | **Change Project Name: to CalculatorWSApplication
| |
− | **Set the server to GlassFish 4.1.1
| |
− | **Set Java EE Version: Java EE 7 Web
| |
− | **Set Context path: /CalculatorWSApplication
| |
− | **After that you should now have a project created in the Projects view on the left hand side.
| |
− |
| |
− | <br />
| |
− |
| |
− | :*'''Creating a WS from a Java Class:'''
| |
− | ::*Right-click the CalculatorWSApplication node and choose New > Web Service.
| |
− | :::*If the option is not there choose Other > Web Services > Web Service
| |
− | ::*Click Next
| |
− | ::*Name the web service CalculatorWS and type ''com.hduser.calculator'' in Package. Leave Create Web Service from Scratch selected.
| |
− | ::*Select Implement Web Service as a Stateless Session Bean.
| |
− | ::*Click Finish. The Projects window displays the structure of the new web service and the Java class (CalculatorWS.java) is generate and automatically shown in the editor area. A default hello web service is created by Netbeans.
| |
− |
| |
− |
| |
− | ::*'''Adding an Operation to the WS (CalculatorWS.java):'''
| |
− | :::*Open CalculatorWS.java WS.
| |
− | :::*Change to the Design view in the editor.
| |
− | :::*Click the Add operation button.
| |
− | :::*In the upper part of the Add Operation dialog box, type '''''add''''' in ''Name'' and type '''''int'''''' in the ''Return Type'' drop-down list.
| |
− | :::*In the lower part of the Add Operation dialog box, click Add and create a parameter of type '''''int''''' named '''''num_1'''''.
| |
− | :::*Click Add again and create a parameter of type int called num_2.
| |
− | :::*Click OK at the bottom of the panel to add the operation.
| |
− | :::*Remove the default hello operation: Right click on hello operation and choose: Remove Operation
| |
− | :::*Click on the source view to go back to view the code in the editor.
| |
− | :::*You will see the default hello code is gone and the new add method is now there instead.
| |
− | :::*Now we have to alter the code to look like this.
| |
− |
| |
− | <blockquote>
| |
− | <blockquote>
| |
− | <syntaxhighlight lang="java">
| |
− | package com.adelo.calculator;
| |
− |
| |
− | import javax.jws.WebService;
| |
− | import javax.jws.WebMethod;
| |
− | import javax.jws.WebParam;
| |
− | import javax.ejb.Stateless;
| |
− |
| |
− | @WebService(serviceName = "CalculatorWS")
| |
− | @Stateless()
| |
− | public class CalculatorWS {
| |
− |
| |
− | /**
| |
− | * Web service operation
| |
− | */
| |
− | @WebMethod(operationName = "add")
| |
− | public int add(@WebParam(name = "num_1") int num_1, @WebParam(name = "num_2") int num_2) {
| |
− | //TODO write your implementation code here:
| |
− | int result = num_1 + num_2;
| |
− | return result;
| |
− | }
| |
− | }
| |
− | </syntaxhighlight>
| |
− | </blockquote>
| |
− | </blockquote>
| |
− |
| |
− |
| |
− | :*Well done, you have just created your first Web Service.
| |
− |
| |
− |
| |
− | :*To test the Web service drop down the Web Services directory and right click on CalculatorWSApplication.
| |
− | :*Choose Test Web service.
| |
− | :*Netbeans throws an error: It is letting us know that we have not deployed our Web Service.
| |
− |
| |
− |
| |
− | :*'''Right click on the main Project node and select deploy'''
| |
− |
| |
− |
| |
− | *'''Testing the WS:'''
| |
− |
| |
− | :*Deploying the Web Service will automatically start the GlassFish server. Allow the server to start, this will take a little while. You can check the progress by clicking on the GlassFish tab at the bottom of the IDE.
| |
− | :*Wait until you see: «CalculatorWSApplication was successfully deployed in 9,912 milliseconds»
| |
− | :*Now you can right click on the Web Service as before and choose Test Web Service.
| |
− | :*The browser will open and you can now test the Web service and view the WSDL file.
| |
− | :*You can also view the Soap Request and Response.
| |
− |
| |
− |
| |
− | <br />
| |
− | ====Understanding a Web Service Java Class====
| |
− | * Each new web service class created with the JAX-WS APIs is a POJO (plain old Java object)
| |
− | :* You do not need to extend a class or implement an interface to create a Web service.
| |
− |
| |
− | * When you compile a class that uses the following JAX-WS 2.0 annotations, the compiler creates the compiled code framework that allows the web service to wait for and respond to client requests:
| |
− |
| |
− | :* '''''@WebService'''''(«Optional elements»)
| |
− | ::* Indicates that a class represents a web service.
| |
− | ::* Optional element '''''name''''' specifies the name of the proxy class that will be generated for the client.
| |
− | ::* Optional element '''''serviceName''''' specifies the name of the class that the client uses to obtain a proxy object.
| |
− | ::* Netbeans places the '''''@WebService''''' annotation at the beginning of each new web service class you create.
| |
− | ::* You can add the optional '''''name''''' and '''''serviceName''''' this way: ''@WebService(serviceName = "CalculatorWS")''
| |
− |
| |
− |
| |
− | :* '''''@WebMethod'''''(«Optional elements»)
| |
− | ::* Methods that are tagged with the '''''@WebMethod''''' annotation can be called remotely.
| |
− | ::* Methods that are not tagged with '''''@WebMethod''''' are not accessible to clients that consume the web service.
| |
− | ::* Optional '''''operationName''''' element to specify the method name that is exposed to the web service's client.
| |
− |
| |
− |
| |
− | :* '''''@WebParam annotation'''''(«Optional elements»)
| |
− | ::* Optional element '''''name''''' indicates the parameter name that is exposed to the web service's clients.
| |
− |
| |
− |
| |
− | <br />
| |
− | ===Consuming the Web Service===
| |
− | Netbeans 6.5 - 9 and Java EE enable programmers to "publish (deploy)" and/or "consume (client request)" web services
| |
− |
| |
− |
| |
− | <br />
| |
− | ====Creating a Java Web Application that consumes a Web Service====
| |
− | El proyecto NetBeans resultante de esta sección: [[:File:CalculatorWSJSPClient.zip]]
| |
− |
| |
− |
| |
− | *Now that we have a web service we need a client to consume it.
| |
− | *Choose File > New Project
| |
− | *Select Web Application from the Java Web category
| |
− | *Name the project ''CalculatorWSJSPClient''
| |
− | *Leave the server and java version as before and click Finish.
| |
− |
| |
− |
| |
− | *Expand the Web Pages node under the project node and delete index.html.
| |
− | *Right-click the Web Pages node and choose New > JSP in the popup menu.
| |
− | **If JSP is not available in the popup menu, choose New > Other and select JSP in the Web category of the New File wizard.
| |
− | *Type index for the name of the JSP file in the New File wizard. Click Finish to create the JSP (Java Server Page)
| |
− |
| |
− |
| |
− | *Right-click the ''CalculatorWSJSPClient'' node and choose ''New > Web Service Client''.
| |
− | **If the option is not there choose ''Other > Web Services > Web Service Client''
| |
− |
| |
− | *'''To consume a locally Java Web Service:'''
| |
− | **Select Project as the WSDL source. Click Browse. Browse to the CalculatorWS web service in the CalculatorWSApplication project. When you have selected the web service, click OK.
| |
− |
| |
− | *'''To consume a Web Service in another location:''' For example, the one that is available at http://vhost3.cs.rit.edu/SortServ/Service.svc?singleWsdl
| |
− | **Specify the WSDL URL as: http://vhost3.cs.rit.edu/SortServ/Service.svc?singleWsdl
| |
− | **(we are going to explain more about this WS in the next section. For this tutorial we are going to continue with the example of our locally Java Web Service CalculatorWS).
| |
− |
| |
− | *Do not select a package name. Leave this field empty.
| |
− | *Leave the other settings as default and click Finish.
| |
− |
| |
− | *The WSDL gets parsed and generates the .java
| |
− | *The Web Service References directory now contains the add method we created in our web service.
| |
− | *Drag and drop the add method just below the H1 tags in index.jsp
| |
− | *The Code will be automatically generated.
| |
− | *Change the values of ''num_1'' and ''num_2'' to any two numbers e.g. 5 and 5 as per test earlier.
| |
− | *Remove the TODO line from the catch block of the code and paste in:
| |
− |
| |
− | :out.println("exception" + ex);
| |
− | :If there is an error this will help us identify the problem.
| |
− |
| |
− |
| |
− | *'''IMPORTANT Once you close Netbeans you are shutting down your server. If you want to reuse a Web Service you must re-deploy.'''
| |
− |
| |
− |
| |
− | <br />
| |
− | ====Creating a Java project that consumes a Web Service====
| |
− | Esta sección fue realizada a partir del siguiente tutorial: [[:File:Creating_a_Java_project_that_consumes_a_Web_Service.zip]]
| |
− |
| |
− | El proyecto NetBeans resultante de esta sección: [[:File:SortClient.zip]]
| |
− |
| |
− |
| |
− | This document provides step-by-step instructions to consume a web service in Java using NetBeans IDE.
| |
− |
| |
− | In the project, we will invoke a sorting web service through its WSDL link: http://vhost3.cs.rit.edu/SortServ/Service.svc?singleWsdl
| |
− |
| |
− |
| |
− | *'''Step 1 - Createa JavaProject:'''
| |
− | **We are going to name it: SortClient
| |
− |
| |
− |
| |
− | *'''Step 2 - Generate a Web Service Client:'''
| |
− | **After the Java Project has been created, go to the Project Tree Structure, Right click on Project and select New and then choose Web Service Client.
| |
− | **Specify the WSDL URL as: http://vhost3.cs.rit.edu/SortServ/Service.svc?singleWsdl
| |
− | **Click Finish
| |
− |
| |
− |
| |
− | *'''Step 3 - Invoke the Service:'''
| |
− | **Expand the Web Service References until you see the operation lists. Drag the operation you want to invoke to the source code window, such as "GetKey". A piece of code is automatically generated to invoke that operation.
| |
− | **Drag MergeSort to the source code window and the corresponding code is automatically generated,too.
| |
− | **In the main function, add the code to call the two functions: getKey() and mergeSort();As it is a call to a remote service, RemoteException needs to be listed in the throws cause
| |
− |
| |
− |
| |
− | <br />
| |
− | ===Another example 1 - Creating a Java Web Application that consume a Web Service using NetBeans===
| |
− | This is the assignment I did for this part of the course, the Netbeans project is available here: [[:File:MyWSJSPClient.zip]]
| |
− |
| |
− |
| |
− | The following image is the Web Application created from MyWSJSPClient:
| |
− | [[File:Java_Web_Application_that_consume_two_Web_Services.png|1000px|thumb|center|Java Web Application that consume two Web Services. The Sort Web Service Tester is consume from http://vhost3.cs.rit.edu/SortServ/Service.svc?Wsdl. The Calculator Web Service is locally consumed from the '''''CalculatorWSApplication''''' project we created at [[Big Data Integration#Creating - Deploying and Testing a Java Web Service using NetBeans]]]]
| |
− |
| |
− |
| |
− | <br />
| |
− | ===Another example 2 - Creating a Java Web Application that consume a Web Service using NetBeans===
| |
− | In this page we can find another example of a Java EE web application that contains a web service:
| |
− | https://netbeans.org/kb/docs/websvc/flower_overview.html
| |
− |
| |
− |
| |
− | <br />
| |