• flink jdbc方式下沉到hive


    import java.sql.{Connection, DriverManager, PreparedStatement}
    
    import it.bigdata.flink.study.SensorReding
    import org.apache.flink.configuration.Configuration
    import org.apache.flink.streaming.api.functions.sink.RichSinkFunction
    import org.apache.flink.streaming.api.scala._
    
    object HiveJdbcSink {
      def main(args: Array[String]): Unit = {
        //创建环境
        val env = StreamExecutionEnvironment.getExecutionEnvironment
        env.setParallelism(1)
    
        //读取数据
        val inputPath="D:\ideaDemo\maven_flink\src\main\resources\sensor.txt"
        val inputStream = env.readTextFile(inputPath)
    
        //简单转换
        val dataStream = inputStream.map(data => {
          var arr = data.split(",")
          SensorReding(arr(0), arr(1).toLong, arr(1).toDouble)
        })
    
        dataStream.addSink(new MyJdbcHiveSinkFunc())
    
        env.execute("JdbcHive sink test")
      }
    
    }
    
    
    class MyJdbcHiveSinkFunc() extends RichSinkFunction[SensorReding]{
      //定义连接、预编译语句
    
      var conn:Connection =_
      var insetstmt:PreparedStatement =_
    
      override def open(parameters: Configuration): Unit = {
        Class.forName("org.apache.hive.jdbc.HiveDriver")
        conn=DriverManager.getConnection("jdbc:hive2://192.168.10.20:10000/hive","root","")
        insetstmt = conn.prepareStatement("insert into tb_tmp_001(id,x) values(?,?)")
      }
    
      override def invoke(value: SensorReding): Unit = {
        //插入数据
        insetstmt.setString(1,value.id)
        insetstmt.setString(2,value.temperature.toString)
        insetstmt.execute()
      }
      override def close(): Unit = {
        insetstmt.close()
        conn.close()
      }
    }

    需引入依赖

     <!--start   hive相关依赖-->
            <dependency>
                <groupId>org.apache.hive</groupId>
                <artifactId>hive-jdbc</artifactId>
                <version>1.1.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-common</artifactId>
                <version>2.6.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hive</groupId>
                <artifactId>hive-exec</artifactId>
                <version>3.1.2</version>
            </dependency>
            <!--end   hive相关依赖-->
    

      

    author@nohert
  • 相关阅读:
    linux 环境下安装oracle11g方法及安装过程中遇上的问题解决方法
    Opencv Mat的操作
    Opencv 的数据结构
    Opencv 摄像头矫正
    LM算法
    Python 正则表达式
    find grep
    Socket 入门
    Python thread
    Javascript实现页面跳转的几种方式
  • 原文地址:https://www.cnblogs.com/gzgBlog/p/14928275.html
Copyright © 2020-2023  润新知