首页 > 其他分享 >大数据总结

大数据总结

时间:2023-08-26 17:22:41浏览次数:35  
标签:总结 boot hive springframework datasource import org 数据

这周四开始试写java web,其中数据库换成了hive,我用的springbootl来集成的,简单的增查可以实现,接下来去写完整逻辑的web网站。

hive整个服务启动流程

复制代码
首先是
start-all.sh
然后打开历史服务器:
mapred --daemon start historyserver
最后首先启动metastore服务,然后启动hiveserver2服务
nohup bin/hive --service metastore >> logs/metastore.log 2>&1 &
nohup bin/hive --service hiveserver2 >> logs/hiveserver2.log 2>&1 &
复制代码

spingboot集成hive小demo

首先呢就是pom文件

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.14</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>hive</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hive</name>
    <description>hive</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>

            <!-- 德鲁伊连接池依赖 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.2.5</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>

            <!-- 添加hive依赖 -->
            <dependency>
                <groupId>org.apache.hive</groupId>
                <artifactId>hive-jdbc</artifactId>
                <version>3.1.2</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.eclipse.jetty</groupId>
                        <artifactId>*</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.apache.hive</groupId>
                        <artifactId>hive-shims</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.glassfish</groupId>
                        <artifactId>javax-el</artifactId>
                    </exclusion>
                    <!--<exclusion>-->
                    <!--<groupId>org.slf4j</groupId>-->
                    <!--<artifactId>slf4j-log4j12</artifactId>-->
                    <!--</exclusion>-->
                    <exclusion>
                        <groupId>org.apache.logging.log4j</groupId>
                        <artifactId>log4j-slf4j-impl</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
复制代码

然后是yaml文件:

复制代码
hive:
  url: jdbc:hive2://node1:10000/db_msg(这个是你的数据库)
  driver-class-name: org.apache.hive.jdbc.HiveDriver
  type: com.alibaba.druid.pool.DruidDataSource
  user: hadoop
  password: 123456


  initialSize: 1
  minIdle: 3
  maxActive: 20

  maxWait: 60000

  timeBetweenEvictionRunsMillis: 60000

  minEvictableIdleTimeMillis: 30000
  validationQuery: select 1
  testWhileIdle: true
  testOnBorrow: false
  testOnReturn: false

  poolPreparedStatements: true
  maxPoolPreparedStatementPerConnectionSize: 20
复制代码

然后是config配置文件:

复制代码
package com.example.hive;
import javax.sql.DataSource;

import lombok.Data;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.pool.DruidDataSource;

@Data
@Configuration
@ConfigurationProperties(prefix = "hive")
public class HiveDruidConfig {

    private String url;
    private String user;
    private String password;
    private String driverClassName;
    private int initialSize;
    private int minIdle;
    private int maxActive;
    private int maxWait;
    private int timeBetweenEvictionRunsMillis;
    private int minEvictableIdleTimeMillis;
    private String validationQuery;
    private boolean testWhileIdle;
    private boolean testOnBorrow;
    private boolean testOnReturn;
    private boolean poolPreparedStatements;
    private int maxPoolPreparedStatementPerConnectionSize;

    @Bean(name = "hiveDruidDataSource")
    @Qualifier("hiveDruidDataSource")
    public DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(url);
        datasource.setUsername(user);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);

        // pool configuration
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        return datasource;
    }

    @Bean(name = "hiveDruidTemplate")
    public JdbcTemplate hiveDruidTemplate(@Qualifier("hiveDruidDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}
复制代码

测试:

复制代码
package com.example.hive;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 使用 DataSource 操作 Hive
 */
@RestController
public class HiveDataSourceController {

    private static final Logger logger = LoggerFactory.getLogger(HiveDataSourceController.class);

    @Autowired
    @Qualifier("hiveDruidDataSource")
    DataSource druidDataSource;

    /**
     * 测试spring boot是否正常启动
     */
    @RequestMapping("/")
    public String hello(){
        return "hello world";
    }

    /**
     * 列举当前Hive库中的所有数据表
     */
    @RequestMapping("/table/list")
    public List<String> listAllTables() throws SQLException {
        List<String> list = new ArrayList<String>();
        Statement statement = druidDataSource.getConnection().createStatement();
        String sql = "show tables";
        logger.info("Running: " + sql);
        ResultSet res = statement.executeQuery(sql);
        while (res.next()) {
            list.add(res.getString(1));
        }
        return list;
    }

    /**
     * 查询指定tableName表中的数据
     */
    @RequestMapping("/table/select")
    public List<String> selectFromTable(String tableName) throws SQLException {
        List<String> list = new ArrayList<String>();
        Statement statement = druidDataSource.getConnection().createStatement();
        String sql = "select * from " + tableName;
        logger.info("Running: " + sql);
        ResultSet res = statement.executeQuery(sql);
        int count = res.getMetaData().getColumnCount();
        String str = null;
        while (res.next()) {
            str = "";
            for (int i = 1; i < count; i++) {
                str += res.getString(i) + " ";
            }
            str += res.getString(count);
            logger.info(str);
            list.add(str);
        }
        return list;
    }

}
复制代码

 

 

标签:总结,boot,hive,springframework,datasource,import,org,数据
From: https://www.cnblogs.com/daitu66/p/17659125.html

相关文章

  • Nacos漏洞总结
    Nacos简介Nacos是阿里巴巴推出来的一个新开源项目,是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。致力于帮助发现、配置和管理微服务。Nacos提供了一组简单易用的特性集,可以快速实现动态服务发现、服务配置、服务元数据及流量管理。复现环境搭建本次复......
  • Lazada商品详情接口 获取Lazada商品详情数据 Lazada商品价格接口
     一、引言随着电子商务的迅速发展和普及,电商平台之间的竞争也日趋激烈。为了提供更好的用户体验和更高效的后端管理,Lazada作为东南亚最大的电商平台之一,开发了一种商品详情接口(ProductDetailAPI)。该接口允许第三方开发者通过API访问Lazada平台上的商品信息,包括商品详情、价......
  • 向es中导入数据的几个方式
    方式一,使用kibana控制台添加(该方式数据量有上限,批量导入推荐CURL)该方式需要安装kibana,启动后打开控制台http://kibana部署IP:5601/app/dev_tools#/consolePOST_bulk{"index":{"_index":"test_goods","_type":"goods","_id":10001}}{"code&q......
  • 【MySQL 8.0】通过pt-archiver实现表的历史数据归档
    (root@node02)>setgloballocal_infile=on;QueryOK,0rowsaffected(0.00sec)(root@node02)>createtablecustomer_jplikecustomer;QueryOK,0rowsaffected(0.20sec)(root@node01)>setgloballocal_infile=on;QueryOK,0rowsaffected......
  • 启动mysql数据库时报错unknown variable 'rpl_semi_sync_slave_enabled=1'
    问题描述:启动mysql数据库时报错unknownvariable'rpl_semi_sync_slave_enabled=1'.数据库:mysql5.7.21系统:rhel7.31、异常重现--启动数据库[mysql@mysql-leo-slavedata]$/usr/local/mysql/bin/mysqld_safe--defaults-file=/home/mysql/etc/my.cnf&--告警信息2023-08-......
  • R语言之数据框的合并
    文章和代码已经归档至【Github仓库:<https://github.com/timerring/dive-into-AI>】或者公众号【AIShareLab】回复R语言也可获取。有时数据集来自多个地方,我们需要将两个或多个数据集合并成一个数据集。合并数据框的操作包括纵向合并、横向合并和按照某个共有变量合并。1.纵向合......
  • 大厂算法每日总结(统计文件夹下的文件)
    //统计文件夹下的文件,是文件就累计1,隐藏文件空累计,文件不累计publicstaticvoidmain(String[]args){System.out.println(getFileNumber("D:\重要文件"));}publicstaticintgetFileNumber(StringfolderPath){Fileroot=newFile(folderPath);if(!root.isDirectory(......
  • 大厂算法题每日总结(num最近的,2的某次方)
    //给定一个非负整数num,不用循环,返回>=num,并离num最近的,2的某次方publicstaticfinalinttableSizeFor(intn){n--;n|=n>>>1;//>>>不带符号右移n|=n>>>2;n|=n>>>4;n|=n>>>8;n|=n>>>16;//打满,至此int32位,全打满return(n<0......
  • 大厂算法每日总结(GB字符串至少交换几次)
    //一个数组中只有两种字符'G'和'B',//想要所有的G都放左边,所有的B都放右边或者所有的B都放左边,所有的G都放右边//但只能在相邻字符之间进行交换操作//返回至少需要交换几次//方法1publicstaticintminSteps1(Strings){if(s==null||s.equals("")){return0;}......
  • 大厂算法题每日总结(绳子最大能盖的数组节点)
    //绳子最大能盖的数组节点publicstaticvoidmain(String[]args){int[]arr={1,4,7,9,60};System.out.println(maxPoint2(arr,50));}publicstaticintmaxPoint(int[]arr,intL){//L是绳子的长度 intres=1; for(inti=0;i<arr.length;i++){ intnearest=n......