zoukankan      html  css  js  c++  java
  • Flink实例(123):connector(二十)clickhouse 写 入 (三)

    package com.caimou.utils;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * Created by Administrator on 2020/2/19.
     */
    public class ClickHouseUtils {
    
        public static void insertData(String tableName,Map<String,String> data,Set<String> intFieldSet){
            /**
             * insert into youfantest (id,name,create_date)values(1,'xiaobai','2018-09-07');
             */
            String resultsql = "insert into ";
            resultsql += tableName +" (";
            String valuesql = "(";
            Set<Map.Entry<String,String>> sets =  data.entrySet();
            for(Map.Entry<String,String> map:sets){
                String fieldName = map.getKey();
                String valuestring = map.getValue();
                resultsql += fieldName  +",";
                if(!intFieldSet.contains(fieldName)){
                    valuesql += "'"+valuestring + "'" +",";
                }else {
                    valuesql += valuestring  +",";
                }
    
            }
            resultsql = resultsql.substring(0,resultsql.length()-1)+")";
            valuesql = valuesql.substring(0,valuesql.length()-1)+")";
            resultsql = resultsql + " values "+ valuesql;
            System.out.println(resultsql);
            try {
                Connection connection = getConnection("jdbc:clickhouse://192.168.246.152:8123/youfands","ru.yandex.clickhouse.ClickHouseDriver");
                Statement statement = connection.createStatement();
                statement.execute(resultsql);//执行sql语句
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        private static Connection getConnection(String addressParam,String driverClassNameParam) throws Exception {
            String address = addressParam;
            Class.forName(driverClassNameParam);
            Connection connection  = DriverManager.getConnection(address);
            return connection;
        }
    
        public static ResultSet getQueryResult(String database,String sql) throws Exception {
            Connection connection = getConnection("jdbc:clickhouse://192.168.246.152:8123/"+database,"ru.yandex.clickhouse.ClickHouseDriver");
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            return resultSet;
        }
    
        public static void main(String[] args) {
            Map<String,String> data = new HashMap<String,String>();
            Set<String> intFieldSet = new HashSet<String>();
            intFieldSet.add("id");
            data.put("id","111");
            data.put("name","xiaobai");
            data.put("create_date","2018-09-07");
            insertData("youfantest",data,intFieldSet);
        }
    }
  • 相关阅读:
    树状数组和线段树
    N皇后问题(函数式编程与过程式)
    单例模式
    BitSet
    蓄水池抽样问题
    关于动态规划的一些感想
    53最大子序和
    5最长回文子串
    139单词拆分
    91.解码方法
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/14352308.html
Copyright © 2011-2022 走看看