Comments (11)
And I don't know the detail of
ClickhouseUtils.java
.
Could you run the sample codes in thesrc/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.
Hi, which driver version did you use?
Could you show some code or show more detail description of usage?
from clickhouse-native-jdbc.
And please provide the version of the clickhouse server version
from clickhouse-native-jdbc.
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.
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.
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.
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.
from clickhouse-native-jdbc.
from clickhouse-native-jdbc.
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.
Close by e795d27
from clickhouse-native-jdbc.
Related Issues (20)
- Was it possible to support two dimensional array?
- Unknown data type: IPv6 HOT 1
- "address.getHostName()" blocking 10+ seconds HOT 1
- 【bug】Connection is currently waiting for an insert operation, check your previous InsertStatement.
- 多线程写入数据的时候会频繁触发GC
- java.nio.BufferOverflowException native jdbc version 2.6.4.
- Looking for new maintainers HOT 1
- The IP address of the clickhouse is in the IPv6 format, but the house power does not support the IPv6 format. HOT 3
- When writing 1 million data per second, frequent GC is triggered in CompressedBuffedWriter.
- sql statement generate: how to use clickhouse function in where condition HOT 1
- Deleting / Updating rows from click house table using python or pyspark HOT 1
- Support LowCardinality series datatype in getMetadata().getColumns() function HOT 1
- Why can't I return error information such as incorrect passwords? HOT 1
- Can not support Array(Tuple(name String, previous_value String, new_value String, reason String)) type
- Unknown compression magic code: 105 HOT 2
- How to insert the Array(Float32) data type
- It is hoped that the executeBatch() method within the Statement class will be supported.
- update sql not support LocalDateTime after 2.5.7
- Is there a way to write to an EPHEMERAL field?
- ClickHouse-Native-JDBC has a vulnerability that depends passively on aircompressor. Check whether the vulnerability involves ClickHouse ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clickhouse-native-jdbc.