OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 com.mapd.thrift.server.MapD;
19 import com.mapd.thrift.server.TColumn;
20 import com.mapd.thrift.server.TColumnData;
21 import com.mapd.thrift.server.TColumnType;
22 import com.mapd.thrift.server.TDBInfo;
23 import com.mapd.thrift.server.TDatum;
24 import com.mapd.thrift.server.TMapDException;
25 import com.mapd.thrift.server.TQueryResult;
26 import com.mapd.thrift.server.TRow;
27 import com.mapd.thrift.server.TRowSet;
28 import com.mapd.thrift.server.TTableDetails;
29 import com.mapd.thrift.server.TTypeInfo;
30 
31 import org.apache.thrift.TException;
32 import org.apache.thrift.protocol.TBinaryProtocol;
33 import org.apache.thrift.protocol.TProtocol;
34 import org.apache.thrift.transport.TSocket;
35 import org.apache.thrift.transport.TTransport;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Map.Entry;
42 
47 public class ThriftTester {
48  final static Logger logger = LoggerFactory.getLogger(ThriftTester.class);
49 
50  public static void main(String[] args) {
51  logger.info("Hello, World");
52 
53  ThriftTester x = new ThriftTester();
54  x.doWork(args);
55  }
56 
57  void doWork(String[] args) {
58  logger.info("In doWork");
59 
60  TTransport transport = null;
61  try {
62  transport = new TSocket("localhost", 6274);
63  // transport = new THttpClient("http://localhost:6278");
64 
65  transport.open();
66 
67  TProtocol protocol = new TBinaryProtocol(transport);
68  // TProtocol protocol = new TJSONProtocol(transport);
69  // TProtocol protocol = new TProtocol(transport);
70 
71  MapD.Client client = new MapD.Client(protocol);
72 
73  String session = null;
74 
75  session = client.connect("admin", "HyperInteractive", "omnisci");
76 
77  logger.info("Connected session is " + session);
78 
79  // lets fetch databases from mapd
80  List<TDBInfo> dbs = client.get_databases(session);
81 
82  for (TDBInfo db : dbs) {
83  logger.info("db is " + db.toString());
84  }
85 
86  // lets fetch tables from mapd
87  List<String> tables = client.get_tables(session);
88 
89  for (String tab : tables) {
90  logger.info("Tables is " + tab);
91  }
92 
93  // lets get the version
94  logger.info("Version " + client.get_version());
95 
96  // get table_details
97  TTableDetails table_details = client.get_table_details(session, "flights");
98  for (TColumnType col : table_details.row_desc) {
99  logger.info("col name :" + col.col_name);
100  logger.info("\tcol encoding :" + col.col_type.encoding);
101  logger.info("\tcol is_array :" + col.col_type.is_array);
102  logger.info("\tcol nullable :" + col.col_type.nullable);
103  logger.info("\tcol type :" + col.col_type.type);
104  }
105 
106  // client.set_execution_mode(session, TExecuteMode.CPU);
107  logger.info(" -- before query -- ");
108 
109  TQueryResult sql_execute = client.sql_execute(session,
110  "Select uniquecarrier,flightnum from flights LIMIT 3;",
111  true,
112  null,
113  -1,
114  -1);
115  // client.send_sql_execute(session, "Select BRAND from ACV ;", true);
116  // logger.info(" -- before query recv -- ");
117  // TQueryResult sql_execute = client.recv_sql_execute();
118 
119  logger.info(" -- after query -- ");
120 
121  logger.info("TQueryResult execution time is " + sql_execute.getExecution_time_ms());
122  logger.info("TQueryResult is " + sql_execute.toString());
123  logger.info("TQueryResult getFieldValue is "
124  + sql_execute.getFieldValue(TQueryResult._Fields.ROW_SET));
125 
126  TRowSet row_set = sql_execute.getRow_set();
127  Object fieldValue = sql_execute.getFieldValue(TQueryResult._Fields.ROW_SET);
128  logger.info("fieldValue " + fieldValue);
129 
130  logger.info("TRowSet is " + row_set.toString());
131 
132  logger.info("Get rows size " + row_set.getRowsSize());
133  logger.info("Get col size " + row_set.getRowsSize());
134 
135  List<TRow> rows = row_set.getRows();
136  int count = 1;
137  for (TRow row : rows) {
138  List<TDatum> cols = row.getCols();
139  if (cols != null) {
140  for (TDatum dat : cols) {
141  logger.info("ROW " + count + " " + dat.getFieldValue(TDatum._Fields.VAL));
142  }
143  count++;
144  }
145  }
146 
147  List<TColumn> columns = row_set.getColumns();
148 
149  logger.info("columns " + columns);
150  count = 1;
151  for (TColumn col : columns) {
152  TColumnData data = col.getData();
153  if (data != null) {
154  logger.info("COL " + count + " " + data.toString());
155  }
156  count++;
157  }
158 
159  int dash = client.create_dashboard(session, "test1", "state", "image", "metadata");
160 
161  logger.info("dash id is " + dash);
162 
163  int dash2 =
164  client.create_dashboard(session, "test2", "state2", "image2", "metadata2");
165 
166  logger.info("dash2 id is " + dash2);
167 
168  client.replace_dashboard(
169  session, dash2, "test3", "mapd", "state3", "image3", "metadata3");
170 
171  logger.info("replaced");
172 
173  // Now disconnect
174  logger.info("Trying to disconnect session " + session);
175  client.disconnect(session);
176  } catch (TMapDException ex) {
177  logger.error(ex.toString());
178  ex.printStackTrace();
179  } catch (TException ex) {
180  logger.error(ex.toString());
181  ex.printStackTrace();
182  } catch (Exception ex) {
183  logger.error(ex.toString());
184  ex.printStackTrace();
185  }
186 
187  logger.info("Connection Ended");
188  logger.info("Exit");
189  }
190 }
static void main(String[] args)