CLI/ODBC Example
mach@localhost:~/machbase_home$ cd sample/ mach@localhost:~/machbase_home/sample$ cd cli/ mach@localhost:~/machbase_home/sample/cli$ ls Makefile sample1_connect.c
If the Machbase package was installed, the sample program will be installed in the following path.
include $(MACHBASE_HOME)/install/machbase_env.mk INCLUDES += $(LIBDIR_OPT)/$(MACHBASE_HOME)/include all : sample1_connect sample1_connect : sample1_connect.o $(LD_CC) $(LD_FLAGS) $(LD_OUT_OPT)$@ $< $(LIB_OPT)machbasecli$(LIB_AFT) $(LIBDIR_OPT)$(MACHBASE_HOME)/lib $(LD_LIBS) sample1_connect.o : sample1_connect.c $(COMPILE.cc) $(CC_FLAGS) $(INCLUDES) $(CC_OUT_OPT)$@ $< clean : rm -f sample1_connect
Executing the following for a given sample creates an executable file.
mach@localhost:~/machbase_home/sample/cli$ make gcc -c -g -W -Wall -rdynamic -O3 -finline-functions -fno-omit-frame-pointer -fno-strict-aliasing -m64 -mtune=k8 -g -W -Wall -rdynamic -O3 -finline-functions -fno-omit-frame-pointer -fno-strict-aliasing -m64 -mtune=k8 -I/home/machbase/machbase_home/include -I. -L//home/machbase/machbase_home/include -osample1_connect.o sample1_connect.c gcc -m64 -mtune=k8 -L/home/machbase/machbase_home/lib -osample1_connect sample1_connect.o -lmachbasecli -L/home/machbase/machbase_home/lib -lm -lpthread -ldl -lrt -rdynamic mach@localhost:~/machbase_home/sample/cli$ ls -al total 1196 drwxrwxr-x 2 mach mach 4096 Jun 18 20:15 . drwxrwxr-x 4 mach mach 4096 Jun 18 19:26 .. -rw-rw-r-- 1 mach mach 483 Jun 18 19:26 Makefile -rwxrwxr-x 1 mach mach 1196943 Jun 18 20:15 sample1_connect -rw-rw-r-- 1 mach mach 549 Jun 18 19:26 sample1_connect.c -rw-rw-r-- 1 mach mach 8168 Jun 18 20:15 sample1_connect.o
You can write your application as necessary by modifying the sample Makefile above
Sample Program
Connection Example
We will create an example program to connect using the CLI.
The sample file name is sample1_connect.c.
MACHBASE_PORT_NO must be the same as the PORT_NO value in the $MACHBASE_HOME/conf/machbase.conf file.
If you register sample1_connect.c in Makefile, compile and run it, it will appear as follows.
[mach@localhost cli]$ make [mach@localhost cli]$ ./sample1_connect connected ...
Data Input and Output Example
In the example source below, we created a table using the CREATE TABLE statement, arbitrarily create simple data values, input data using the INSERT statement, and output the data using the SELECT statement. You will be able to see how to configure each type when entering and checking values directly.
The sample file name is sample2_insert.c.
If you register sample2_insert.c in the Makefile, compile and run it, it will appear as follows.
[mach@localhost cli]$ make [mach@localhost cli]$ ./sample2_insert connected ... 1 record inserted 2 record inserted 3 record inserted 4 record inserted 5 record inserted 6 record inserted 7 record inserted 8 record inserted 9 record inserted ===== 0 ======== seq = 9, score = 18, total = 270000, percentage = 0.00, ratio = 3.3e-05, id = id-9, srcip = 192.168.0.9, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0009, regdate = 2015-03-31 15:26:09, log = text log-9, image = 62696E61727920696D6167652D39 ===== 1 ======== seq = 8, score = 16, total = 240000, percentage = 0.00, ratio = 3.3e-05, id = id-8, srcip = 192.168.0.8, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0008, regdate = 2015-03-31 15:26:08, log = text log-8, image = 62696E61727920696D6167652D38 ===== 2 ======== seq = 7, score = 14, total = 210000, percentage = 0.00, ratio = 3.3e-05, id = id-7, srcip = 192.168.0.7, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0007, regdate = 2015-03-31 15:26:07, log = text log-7, image = 62696E61727920696D6167652D37 ===== 3 ======== seq = 6, score = 12, total = 180000, percentage = 0.00, ratio = 3.3e-05, id = id-6, srcip = 192.168.0.6, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0006, regdate = 2015-03-31 15:26:06, log = text log-6, image = 62696E61727920696D6167652D36 ===== 4 ======== seq = 5, score = 10, total = 150000, percentage = 0.00, ratio = 3.3e-05, id = id-5, srcip = 192.168.0.5, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0005, regdate = 2015-03-31 15:26:05, log = text log-5, image = 62696E61727920696D6167652D35 ===== 5 ======== seq = 4, score = 8, total = 120000, percentage = 0.00, ratio = 3.3e-05, id = id-4, srcip = 192.168.0.4, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0004, regdate = 2015-03-31 15:26:04, log = text log-4, image = 62696E61727920696D6167652D34 ===== 6 ======== seq = 3, score = 6, total = 90000, percentage = 0.00, ratio = 3.3e-05, id = id-3, srcip = 192.168.0.3, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0003, regdate = 2015-03-31 15:26:03, log = text log-3, image = 62696E61727920696D6167652D33 ===== 7 ======== seq = 2, score = 4, total = 60000, percentage = 0.00, ratio = 3.3e-05, id = id-2, srcip = 192.168.0.2, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0002, regdate = 2015-03-31 15:26:02, log = text log-2, image = 62696E61727920696D6167652D32 ===== 8 ======== seq = 1, score = 2, total = 30000, percentage = 0.00, ratio = 3.3e-05, id = id-1, srcip = 192.168.0.1, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0001, regdate = 2015-03-31 15:26:01, log = text log-1, image = 62696E61727920696D6167652D31
Prepare Execute Example
Let's write an example program that binds and INSERTs data.
You can enter a value by binding data in Machbase. When you use this, you need to specify the types of data values clearly. In case of long string types, you must specify the length value.
The following example shows how to bind data for each type.
The file name is sample3_prepare.c.
If you register sample3_prepare.c in the Makefile, compile and run it, it will appear as follows.
[mach@localhost cli]$ make [mach@localhost cli]$ ./sample3_prepare connected ... 1 prepared record inserted 2 prepared record inserted 3 prepared record inserted 4 prepared record inserted 5 prepared record inserted 6 prepared record inserted 7 prepared record inserted 8 prepared record inserted 9 prepared record inserted ===== 0 ======== seq = 9, score = 18, total = 270000, percentage = 1.11, ratio = 3.7037e-05, id = id-9, srcip = 192.168.0.9, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0009, regdate = 1970-01-01 09:00:00, log = log-9, image = 696D6167652D39 ===== 1 ======== seq = 8, score = 16, total = 240000, percentage = 1.12, ratio = 3.75e-05, id = id-8, srcip = 192.168.0.8, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0008, regdate = 1970-01-01 09:00:00, log = log-8, image = 696D6167652D38 ===== 2 ======== seq = 7, score = 14, total = 210000, percentage = 1.14, ratio = 3.80952e-05, id = id-7, srcip = 192.168.0.7, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0007, regdate = 1970-01-01 09:00:00, log = log-7, image = 696D6167652D37 ===== 3 ======== seq = 6, score = 12, total = 180000, percentage = 1.17, ratio = 3.88889e-05, id = id-6, srcip = 192.168.0.6, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0006, regdate = 1970-01-01 09:00:00, log = log-6, image = 696D6167652D36 ===== 4 ======== seq = 5, score = 10, total = 150000, percentage = 1.20, ratio = 4e-05, id = id-5, srcip = 192.168.0.5, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0005, regdate = 1970-01-01 09:00:00, log = log-5, image = 696D6167652D35 ===== 5 ======== seq = 4, score = 8, total = 120000, percentage = 1.25, ratio = 4.16667e-05, id = id-4, srcip = 192.168.0.4, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0004, regdate = 1970-01-01 09:00:00, log = log-4, image = 696D6167652D34 ===== 6 ======== seq = 3, score = 6, total = 90000, percentage = 1.33, ratio = 4.44444e-05, id = id-3, srcip = 192.168.0.3, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0003, regdate = 1970-01-01 09:00:00, log = log-3, image = 696D6167652D33 ===== 7 ======== seq = 2, score = 4, total = 60000, percentage = 1.50, ratio = 5e-05, id = id-2, srcip = 192.168.0.2, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0002, regdate = 1970-01-01 09:00:00, log = log-2, image = 696D6167652D32 ===== 8 ======== seq = 1, score = 2, total = 30000, percentage = 2.00, ratio = 6.66667e-05, id = id-1, srcip = 192.168.0.1, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0001, regdate = 1970-01-01 09:00:00, log = log-1, image = 696D6167652D31
Extension Function Append Example
In Machbase, the append protocol is provided by reading a large amount of data from a file and inputting it at a high speed. Let's write an example program that uses this Append protocol.
First, let's look at an example of how to append to the various types provided by Machbase. The Append method has its own settings for each type. Therefore, if you know how to use every method, you will be able to write programs more efficiently. All the methods are listed in the example code at the bottom.
The file name is sample4_append1.c.
If you register sample4_append1.c in the Makefile, compile and run it, it will appear as follows.
[mach@localhost cli]$ make sample4_append1 gcc -c -g -W -Wall -rdynamic -fno-inline -m64 -mtune=k8 -g -W -Wall -rdynamic -fno-inline -m64 -mtune=k8 -I/home/mach/machbase_home/include -I. -L//home/mach/machbase_home/include -osample4_append1.o sample4_append1.c gcc -m64 -mtune=k8 -L/home/mach/machbase_home/lib -osample4_append1 sample4_append1.o -lmachcli -L/home/mach/machbase_home/lib -lm -lpthread -ldl -lrt -rdynamic [mach@localhost cli]$ ./sample4_append1 connected ... append open ok append close ok success : 13, failure : 0 timegap = 48 microseconds for 13 records 270833.33 records/second [mach@localhost cli]$ You can check what is inserted after MACH_SQL. Mach> select * from CLI_SAMPLE; SHORT1 INTEGER1 LONG1 FLOAT1 DOUBLE1 ----------------------------------------------------------------------------------------------------------- DATETIME1 VARCHAR1 IP IP2 ------------------------------------------------------------------------------------------------------------------------------ TEXT1 ------------------------------------------------------------------------------------ BIN1 ------------------------------------------------------------------------------------ 2 4 6 8.4 10.9 2000-12-31 00:00:00 000:000:000 MY VARCHAR 203.212.222.111 NULL XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX FAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFA FAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFA FAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFAFA 2 4 6 8.4 10.9 2000-12-31 00:00:00 000:000:000 MY VARCHAR 203.212.222.111 NULL XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX NULL 2 4 6 8.4 10.9 2000-12-31 00:00:00 000:000:000 MY VARCHAR 203.212.222.111 7F7F:7F7F:7F7F:7F7F:7F7F:7F7F:7F7F:7F7F NULL NULL 2 4 6 8.4 10.9 2000-12-31 00:00:00 000:000:000 MY VARCHAR 203.212.222.111 NULL NULL NULL 2 4 6 8.4 10.9 2000-12-31 00:00:00 000:000:000 MY VARCHAR 192.168.0.1 NULL NULL NULL 2 4 6 8.4 10.9 2000-12-31 00:00:00 000:000:000 MY VARCHAR 127.0.0.1 NULL NULL NULL 2 4 6 8.4 10.9 2000-12-31 00:00:00 000:000:000 MY VARCHAR NULL NULL NULL NULL 2 4 6 8.4 10.9 2000-12-31 00:00:00 000:000:000 NULL NULL NULL NULL NULL 2 4 6 8.4 10.9 2014-05-23 17:41:28 000:000:000 NULL NULL NULL NULL NULL 2 4 6 8.4 10.9 2015-04-09 16:44:11 134:256:000 NULL NULL NULL NULL NULL 2 4 6 8.4 10.9 1970-01-01 09:00:01 000:000:000 NULL NULL NULL NULL NULL 2 4 6 8.4 10.9 1970-01-01 09:00:00 000:000:000 NULL NULL NULL NULL NULL [12] row(s) selected.
Now let's use a fast append method using a file. This is an example useful for fast input of large amounts of logs, packets, etc. used in business. The file name is sample4_append2.c.
You have to save the data to be entered in advance in data.txt.
./make_data
Modifying the given make_data.c gives you the opportunity to create a data.txt file for your situation.
If you register sample4_append2.c in the Makefile, compile and run it, it will appear as follows.
[mach@localhost cli]$ make gcc -c -g -W -Wall -rdynamic -fno-inline -m64 -mtune=k8 -g -W -Wall -rdynamic -fno-inline -m64 -mtune=k8 -I/home/mach/machbase_home/include -I. -L//home/mach/machbase_home/include -osingle_append2.o single_append2.c gcc -m64 -mtune=k8 -L/home/mach/machbase_home/lib -osingle_append2 single_append2.o -lmachcli -L/home/mach/machbase_home/lib -lm -lpthread -ldl -lrt -rdynamic [mach@localhost cli]$ ./single_append2 connected ... table created append open ok append data start .................................................................................................... append data end append close ok success : 1000000, failure : 0 timegap = 1641503 microseconds for 1000000 records 609197.79 records/second
Acquiring Table Column Information Example
There are a number of ways to obtain table column information, but we will look at how to use SQLDescribeCol and SQLColumns.
SQLDescribeCol
SQLDescribeCol is a function that retrieves the number, name, buffer size, length, and type of table columns.
The sample file name is sample5_describe.c.
If you add the above file and run make, you can see the contents of the column as shown below.
[mach@localhost cli]$ make [mach@localhost cli]$ ./sample5_describe connected ... ---------------------------------------------------------------- Name Type Length ---------------------------------------------------------------- SEQ 5 5 SCORE 4 10 TOTAL -5 19 PERCENTAGE 6 27 RATIO 8 27 ID 12 10 SRCIP 2104 15 DSTIP 2106 60 REG_DATE 9 31 TLOG 2100 67108864 IMAGE -2 67108864 ---------------------------------------------------------------- [mach@localhost cli]$
SQLColumns
SQLColumns is a function that can find the information of the columns existing in the current table. Machbase also supports the above functions and can be used to find out the information of each column.
The file name is sample6_columns.c.
Add the above file and run make. The results are as follows.
[mach@localhost cli]$ make [mach@localhost cli]$ ./sample6_columns connected ... -------------------------------------------------------------------------------- Name Type TypeName Length -------------------------------------------------------------------------------- _ARRIVAL_TIME 93 DATE 31 SEQ 5 SMALLINT 5 SCORE 4 INTEGER 10 TOTAL -5 BIGINT 19 PERCENTAGE 6 FLOAT 27 RATIO 8 DOUBLE 27 ID 12 VARCHAR 10 SRCIP 2104 IPV4 15 DSTIP 2106 IPV6 60 REG_DATE 93 DATE 31 TLOG 2100 TEXT 67108864 IMAGE -2 BINARY 67108864 --------------------------------------------------------------------------------
An example of using multiple threads in one program to append to multiple tables.
The file name is sample8_multi_session_multi_table.c.
Add the make code and run the executable file. Because the threads are used, the output order may be different. The results are as follows.
[mach@localhost cli]$ make sample8_multi_session_multi_table gcc -c -g -W -Wall -rdynamic -fno-inline -m64 -mtune=k8 -g -W -Wall -rdynamic -fno-inline -m64 -mtune=k8 -I/home/mach/machbase_home/include -I. -L//home/mach/machbase_home/include -osample8_multi_session_multi_table.o sample8_multi_session_multi_table.c gcc -m64 -mtune=k8 -L/home/mach/machbase_home/lib -osample8_multi_session_multi_table sample8_multi_session_multi_table.o -lmachcli -L/home/mach/machbase_home/lib -lm -lpthread -ldl -lrt -rdynamic [mach@localhost cli]$ ./sample8_multi_session_multi_table connectDB success. createTables success. [0]connectDB success. [1]connectDB success. [2]connectDB success. [1-0]appendOpen success. [0-0]appendOpen success. [2-0]appendOpen success. [1-1]appendOpen success. [2-1]appendOpen success. [0-1]appendOpen success. [1-2]appendOpen success. [2-2]appendOpen success. file open success - [1][suffle_data2.txt] file open success - [2][suffle_data3.txt] [0-2]appendOpen success. file open success - [0][suffle_data1.txt] ....................................................................................... [1-0]appendClose start... .. [0-0]appendClose start... append result success : 100000, failure : 0 [1-0]appendClose success [1-1]appendClose start... append result success : 100000, failure : 0 [1-1]appendClose success [1-2]appendClose start... append result success : 100000, failure : 0 [1-2]appendClose success append result success : 100000, failure : 0 [0-0]appendClose success [0-1]appendClose start... .append result success : 100000, failure : 0 [0-1]appendClose success [0-2]appendClose start... append result success : 100000, failure : 0 [0-2]appendClose success [2-0]appendClose start... append result success : 100000, failure : 0 [2-0]appendClose success [2-1]appendClose start... append result success : 100000, failure : 0 [2-1]appendClose success [2-2]appendClose start... append result success : 100000, failure : 0 [2-2]appendClose success [1]disconnected. [2]disconnected. [0]disconnected. 1 thread join 2 thread join 3 thread join
You can see the result through machsql as below.
[mach@localhost cli]$ machsql ================================================================= Machbase Client Query Utility Release Version 3.5.0 Copyright 2014, Machbase Inc. or its subsidiaries. All Rights Reserved. ================================================================= Machbase Server Addr (Default:127.0.0.1) : Machbase User ID (Default:SYS) Machbase User Password : manager MACH_CONNECT_MODE=INET, PORT=5656 Mach> select count(*) from table_f1; count(*) ----------------------- 300000 [1] Row Selected. Mach> select count(*) from table_f2; count(*) ----------------------- 300000 [1] row(s) selected. Mach> select count(*) from table_event; count(*) ----------------------- 300000 [1] row(s) selected.