Giter Club home page Giter Club logo

Comments (11)

Asura7969 avatar Asura7969 commented on July 18, 2024 1

And I don't know the detail of ClickhouseUtils.java.
Could you run the sample codes in the src/main/java/examples directory?

object ClickhouseUtils {

  /**
    * Clickhouse 连接信息
    */
  def getCHConf:Properties={
    val clickhouseConf = new Properties()
    clickhouseConf.put("user","xxxxx")
    clickhouseConf.put("password","xxxx")
    clickhouseConf.put("database","xxx")
    clickhouseConf
  }

  /**
    * 初始化 insert SQL
    */
  def initPrepareSQL(userTableSchema: immutable.Seq[((String, String), Int)],tableName:String): String = {
    val prepare = List.fill(userTableSchema.size)("?")

    val fields = userTableSchema.map(_._1._1).mkString(",")
    val sql = String.format(
      "INSERT INTO %s (%s) VALUES (%s)",
      tableName,
      fields,
      prepare.mkString(","))
    sql
  }

  /**
    * 校验是否是数值
    */
  def isIntByRegex(s : String) = {
    val pattern = """^(\d+)$""".r
    s match {
      case pattern(_*) => true
      case _ => false
    }
  }

  /**
    * 插入数据
    */
  def newInsert(json: JSONObject,
                statement: PreparedStatement,
                userTableSchema: immutable.Seq[((String, String), Int)]): Unit ={

    userTableSchema.foreach(each => {
      val field = each._1._1
      val fieldType = each._1._2
      val i = each._2

      val value = json.get(field)
      fieldType match {
        case "String" =>
          if (null == value) statement.setString(i + 1, "null")
          else statement.setString(i + 1, json.getString(field))

        case "DateTime" =>
          if (null != value) statement.setTimestamp(i + 1,new Timestamp(value.toString.toLong))
          else statement.setTimestamp(i + 1,new Timestamp(System.currentTimeMillis()))

        case "Date" =>
          if (null != value) statement.setDate(i + 1,new Date(value.toString.toLong))
          else statement.setDate(i + 1,new Date(System.currentTimeMillis()))

        case "Int8" | "UInt8" | "Int16" | "UInt16" | "Int32" =>
          if (null == value) statement.setInt(i + 1, 0)
          else statement.setInt(i + 1, json.getIntValue(field))

        case "UInt32" | "UInt64" | "Int64" =>
          if (null != value && isIntByRegex(value.toString.replace(".",""))){
            statement.setLong(i + 1, json.getLongValue(field))
          } else {
            statement.setLong(i + 1, 0)
          }

        case "Float32" =>
          if (null != value && isIntByRegex(value.toString.replace(".",""))){
            statement.setFloat(i + 1, json.getFloatValue(field))
          } else {
            statement.setFloat(i + 1, 0)
          }

        case "Float64" =>
          if (null != value && isIntByRegex(value.toString.replace(".",""))){
            statement.setDouble(i + 1, json.getFloatValue(field))
          } else {
            statement.setDouble(i + 1, 0)
          }

        // TODO: 暂时不考虑Array类型
        // case arrayPattern(_) =>
        // statement.setArray(i + 1, item.getAs[WrappedArray[AnyRef]](field))
        case _ =>
      }
    })
    statement.addBatch()
  }
}

from clickhouse-native-jdbc.

sundy-li avatar sundy-li commented on July 18, 2024

Hi, which driver version did you use?
Could you show some code or show more detail description of usage?

from clickhouse-native-jdbc.

zhang2014 avatar zhang2014 commented on July 18, 2024

And please provide the version of the clickhouse server version

from clickhouse-native-jdbc.

Asura7969 avatar Asura7969 commented on July 18, 2024

Hi, which driver version did you use?
Could you show some code or show more detail description of usage?

sparkstreaming job:duration = 15s

clickhouse version:19.4.1

<dependency>
      <groupId>com.github.housepower</groupId>
      <artifactId>clickhouse-native-jdbc</artifactId>
      <version>1.6-stable</version>
</dependency>
soaStream.foreachRDD(rdd =>{

      rdd.foreachPartition(it => {

        InternalRedisClient.build(redisConfig)
        val jedis = InternalRedisClient.getPool.getResource
        val userTableSchema =
          InternalRedisClient.getTableSchema(jedis, redisETLkey).asScala.toList.zipWithIndex.sortBy(_._2)
        val cf = new ClickHouseConfig("jdbc:clickhouse://xxxx:9000/database", chConfig.value)
        val conn = ClickHouseConnection.createClickHouseConnection(cf)
        val insertSQL = ClickhouseUtils.initPrepareSQL(userTableSchema,clickhouseConfig.getProperty("tableName"))

        val pstmt = conn.prepareStatement(insertSQL)
        // bulkSize = 90000
        val bulkSize = clickhouseConfig.getProperty("insertBulkSize").toInt
        var length = 0
        val keys = userTableSchema.toMap.keys.map(_._1).toList.asJava
        while (it.hasNext) {
          length += 1
          val json = NewLogScan.scan(it.next().value(), keys)
          if (null != json){
            ClickhouseUtils.newInsert(json,pstmt,userTableSchema)
          }
          if (length >= bulkSize) {
            pstmt.executeBatch()
            length = 0
          }
        }
        pstmt.executeBatch()
        conn.close()
      })
    })

And please provide the version of the clickhouse server version

from clickhouse-native-jdbc.

sundy-li avatar sundy-li commented on July 18, 2024

For the second
pstmt.executeBatch()

maybe better to be

if (length > 0) {
    pstmt.executeBatch()
}

I don't know if this is the problem, just test for v19.5.1 and that's ok.

from clickhouse-native-jdbc.

sundy-li avatar sundy-li commented on July 18, 2024

And I don't know the detail of ClickhouseUtils.java.
Could you run the sample codes in the src/main/java/examples directory?

from clickhouse-native-jdbc.

BernieJiangDL avatar BernieJiangDL commented on July 18, 2024

the same thing happened here. I've tried to use spark dataframe jdbc to write down some items, but got the "java.io.EOFException: Attempt to read after eof."

The code I am using is like:
df.write
.mode(saveMode)
.format("jdbc")
.option("driver", "com.github.housepower.jdbc.ClickHouseDriver")
.option("url", "jdbc:clickhouse:/xxxxxxxx")
.save()

from clickhouse-native-jdbc.

Asura7969 avatar Asura7969 commented on July 18, 2024

from clickhouse-native-jdbc.

BernieJiangDL avatar BernieJiangDL commented on July 18, 2024

from clickhouse-native-jdbc.

pan3793 avatar pan3793 commented on July 18, 2024

I've tried yandex/clickhouse-jdbc dependency at the very first time. However I ran into some issues : one of the dependency of yandex/clickhouse-jdbc named jackson-databind's version is banned because of security reasons. How do you use it? Could you please show your demo and pom? I really appriciate it.

Generally speaking, using shade and relocate to avoid such dependency conflicts.

If use clickhouse-native-jdbc, paste below code segment in pom.xml. See more details at #156

<!-- (recommended) shaded version, available since 2.3-stable -->
<dependency>
    <groupId>com.github.housepower</groupId>
    <artifactId>clickhouse-native-jdbc-shaded</artifactId>
    <version>2.3-stable</version>
</dependency>

If use yandex/clickhouse-jdbc, paste below code segment in pom.xml

<dependency>
   <groupId>ru.yandex.clickhouse</groupId>
   <artifactId>clickhouse-jdbc</artifactId>
   <classifier>shaded</classifier>
   <version>${yandex-clickhouse-jdbc.version}</version>
   <exclusions>
      <exclusion>
         <groupId>*</groupId>
         <artifactId>*</artifactId>
      </exclusion>
   </exclusions>
</dependency>

from clickhouse-native-jdbc.

pan3793 avatar pan3793 commented on July 18, 2024

Close by e795d27

from clickhouse-native-jdbc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.