maven
- mysql-connector==>java MySql连接
- mybatis==>MyBatis包
- druid==>阿里jdbc连接池
- hive-jdbc==>hive连接
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.1.0</version>
</dependency>
mybatis连接hive和mysql 测试
1.创建druid连接池
//读配置文件拿出连接
public class DruidDataSourceFactory implements DataSourceFactory {
private Properties prop;
@Override
public void setProperties(Properties properties) {
this.prop = properties;
}
@Override
public DataSource getDataSource() {
//连接池的对象
DruidDataSource druid = new DruidDataSource();
druid.setDriverClassName(this.prop.getProperty("driver"));
druid.setUrl(this.prop.getProperty("url"));
druid.setUsername(this.prop.getProperty("username"));
druid.setPassword(this.prop.getProperty("password"));
//最大激活连接数
// druid.setMaxActive(Integer.parseInt(this.prop.getProperty("maxactive")));
//初始化连接个数
// druid.setInitialSize(Integer.parseInt(this.prop.getProperty("initialsize")));
try {
druid.init();
} catch (SQLException e) {
e.printStackTrace();
}
return druid;
}
}
2.数据库工具
//通过连接源创建SqlSession
public class DatabaseUtils {
private static final String configPath = "mybatis-config.xml";
public static SqlSession getSession(String db) {
SqlSession session = null;
try {
InputStream is = Resources.getResourceAsStream(configPath);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is,db.equals("mysql")?"sq":"sq1");
session = factory.openSession();
} catch (IOException e) {
e.printStackTrace();
}
return session;
}
public static void closeSession(SqlSession session) {
session.close();
}
}
3.创建实体类
package org.entry;
//实体类
public class Events {
private String eventid;
private String userid;
private String starttime;
private String city;
private String states;
private String zip;
private String country;
private String lat;
private String lng;
private String features;
}
4.创建Dao
public interface MySqlEventDAO {
List<Events> findAll();
}
public interface HiveEventDAO {
public List<Events> findAll();
}
5.mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<!-- 别名-->
<typeAlias type="org.example.DruidDataSourceFactory" alias="DRUID"></typeAlias>
<typeAlias type="org.entry.Events" alias="event"></typeAlias>
</typeAliases>
<environments default="sq">
<environment id="sq">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="DRUID">
<!--名字要与druidDataSourceFactory中的配置一致-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://single:3306/ms_dm_intes"/>
<property name="username" value="root"/>
<property name="password" value="kb10"/>
</dataSource>
</environment>
<!--连接hive-->
<environment id="sq1">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="DRUID">
<property name="driver" value="org.apache.hive.jdbc.HiveDriver"/>
<property name="url" value="jdbc:hive2://single:10000/dwd_intes"/>
<property name="username" value=""/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<!-- 映射文件-->
<mappers>
<mapper resource="mysql-events.xml"></mapper>
<mapper resource="hive-events.xml"></mapper>
</mappers>
</configuration>
6.mysql-events.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.dao.MySqlEventDAO">
<select id="findAll" resultType="event">
select * from dm_events limit 3
</select>
</mapper>
7.hive-events.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.dao.HiveEventDAO">
<select id="findAll" resultType="event">
select eventid,userid,starttime,city,states,zip,country,lat,lng,features from dwd_intes.dwd_events limit 3
</select>
</mapper>
8.测试类
public class App {
public static void main(String[] args) {
SqlSession session = DatabaseUtils.getSession("mysql");
MySqlEventDAO edao = session.getMapper(MySqlEventDAO.class);
System.out.println(edao.findAll());
SqlSession session1 = DatabaseUtils.getSession("hive");
HiveEventDAO edao1 = session1.getMapper(HiveEventDAO.class);
System.out.println(edao1.findAll());
}
}
将数据从Hive导入MySql
YM