java Hive相关说明

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Hive相关说明相关的知识,希望对你有一定的参考价值。

package org.anvard.introtojava.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.anvard.introtojava.Person;
import org.springframework.util.Assert;
public class PersonDao {
    private DataSource ds;
    public PersonDao(DataSource ds) {
        this.ds = ds;
    }
    public void create(Person person) throws Exception {
        Assert.notNull(person);
        try {
            Connection c = ds.getConnection();
            PreparedStatement stmt = c
                    .prepareStatement("INSERT INTO person (id, first_name, last_name) values (?, ?, ?)");
            stmt.setInt(1, person.getId());
            stmt.setString(2, person.getFirstName());
            stmt.setString(3, person.getLastName());
            stmt.executeUpdate();
            c.close();
        } catch (SQLException e) {
            throw new Exception(e);
        }
    }
    public Person retrieve(int id) throws Exception {
        try {
            Connection c = ds.getConnection();
            PreparedStatement stmt = c
                    .prepareStatement("SELECT id, first_name, last_name FROM person WHERE id = ?");
            stmt.setInt(1, id);
            ResultSet rs = stmt.executeQuery();
            if (!rs.first()) {
                return null;
            }
            Person p = new Person();
            p.setId(rs.getInt(1));
            p.setFirstName(rs.getString(2));
            p.setLastName(rs.getString(3));
            c.close();
            return p;
        } catch (SQLException e) {
        	throw new Exception(e);
        }
    }
    public void update(Person person) throws Exception {
        Assert.notNull(person);
        try {
            Connection c = ds.getConnection();
            PreparedStatement stmt = c
                    .prepareStatement("UPDATE person SET first_name=?, last_name=? WHERE id=?");
            stmt.setString(1, person.getFirstName());
            stmt.setString(2, person.getLastName());
            stmt.setInt(3, person.getId());
            stmt.executeUpdate();
            c.close();
        } catch (SQLException e) {
        	throw new Exception(e);
        }
    }
    public void delete(int id) throws Exception {
        try {
            Connection c = ds.getConnection();
            PreparedStatement stmt = c
                    .prepareStatement("DELETE FROM person WHERE id=?");
            stmt.setInt(1, id);
            stmt.executeUpdate();
            c.close();
        } catch (SQLException e) {
        	throw new Exception(e);
        }
    }
}

import org.paukov.combinatorics.Factory;
import org.paukov.combinatorics.Generator;
import org.paukov.combinatorics.ICombinatoricsVector;

import java.util.StringTokenizer;


public class demograhicLikelihood {


    public demograhicLikelihood() {}

    private double formula(double [] numblist)
    {
        Integer elments[] = new Integer[numblist.length];
        for (Integer i = 0 ; i < numblist.length; i++)
        {
            elments[i] = i;
        }
        double masterSum = 0;
        for ( int i = 0 ; i < numblist.length ; i++)
        {
            double tempSum = 0;
            // Create the initial vector
            ICombinatoricsVector<Integer> initialVector = Factory.createVector(elments);
            // Create the generator
            Generator<Integer> generator = Factory.createSimpleCombinationGenerator(initialVector, i + 1);
            for (ICombinatoricsVector<Integer> perm : generator)
            {
                double tempproduct = 1;
                for( int j = 0; j < perm.getSize(); j++)
                {
                    tempproduct = tempproduct * numblist[perm.getValue(j)];

                }
                tempSum = tempSum + tempproduct;
            }
            if( (i + 1) % 2 == 1)
                masterSum = masterSum + tempSum;
            else
                masterSum = masterSum - tempSum;
        }
        return masterSum;
    }
    public double evaluate(int headcount, int children, String prochild, String proconversion, String proconversionset)
    {
        double s_list[] = new double[8];
        double proconv;
        double probabilityofchild;
        if(prochild != null && proconversion != null && !"".equals(proconversionset))
        {
            proconv = Double.parseDouble(proconversion);
            probabilityofchild = Double.parseDouble(prochild);
            StringTokenizer str = new StringTokenizer(proconversionset, "~");
            int i = 0;
            while (str.hasMoreElements()) {
                s_list[i] = Double.parseDouble(str.nextToken());
                i++;
            }
        }

        else
        {
            return 0.0D;
        }
        for (double aS_list : s_list) {
            System.out.println(aS_list);
        }
        //s1
        if ( children == 0 && headcount == 1 )
        {
            return 1;
        }
        else if ( children != 0 && headcount == 1 )
        {
            double tmp_array[ ] = { s_list[ 0 ], probabilityofchild };
            return ( proconv / formula( tmp_array ) );
        }
        //s2
        else if ( children == 0 && headcount == 2 )
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ] };
            return ( proconv / formula( tmp_array ) );
        }
        else if (children != 0 && headcount == 2 )
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ] , probabilityofchild };
            return ( proconv / formula( tmp_array ) );
        }
        //s3
        else if ( children == 0 && headcount == 3 )
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ], s_list[ 2 ] };
            return ( proconv / formula( tmp_array ));
        }
        else if ( children != 0 && headcount == 3 )
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ], s_list[ 2 ], probabilityofchild };
            return ( proconv / formula( tmp_array ) );
        }
        //s4
        else if ( children == 0 && headcount == 4 )
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ], s_list[ 2 ], s_list[ 3 ]};
            return ( proconv / formula( tmp_array ));
        }
        else if ( children != 0 && headcount == 4)
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ], s_list[ 2 ], s_list[ 3 ], probabilityofchild };
            return ( proconv / formula( tmp_array ));
        }
        //s5
        else if ( children == 0 && headcount == 5 )
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ], s_list[ 2 ], s_list[ 3 ], s_list[ 4 ]};
            return ( proconv / formula( tmp_array ));
        }
        else if ( children != 0 && headcount == 5)
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ], s_list[ 2 ], s_list[ 3 ],s_list[ 4 ], probabilityofchild };
            return ( proconv / formula( tmp_array ));
        }
        //s6
        else if ( children == 0 && headcount == 6 )
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ], s_list[ 2 ], s_list[ 3 ], s_list[ 4 ], s_list[ 5 ]};
            return ( proconv / formula( tmp_array ));
        }
        else if ( children != 0 && headcount == 6)
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ], s_list[ 2 ], s_list[ 3 ],s_list[ 4 ],s_list[ 5 ],
                    probabilityofchild };
            return ( proconv / formula( tmp_array ));
        }
        //s7
        else if ( children == 0 && headcount == 7 )
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ], s_list[ 2 ], s_list[ 3 ], s_list[ 4 ], s_list[ 5 ],
                    s_list[ 6 ]};
            return ( proconv / formula( tmp_array ));
        }
        else if ( children != 0 && headcount == 7)
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ], s_list[ 2 ], s_list[ 3 ],s_list[ 4 ],s_list[ 5 ],
                    s_list[ 6 ], probabilityofchild };
            return ( proconv / formula( tmp_array ));
        }
        //s8
        else if ( children == 0 && headcount == 8 )
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ], s_list[ 2 ], s_list[ 3 ], s_list[ 4 ], s_list[ 5 ],
                    s_list[ 6 ], s_list[ 7 ] };
            return ( proconv / formula( tmp_array ));
        }
        else if ( children != 0 && headcount == 8)
        {
            double tmp_array[ ] = { s_list[ 0 ], s_list[ 1 ], s_list[ 2 ], s_list[ 3 ],s_list[ 4 ],s_list[ 5 ],
                    s_list[ 6 ], s_list[ 7 ], probabilityofchild };
            return ( proconv / formula( tmp_array ));
        }
        return 0;
    }

    public static void main(String args[])
    {
        demograhicLikelihood obj = new demograhicLikelihood();
        double res = obj.evaluate(8,2,"0.111","0.222","0.111~0.222~0.333~0.444~0.555~0.666~0.777~");
        System.out.println(res);
    }
}
package org.anvard.introtojava.jdbc;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;
import org.anvard.introtojava.Person;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class PersonDaoTest {
    @Mock
    private DataSource ds;
    @Mock
    private Connection c;
    @Mock
    private PreparedStatement stmt;
    @Mock
    private ResultSet rs;
    private Person p;
    @Before
    public void setUp() throws Exception {
        assertNotNull(ds);
        when(c.prepareStatement(any(String.class))).thenReturn(stmt);
        when(ds.getConnection()).thenReturn(c);
        p = new Person();
        p.setId(1);
        p.setFirstName("Johannes");
        p.setLastName("Smythe");
        when(rs.first()).thenReturn(true);
        when(rs.getInt(1)).thenReturn(1);
        when(rs.getString(2)).thenReturn(p.getFirstName());
        when(rs.getString(3)).thenReturn(p.getLastName());
        when(stmt.executeQuery()).thenReturn(rs);
    }
    @Test(expected=IllegalArgumentException.class)
    public void nullCreateThrowsException() throws Exception {
        new PersonDao(ds).create(null);
    }
    @Test
    public void createPerson() throws Exception {
        new PersonDao(ds).create(p);
    }
    @Test
    public void createAndRetrievePerson() throws Exception {
        PersonDao dao = new PersonDao(ds);
        dao.create(p);
        Person r = dao.retrieve(1);
        assertEquals(p, r);
    }
}
package org.anvard.introtojava;

public class Person {
	int id;
	String firstName;
	String lastName;
	
	public void setId(int id) {
		this.id = id;
	}
	
	public int getId() {
		return this.id;
	}
	
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	public String getFirstName() {
		return this.firstName;
	}
	
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public String getLastName() {
		return this.lastName;
	}
}

以上是关于java Hive相关说明的主要内容,如果未能解决你的问题,请参考以下文章

Hive 之表的种类以及相关操作

大数据之Hive:hive函数讲解:行转列

Hive数仓项目架构说明环境搭建及数据仓库基础知识

Hive数仓项目之sqoop相关操作访问咨询主题看板_全量流程

hive 相关异常

Hive数仓项目之基本优化方案学生出勤主题看板分析