2019-08-10 14:30:23 +00:00
local driver = require " luaconnector "
2019-08-04 13:32:49 +00:00
2020-12-07 07:04:10 +00:00
local config = {
host = " 127.0.0.1 " ,
port = 6030 ,
database = " " ,
user = " root " ,
password = " taosdata " ,
max_packet_size = 1024 * 1024
}
2019-08-04 13:32:49 +00:00
2020-12-07 07:04:10 +00:00
local conn
local res = driver.connect ( config )
2019-08-04 13:32:49 +00:00
if res.code ~= 0 then
2020-12-07 07:04:10 +00:00
print ( " connect--- failed: " .. res.error )
2019-08-04 13:32:49 +00:00
return
else
conn = res.conn
2020-12-07 07:04:10 +00:00
print ( " connect--- pass. " )
2019-08-04 13:32:49 +00:00
end
2020-08-30 04:09:37 +00:00
local res = driver.query ( conn , " drop database if exists demo " )
2019-08-04 13:32:49 +00:00
2019-08-10 14:30:23 +00:00
res = driver.query ( conn , " create database demo " )
2019-08-04 13:32:49 +00:00
if res.code ~= 0 then
2020-12-07 07:04:10 +00:00
print ( " create db--- failed: " .. res.error )
2019-08-04 13:32:49 +00:00
return
2020-12-07 07:04:10 +00:00
else
print ( " create db--- pass. " )
2019-08-04 13:32:49 +00:00
end
2019-08-10 14:30:23 +00:00
res = driver.query ( conn , " use demo " )
2019-08-04 13:32:49 +00:00
if res.code ~= 0 then
2020-12-07 07:04:10 +00:00
print ( " select db--- failed: " .. res.error )
2019-08-04 13:32:49 +00:00
return
2020-12-07 07:04:10 +00:00
else
print ( " select db--- pass. " )
2019-08-04 13:32:49 +00:00
end
2019-08-10 14:30:23 +00:00
res = driver.query ( conn , " create table m1 (ts timestamp, speed int,owner binary(20)) " )
2019-08-04 13:32:49 +00:00
if res.code ~= 0 then
2020-12-07 07:04:10 +00:00
print ( " create table---failed: " .. res.error )
2019-08-04 13:32:49 +00:00
return
2020-12-07 07:04:10 +00:00
else
print ( " create table--- pass. " )
2019-08-04 13:32:49 +00:00
end
2019-11-12 02:40:44 +00:00
res = driver.query ( conn , " insert into m1 values ('2019-09-01 00:00:00.001',0,'robotspace'), ('2019-09-01 00:00:00.002',1,'Hilink'),('2019-09-01 00:00:00.003',2,'Harmony') " )
2019-08-10 14:30:23 +00:00
if res.code ~= 0 then
2020-12-07 07:04:10 +00:00
print ( " insert records failed: " .. res.error )
2019-08-10 14:30:23 +00:00
return
2019-11-12 02:40:44 +00:00
else
2020-12-07 07:04:10 +00:00
if ( res.affected == 3 ) then
print ( " insert records--- pass " )
else
print ( " insert records---failed: expect 3 affected records, actually affected " .. res.affected )
end
2019-08-10 14:30:23 +00:00
end
2019-08-04 13:32:49 +00:00
2019-08-10 14:30:23 +00:00
res = driver.query ( conn , " select * from m1 " )
2019-08-04 13:32:49 +00:00
2019-08-10 14:30:23 +00:00
if res.code ~= 0 then
2020-12-07 07:04:10 +00:00
print ( " select failed: " .. res.error )
2019-08-10 14:30:23 +00:00
return
else
2020-12-07 07:04:10 +00:00
if ( # ( res.item ) == 3 ) then
print ( " select--- pass " )
else
print ( " select--- failed: expect 3 affected records, actually received " .. # ( res.item ) )
end
2019-08-04 13:32:49 +00:00
end
2019-08-10 14:30:23 +00:00
2019-11-12 02:40:44 +00:00
res = driver.query ( conn , " CREATE TABLE thermometer (ts timestamp, degree double) TAGS(location binary(20), type int) " )
if res.code ~= 0 then
print ( res.error )
return
2020-12-07 07:04:10 +00:00
else
print ( " create super table--- pass " )
2019-11-12 02:40:44 +00:00
end
res = driver.query ( conn , " CREATE TABLE therm1 USING thermometer TAGS ('beijing', 1) " )
if res.code ~= 0 then
print ( res.error )
return
2020-12-07 07:04:10 +00:00
else
print ( " create table--- pass " )
2019-11-12 02:40:44 +00:00
end
2020-12-07 07:04:10 +00:00
2019-11-12 02:40:44 +00:00
res = driver.query ( conn , " INSERT INTO therm1 VALUES ('2019-09-01 00:00:00.001', 20),('2019-09-01 00:00:00.002', 21) " )
if res.code ~= 0 then
print ( res.error )
return
else
2020-12-07 07:04:10 +00:00
if ( res.affected == 2 ) then
print ( " insert records--- pass " )
else
print ( " insert records---failed: expect 2 affected records, actually affected " .. res.affected )
end
2019-11-12 02:40:44 +00:00
end
res = driver.query ( conn , " SELECT COUNT(*) count, AVG(degree) AS av, MAX(degree), MIN(degree) FROM thermometer WHERE location='beijing' or location='tianjin' GROUP BY location, type " )
if res.code ~= 0 then
2020-12-07 07:04:10 +00:00
print ( " select from super table--- failed: " .. res.error )
2019-11-12 02:40:44 +00:00
return
else
2020-12-07 07:04:10 +00:00
print ( " select from super table--- pass " )
2019-11-12 02:40:44 +00:00
for i = 1 , # ( res.item ) do
print ( " res: " .. res.item [ i ] . count )
end
end
function callback ( t )
2020-12-07 07:04:10 +00:00
print ( " ------------------------ " )
2019-11-12 02:40:44 +00:00
print ( " continuous query result: " )
for key , value in pairs ( t ) do
print ( " key: " .. key .. " , value: " .. value )
end
end
local stream
res = driver.open_stream ( conn , " SELECT COUNT(*) as count, AVG(degree) as avg, MAX(degree) as max, MIN(degree) as min FROM thermometer interval(2s) sliding(2s);) " , 0 , callback )
if res.code ~= 0 then
2020-12-07 07:04:10 +00:00
print ( " open stream--- failed: " .. res.error )
2019-11-12 02:40:44 +00:00
return
else
2020-12-07 07:04:10 +00:00
print ( " open stream--- pass " )
2019-11-12 02:40:44 +00:00
stream = res.stream
end
2020-12-07 07:04:10 +00:00
print ( " From now on we start continous insert in an definite (infinite if you want) loop. " )
2019-11-12 02:40:44 +00:00
local loop_index = 0
2020-12-07 07:04:10 +00:00
while loop_index < 30 do
2019-11-12 02:40:44 +00:00
local t = os.time ( ) * 1000
local v = loop_index
res = driver.query ( conn , string.format ( " INSERT INTO therm1 VALUES (%d, %d) " , t , v ) )
if res.code ~= 0 then
2020-12-07 07:04:10 +00:00
print ( " continous insertion--- failed: " .. res.error )
2019-11-12 02:40:44 +00:00
return
else
2020-12-07 07:04:10 +00:00
--print("insert successfully, affected:"..res.affected)
2019-11-12 02:40:44 +00:00
end
os.execute ( " sleep " .. 1 )
loop_index = loop_index + 1
end
driver.close_stream ( stream )
2019-08-10 14:30:23 +00:00
driver.close ( conn )