OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ThriftTester.java
Go to the documentation of this file.
1 /*
2  * Copyright 2015 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.mapd.testthrift;
17 
18 import org.apache.thrift.TException;
19 import org.apache.thrift.protocol.TBinaryProtocol;
20 import org.apache.thrift.protocol.TProtocol;
21 import org.apache.thrift.transport.TSocket;
22 import org.apache.thrift.transport.TTransport;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 
30 import ai.heavy.thrift.server.Heavy;
31 import ai.heavy.thrift.server.TColumn;
32 import ai.heavy.thrift.server.TColumnData;
33 import ai.heavy.thrift.server.TColumnType;
34 import ai.heavy.thrift.server.TDBException;
35 import ai.heavy.thrift.server.TDBInfo;
36 import ai.heavy.thrift.server.TDatum;
37 import ai.heavy.thrift.server.TQueryResult;
38 import ai.heavy.thrift.server.TRow;
39 import ai.heavy.thrift.server.TRowSet;
40 import ai.heavy.thrift.server.TTableDetails;
41 import ai.heavy.thrift.server.TTypeInfo;
42 
43 public class ThriftTester {
44  final static Logger logger = LoggerFactory.getLogger(ThriftTester.class);
45 
46  public static void main(String[] args) {
47  logger.info("Hello, World");
48 
49  ThriftTester x = new ThriftTester();
50  x.doWork(args);
51  }
52 
53  void doWork(String[] args) {
54  logger.info("In doWork");
55 
56  TTransport transport = null;
57  try {
58  transport = new TSocket("localhost", 6274);
59  // transport = new THttpClient("http://localhost:6278");
60 
61  transport.open();
62 
63  TProtocol protocol = new TBinaryProtocol(transport);
64  // TProtocol protocol = new TJSONProtocol(transport);
65  // TProtocol protocol = new TProtocol(transport);
66 
67  Heavy.Client client = new Heavy.Client(protocol);
68 
69  String session = null;
70 
71  session = client.connect("admin", "HyperInteractive", "omnisci");
72 
73  logger.info("Connected session is " + session);
74 
75  // lets fetch databases from mapd
76  List<TDBInfo> dbs = client.get_databases(session);
77 
78  for (TDBInfo db : dbs) {
79  logger.info("db is " + db.toString());
80  }
81 
82  // lets fetch tables from mapd
83  List<String> tables = client.get_tables(session);
84 
85  for (String tab : tables) {
86  logger.info("Tables is " + tab);
87  }
88 
89  // lets get the version
90  logger.info("Version " + client.get_version());
91 
92  // get table_details
93  TTableDetails table_details = client.get_table_details(session, "flights");
94  for (TColumnType col : table_details.row_desc) {
95  logger.info("col name :" + col.col_name);
96  logger.info("\tcol encoding :" + col.col_type.encoding);
97  logger.info("\tcol is_array :" + col.col_type.is_array);
98  logger.info("\tcol nullable :" + col.col_type.nullable);
99  logger.info("\tcol type :" + col.col_type.type);
100  }
101 
102  // client.set_execution_mode(session, TExecuteMode.CPU);
103  logger.info(" -- before query -- ");
104 
105  TQueryResult sql_execute = client.sql_execute(session,
106  "Select uniquecarrier,flightnum from flights LIMIT 3;",
107  true,
108  null,
109  -1,
110  -1);
111  // client.send_sql_execute(session, "Select BRAND from ACV ;", true);
112  // logger.info(" -- before query recv -- ");
113  // TQueryResult sql_execute = client.recv_sql_execute();
114 
115  logger.info(" -- after query -- ");
116 
117  logger.info("TQueryResult execution time is " + sql_execute.getExecution_time_ms());
118  logger.info("TQueryResult is " + sql_execute.toString());
119  logger.info("TQueryResult getFieldValue is "
120  + sql_execute.getFieldValue(TQueryResult._Fields.ROW_SET));
121 
122  TRowSet row_set = sql_execute.getRow_set();
123  Object fieldValue = sql_execute.getFieldValue(TQueryResult._Fields.ROW_SET);
124  logger.info("fieldValue " + fieldValue);
125 
126  logger.info("TRowSet is " + row_set.toString());
127 
128  logger.info("Get rows size " + row_set.getRowsSize());
129  logger.info("Get col size " + row_set.getRowsSize());
130 
131  List<TRow> rows = row_set.getRows();
132  int count = 1;
133  for (TRow row : rows) {
134  List<TDatum> cols = row.getCols();
135  if (cols != null) {
136  for (TDatum dat : cols) {
137  logger.info("ROW " + count + " " + dat.getFieldValue(TDatum._Fields.VAL));
138  }
139  count++;
140  }
141  }
142 
143  List<TColumn> columns = row_set.getColumns();
144 
145  logger.info("columns " + columns);
146  count = 1;
147  for (TColumn col : columns) {
148  TColumnData data = col.getData();
149  if (data != null) {
150  logger.info("COL " + count + " " + data.toString());
151  }
152  count++;
153  }
154 
155  int dash = client.create_dashboard(session, "test1", "state", "image", "metadata");
156 
157  logger.info("dash id is " + dash);
158 
159  int dash2 =
160  client.create_dashboard(session, "test2", "state2", "image2", "metadata2");
161 
162  logger.info("dash2 id is " + dash2);
163 
164  client.replace_dashboard(
165  session, dash2, "test3", "mapd", "state3", "image3", "metadata3");
166 
167  logger.info("replaced");
168 
169  // Now disconnect
170  logger.info("Trying to disconnect session " + session);
171  client.disconnect(session);
172  } catch (TDBException ex) {
173  logger.error(ex.getError_msg());
174  ex.printStackTrace();
175  } catch (TException ex) {
176  logger.error(ex.toString());
177  ex.printStackTrace();
178  } catch (Exception ex) {
179  logger.error(ex.toString());
180  ex.printStackTrace();
181  }
182 
183  logger.info("Connection Ended");
184  logger.info("Exit");
185  }
186 }
tuple rows
Definition: report.py:114
static void main(String[] args)