test.lua 4.4 KB
Newer Older
1
local driver = require "luaconnector"
2

3 4 5 6 7 8 9 10
local config = {
   host = "127.0.0.1",
   port = 6030,
   database = "",
   user = "root",
   password = "taosdata",
   max_packet_size = 1024 * 1024 
}
11

12 13
local conn
local res = driver.connect(config)
14
if res.code ~=0 then
15
   print("connect--- failed: "..res.error)
16 17 18
   return
else
   conn = res.conn
19
   print("connect--- pass.")
20 21
end

R
robot 已提交
22
local res = driver.query(conn,"drop database if exists demo")
23

24
res = driver.query(conn,"create database demo")
25
if res.code ~=0 then
26
   print("create db--- failed: "..res.error)
27
   return
28 29
else
   print("create db--- pass.")
30 31
end

32
res = driver.query(conn,"use demo")
33
if res.code ~=0 then
34
   print("select db--- failed: "..res.error)
35
   return
36 37
else
   print("select db--- pass.")
38 39
end

40
res = driver.query(conn,"create table m1 (ts timestamp, speed int,owner binary(20))")
41
if res.code ~=0 then
42
   print("create table---failed: "..res.error)
43
   return
44 45
else
   print("create table--- pass.")
46 47
end

48
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')")
49
if res.code ~=0 then
50
   print("insert records failed: "..res.error)
51
   return
52
else
53 54 55 56 57
   if(res.affected == 3) then
      print("insert records--- pass")
   else
      print("insert records---failed: expect 3 affected records, actually affected "..res.affected)
   end   
58
end
59

60
res = driver.query(conn,"select * from m1")
61

62
if res.code ~=0 then
63
   print("select failed: "..res.error)
64 65
   return
else
66 67 68 69 70 71
    if (#(res.item) == 3) then
	print("select--- pass")
    else
	print("select--- failed: expect 3 affected records, actually received "..#(res.item))
    end

72
end
73

74 75 76 77
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
78 79
else
   print("create super table--- pass")
80 81 82 83 84
end
res = driver.query(conn,"CREATE TABLE therm1 USING thermometer TAGS ('beijing', 1)")
if res.code ~=0 then
   print(res.error)
   return
85 86
else
   print("create table--- pass")
87
end
88

89 90 91 92 93 94
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
95 96 97 98 99
   if(res.affected == 2) then
      print("insert records--- pass")
   else
      print("insert records---failed: expect 2 affected records, actually affected "..res.affected)
   end 
100 101 102 103
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
104
   print("select from super table--- failed:"..res.error)
105 106
   return
else
107
   print("select from super table--- pass")
108 109 110 111 112
   for i = 1, #(res.item) do
      print("res:"..res.item[i].count)
   end
end

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
function async_query_callback(res)
   if res.code ~=0 then
      print("async_query_callback--- failed:"..res.error)
      return
   else

   if(res.affected == 3) then
      print("async_query_callback, insert records--- pass")
   else
      print("async_query_callback, insert records---failed: expect 3 affected records, actually affected "..res.affected)
   end 

   end
end

driver.query_a(conn,"INSERT INTO therm1 VALUES ('2019-09-01 00:00:00.005', 100),('2019-09-01 00:00:00.006', 101),('2019-09-01 00:00:00.007', 102)", async_query_callback)


function stream_callback(t)
132
   print("------------------------")
133 134 135 136 137 138 139
   print("continuous query result:")
   for key, value in pairs(t) do
      print("key:"..key..", value:"..value)
   end
end

local stream
140
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, stream_callback)
141
if res.code ~=0 then
142
   print("open stream--- failed:"..res.error)
143 144
   return
else
145
   print("open stream--- pass")
146 147 148
   stream = res.stream
end

149
print("From now on we start continous insert in an definite (infinite if you want) loop.")
150
local loop_index = 0
151
while loop_index < 30 do
152 153 154 155 156
   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
157
      print("continous insertion--- failed:" .. res.error)
158 159
      return
   else
160
      --print("insert successfully, affected:"..res.affected)
161 162 163 164 165 166
   end
   os.execute("sleep " .. 1)
   loop_index = loop_index + 1
end

driver.close_stream(stream)
167

168
driver.close(conn)