Sunday 20 January 2019

Shiny Table example

library(shiny)
ui <- fluidPage(
   titlePanel("TABLE"),
    sidebarLayout(
      sidebarPanel(
        sliderInput("num", "integer", 1, 20, 1,
                    step = 1, animate =
                      animationOptions(interval=400, loop=TRUE))),
      mainPanel(
        tableOutput("prod")
      )) )
 
server <- function(input, output) {
    output$prod <- renderPrint({ x<-input$num
    for(i in 1:10){
      a=x*i
      cat(x,"x",i,"=",a,"<br>")
       }})}

shinyApp(ui = ui, server = server)

Friday 18 January 2019

Payroll Solidity

contract Payroll {
    // define variables
    address public CompanyOwner;
   
    uint employeeId;
   
    // create Employee object
    struct Employee {
        uint employeeId;
        address employeeAddress;
        uint wages;
        uint balance;
    }
   
    // employees contain Employee
    Employee[] public employees;
   
    // run this on contract creation just once
    function Payroll() {
        // make CompanyOwner the person who deploys the contract
        CompanyOwner = msg.sender;
        employeeId = 0;
        // add the company owner as an employee with a wage of 0
        AddEmployee(msg.sender, 0);
    }
   
    // function for checking number of employees
    function NumberOfEmployees() returns (uint _numEmployees) {
        return employeeId;
    }
   
    // allows function initiator to withdraw funds if they're an employee equal to their balance
    function WithdrawPayroll() returns (bool _success) {
        var employeeId = GetCurrentEmployeeId();
        if (employeeId != 999999) {
            // they are an employee
            if (employees[employeeId].balance > 0) {
                // if they have a balance
                if (this.balance >= employees[employeeId].balance) {
                    // if the balance of the contract is greater than or equal to employee balance
                    // then send them the money from the contract and set balance back to 0
                    msg.sender.send(employees[employeeId].balance);
                    employees[employeeId].balance = 0;
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
   
    function GetCurrentEmployeeId() returns (uint _employeeId) {
        // loop through employees
        for (var i = 0; i < employees.length; i++) {
            // if the initiator of the function's address exists in the employeeAddress area of an Employee, return ID
            if (msg.sender == employees[i].employeeAddress) {
                return employees[i].employeeId;
            }
        }
        return 999999;
    }
   
    // function for getting an ID by address if needed
    function GetEmployeeIdByAddress(address _employee) returns (uint _employeeId) {
        for (var i = 0; i < employees.length; i++) {
            if (_employee == employees[i].employeeAddress) {
                return employees[i].employeeId;
            }
        }
        return 999999;
    }
   
   
    /* OWNER ONLY FUNCTIONS */
    // add an employee given an address and wages
    function AddEmployee(address _employee, uint _wages) returns (bool _success) {
        if (msg.sender != CompanyOwner) {
            return false;
        } else {
            employees.push(Employee(employeeId, _employee, _wages, 0));
            employeeId++;
            return true;
        }
    }
   
    // pay the employee their wages given an employee ID
    function PayEmployee(uint _employeeId) returns (bool _success) {
        if (msg.sender != CompanyOwner) {
            return false;
        } else {
            // TODO: need to figure out how to check to make sure employees[_employeeId] exists
            employees[_employeeId].balance += employees[_employeeId].wages;
            return true;
        }
    }
   
    // modify the employee wages given an employeeId and a new wage
    function ModifyEmployeeWages(uint _employeeId, uint _newWage) returns (bool _success) {
        if (msg.sender != CompanyOwner) {
            return false;
        } else {
            // TODO: need to figure out how to check to make sure employees[_employeeId] exists
            // change employee wages to new wage
            employees[_employeeId].wages = _newWage;
            return true;
        }
    }
   
    // check how much money is in the contract
    function CheckContractBalance() returns (uint _balance) {
        return this.balance;
    }
   
}

Friday 11 January 2019

Spart link

https://spark.apache.org/examples.html

Spark MR WC

val myfile = sc.textFile("/user/cloudera/input.txt")

val counts = myfile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey(_ + _)

counts.saveAsTextFile("/home/cloudera/Desktop/output")

MR using JAVA


Source : https://dzone.com/articles/word-count-hello-word-program-in-mapreduce

Thursday 10 January 2019

sqoop dataset


sqoop import --connect jdbc:mysql://localhost:3306/sqoop --username root -P --split-by id --columns id,name --table customer --target-dir /user/cloudera/customer1 --fields-terminated-by “,” --hive-import --create-hive-table --hive-table sqoop.customer1;

Sqoop


1. create database sqoop;
            2.  Use sqoop;
            3. Create table customer( id varchr(3), name varchar(10), age varchar(3), sal  
                integer(6));
            4. insert into values (‘1’,’john’,’45’,45000);
            5. select * from customer

Most occurred first character in the word of a file -PIG


Most occurred first character in the word of a  file


lines  = LOAD '/user/cloudera/my-friends' AS (line: chararray); 
tokens = FOREACH lines GENERATE flatten(TOKENIZE(line)) As token:chararray;
letters = FOREACH tokens  GENERATE SUBSTRING(token,0,1) As letter:chararray;
lettergrp = GROUP letters by letter; 
countletter  = FOREACH  lettergrp  GENERATE group,COUNT(letters);
OrderCnt = ORDER countletter BY $1 DESC; 
result = LIMIT OrderCnt 1;
STORE result into '/user/cloudera/dummy5556777777';

PIG WC


A = load '/user/cloudera/55';
B = foreach A generate flatten(TOKENIZE((chararray)$0)) as word;
C = filter B by word matches '\\w+';
D = group C by word;
E = foreach D generate COUNT(C),group;
store E into '/user/cloudera/n66';

PIG more functions



Step 1:  create file “my-friend” with some rows like
1,abc,1892
2,xyz,1894
3,apple,1896
4,orange,1892
Step 2: upload to HDFS file browser
Step3:  Grunt> log = LOAD ‘/user/cloudera/my-friend’ AS (sno,name,dob);
Step 4: grunt> A = FOREACH  log generate sno,name,dob;
Step 5: describe A;
Step 6: B = GROUP A BY dob;
Step 7: DUMP B;
Result:
 (1892,{(4,orange,1892),(1,abc,1892)})
(1894,{(2,xyz,1894)})
(1896,{(3,apple,1896)})
Step 8: cntd = FOREACH B GENERATE group,COUNT(A) as cnt;
Step 9:  STORE B  INTO ‘ouput’;

PIG

Grunt>A = LOAD '/user/cloudera/mymovie' using PigStorage(',') as (col1: int,col2:chararray,col3:chararray,col4:chararray,col5:chararray);


Grunt> DUMP A;

Grunt> B = FOREACH A generate col1,col4,col5;

Grunt> DUMP B;
CREATE TABLE feedback_vnr(comments STRING);

load data LOCAL INPATH '/home/cloudera/Desktop/file.txt' INTO TABLE feedback_vnr;


select * from feedback_vnr;

select split(comments,' ') FROM feedback_vnr;
select explode(split(comments,' ')) FROM feedback_vnr
select word,count(*) from (select explode(split(comments,' ')) as word from feedback_vnr)  tmp GROUP BY word;

Saturday 5 January 2019

Most occurred first character in the word of a file


lines  = LOAD '/user/cloudera/my-friends' AS (line: chararray); 
tokens = FOREACH lines GENERATE flatten(TOKENIZE(line)) As token:chararray;
letters = FOREACH tokens  GENERATE SUBSTRING(token,0,1) As letter:chararray;
lettergrp = GROUP letters by letter; 
countletter  = FOREACH  lettergrp  GENERATE group,COUNT(letters);
OrderCnt = ORDER countletter BY $1 DESC; 
result = LIMIT OrderCnt 1;
STORE result into '/user/cloudera/dummy5556777777';