项目作者: lizhichao

项目描述 :
php tcp client for clickhouse
高级语言: PHP
项目地址: git://github.com/lizhichao/one-ck.git
创建时间: 2020-09-04T03:11:51Z
项目社区:https://github.com/lizhichao/one-ck

开源协议:Apache License 2.0

下载


一个php实现的clickhouse tcp协议客户端 | php tcp client for clickhouse

  • 支持流式写入 Support streaming writing
  • 超高的性能,支持1000w/s的写入速度 Super high performance, Support 1000w/s writing speed
  • tcp对服务器对服务器友好,压力小 tcp is server-friendly, with little pressure
  • 零依赖 Zero dependence

安装 | install

composer require lizhichao/one-ck

php5.6 - php7.1 不支持 Decimal128 ,请使用 0.1.x 版本
php7.2 >= 推荐使用 0.2.x版本
clickhouse QQ交流群 970435787

支持的数据类型 | Supported types

  • UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64
  • Float32, Float64
  • Decimal(P, S), Decimal32(S), Decimal64(S), Decimal128(S)
  • String
  • Nothing
  • FixedString(N)
  • UUID
  • Date
  • Datetime
  • Datetime64
  • Nullable(T)
  • IPv4
  • IPv6
  • Array(T)
  • SimpleAggregateFunction(F, T)
  • Tuple(T)
  • Int128,Int256,UInt128,UInt256,Decimal256(S)
  • Bool
  • JSON
  • Enum (can use LowCardinality instead)

Array of client options

It is also possible to create the client with configuration options:

  1. use OneCk\Client;
  2. $client = new Client(
  3. 'tcp://localhost:9000',
  4. 'username',
  5. 'password',
  6. 'database',
  7. [
  8. 'connect_timeout' => 3,
  9. 'socket_timeout' => 30,
  10. 'tcp_nodelay' => true,
  11. 'persistent' => true,
  12. ]
  13. );

The following options are available:

Name Type Default Description
connect_timeout float 3.0 The number of seconds that the client waits for a connect to a ClickHouse server before throwing a CkException exception.
socket_timeout float 30.0 The number of seconds that the client waits for a respond from a ClickHouse server before throwing a CkException exception.
tcp_nodelay boolean false Whether the Nagle algorithm is disabled on a TCP connection.
persistent boolean false Whether to use a persistent connection.

使用例子 | Demo

  1. use OneCk\Client;
  2. use OneCk\Types;
  3. //default
  4. //$ck = new Client('tcp://127.0.0.1:9000');
  5. $t1 = microtime(true);
  6. $ck = new Client('tcp://192.168.31.216:9091', 'default', '123456', 'test1');
  7. $data['server info'] = $ck->getServerInfo();
  8. $data['drop table'] = $ck->query('DROP TABLE IF EXISTS t6');
  9. $table = [
  10. 'CREATE TABLE t6 (',
  11. '`id` UInt32,',
  12. '`f1` Int32,',
  13. '`f2` Nullable(Int32),',
  14. '`f3` UInt8,',
  15. '`f4` Nullable(UInt8),',
  16. '`f5` UInt16,',
  17. '`f6` UInt64,',
  18. '`f7` Int64,',
  19. '`f8` Float32,',
  20. '`f9` Float64,',
  21. '`f10` Nullable(Float64),',
  22. '`f11` Decimal32(3),',
  23. '`f12` Decimal64(5),',
  24. '`f13` Decimal128(7),',
  25. '`f14` Nullable(Decimal128(7)),',
  26. '`f15` String,',
  27. '`f16` Nullable(String),',
  28. '`f17` FixedString(32),',
  29. '`f18` UUID,',
  30. '`f19` Date,',
  31. '`f20` Nullable(Date),',
  32. '`f21` Datetime,',
  33. '`f22` Datetime64(3),',
  34. '`f23` IPv4,',
  35. '`f24` Nullable(IPv4),',
  36. '`f25` IPv6,',
  37. '`f26` LowCardinality(String),',
  38. '`f27` Array(Int32),',
  39. '`f28` Array(Array(Array(Nullable(Date)))),',
  40. '`f29` Array(Array(Array(Array(Array(Nullable(Datetime))))))',
  41. ') ENGINE = MergeTree() ORDER BY id SETTINGS index_granularity = 8192'
  42. ];
  43. $data['create table'] = $ck->query(implode("\n", $table));
  44. $data['insert data'] = $ck->insert('t6', [
  45. [
  46. 'id' => 1,
  47. 'f1' => -3,
  48. 'f2' => null,
  49. 'f3' => 127,
  50. 'f4' => null,
  51. 'f5' => 3322,
  52. 'f6' => 1844674407370955161,
  53. 'f7' => 9223372036854775807,
  54. 'f8' => -2132121.5,
  55. 'f9' => 6546546544665.66658,
  56. 'f10' => null,
  57. 'f11' => 552.339,
  58. 'f12' => 3658.6954,
  59. 'f13' => '170141183460469231168730371588.4105721',
  60. 'f14' => null,
  61. 'f15' => 'emoji😀😁😂😃😄',
  62. 'f16' => null,
  63. 'f17' => md5('a'),
  64. 'f18' => '016e64be-605f-4108-8a67-495d74d7ef3c',
  65. 'f19' => '2020-09-05',
  66. 'f20' => null,
  67. 'f21' => '2020-09-05 14:25:12',
  68. 'f22' => '2020-09-05 14:25:12.258',
  69. 'f23' => '192.168.1.1',
  70. 'f24' => null,
  71. 'f25' => 'CDCD:910A:2222:5498:8475:1111:3900:2020',
  72. 'f26' => 'eee',
  73. 'f27' => [0, -2, 3, 4, 5, 6, 7, 8, 64],
  74. 'f28' => [[['2020-01-05', null, '2020-01-06']], [['2020-01-07'], ['2020-01-08']], [['2020-01-09']]],
  75. 'f29' => [[[[["2020-01-05 05:05:05", null, "2020-01-06 15:16:17"]], [["2020-01-07 18:19:20"], ["2020-01-08 21:22:23"]], [["2020-01-09 00:00:00"]]], [[["2020-01-10 01:00:00", null]]]], [[[["2020-01-11 00:00:01", null, "2020-01-12 11:01:58"]], [["2020-01-13 21:22:01"]]]]]
  76. ],
  77. [
  78. 'id' => 2,
  79. 'f1' => 3,
  80. 'f2' => 3,
  81. 'f3' => 3,
  82. 'f4' => 3,
  83. 'f5' => 3,
  84. 'f6' => 3,
  85. 'f7' => 3,
  86. 'f8' => 3,
  87. 'f9' => 3,
  88. 'f10' => 3,
  89. 'f11' => -552.339,
  90. 'f12' => -3658.6954,
  91. 'f13' => '-170141183460469231168730371588.4105721',
  92. 'f14' => 3,
  93. 'f15' => str_repeat(md5('aa'), '6'),
  94. 'f16' => '',
  95. 'f17' => md5('55'),
  96. 'f18' => md5('55'),
  97. 'f19' => '2020-09-06',
  98. 'f20' => '2020-09-06',
  99. 'f21' => '2020-09-06 14:25:12',
  100. 'f22' => '2020-09-06 14:25:12.258',
  101. 'f23' => '251.222.221.231',
  102. 'f24' => '192.168.1.2',
  103. 'f25' => '1030::C9B4:FF12:48AA:1A2B',
  104. 'f26' => 'eee22',
  105. 'f27' => [1, 2, 3, 4],
  106. 'f28' => [[['2020-01-05', '2020-01-06']], [['2020-01-07', null], ['2020-01-08']], [['2020-01-09']]],
  107. 'f29' => [[[[[null]]]]]
  108. ],
  109. [
  110. 'id' => 3,
  111. 'f1' => -1,
  112. 'f2' => 3,
  113. 'f3' => 3,
  114. 'f4' => 3,
  115. 'f5' => 3,
  116. 'f6' => 3,
  117. 'f7' => 3,
  118. 'f8' => 3,
  119. 'f9' => 3,
  120. 'f10' => 3,
  121. 'f11' => 3,
  122. 'f12' => 3,
  123. 'f13' => 3,
  124. 'f14' => 3,
  125. 'f15' => 'aaa',
  126. 'f16' => 'aaa',
  127. 'f17' => md5('a'),
  128. 'f18' => '3026ee79-ac2a-46d2-882d-959a55d71025',
  129. 'f19' => '2020-09-07',
  130. 'f20' => '2020-09-07',
  131. 'f21' => '2020-09-07 14:25:12',
  132. 'f22' => '2020-09-07 14:25:12.258',
  133. 'f23' => '192.168.1.1',
  134. 'f24' => null,
  135. 'f25' => '2001:DB8:2de::e13',
  136. 'f26' => 'eee22',
  137. 'f27' => [12344],
  138. 'f28' => [[['2020-01-05', '2020-01-06'], [null]], [['2020-01-07'], ['2020-01-08']], [['2020-01-09']]],
  139. 'f29' => [[[[['2018-01-25 11:25:14']]]]]
  140. ]
  141. ]);
  142. //$data['struct'] = $ck->query('desc t6');
  143. $data['select t6'] = $ck->query('select * from t6');
  144. $data['select t6 int64'] = $ck->query("select id,f6 from t6 where f6=1844674407370955161");
  145. $data['select t6 Decimal32'] = $ck->query("select id,f11 from t6 where f11='552.339'");
  146. $data['select t6 Decimal64'] = $ck->query("select id,f12 from t6 where f12='-3658.69540'");
  147. $data['select t6 Decimal128'] = $ck->query("select id,f13 from t6 where f13='170141183460469231168730371588.4105721'");
  148. $data['select t6 uuid'] = $ck->query("select id,f18 from t6 where f18='3026ee79-ac2a-46d2-882d-959a55d71025'");
  149. $data['select t6 date'] = $ck->query("select id,f19 from t6 where f19='2020-09-05'");
  150. $data['select t6 datetime'] = $ck->query("select id,f21 from t6 where f21='2020-09-07 20:25:12'");
  151. $data['select t6 datetime64'] = $ck->query("select id,f22 from t6 where f22='2020-09-06 20:25:12.258'");
  152. $data['select t6 ip'] = $ck->query("select id,f23,f25 from t6 where f23=" . Types::encodeIpv4('192.168.1.1'));
  153. $data['select t6 ip64'] = $ck->query("select id,f23,f25 from t6 where f25='" . Types::encodeIpv6('1030::c9b4:ff12:48aa:1a2b') . "'");
  154. $data['nothing'] = $ck->query('select array()');
  155. // flow of write
  156. $data['drop table'] = $ck->query('DROP TABLE IF EXISTS t7');
  157. $table = [
  158. 'CREATE TABLE t7 (',
  159. '`id` UInt32,',
  160. '`f2` Nullable(Int32),',
  161. '`f5` UInt16,',
  162. '`f15` String',
  163. ') ENGINE = MergeTree() ORDER BY id SETTINGS index_granularity = 8192'
  164. ];
  165. $data['create table'] = $ck->query(implode("\n", $table));
  166. $ck->writeStart('t7',['id','f2','f5','f15']);
  167. for ($i = 0; $i < 100; $i++) {
  168. $da = [];
  169. for ($j = 0; $j < 1000; $j++) {
  170. $da[] = [
  171. 'id' => mt_rand(1,1000000),
  172. 'f2' => mt_rand(-1000000,1000000),
  173. 'f5' => mt_rand(1,10000),
  174. 'f15' => md5(mt_rand(1,10000))
  175. ];
  176. }
  177. $ck->writeBlock($da);
  178. }
  179. $ck->writeEnd();
  180. $data['write 10w rows time'] = microtime(true) - $t1;
  181. echo json_encode($data);
  182. //Close connection
  183. unset($ck);

我的其他仓库