OmniSciDB  04ee39c94c
MapDExample.java
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
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 
17 import static java.lang.System.out;
18 
19 import com.mapd.thrift.server.MapD;
20 import com.mapd.thrift.server.TDatumType;
21 import com.mapd.thrift.server.TQueryResult;
22 
23 import org.apache.thrift.TException;
24 import org.apache.thrift.protocol.TBinaryProtocol;
25 import org.apache.thrift.protocol.TJSONProtocol;
26 import org.apache.thrift.transport.THttpClient;
27 import org.apache.thrift.transport.TSSLTransportFactory;
28 import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
29 import org.apache.thrift.transport.TSocket;
30 import org.apache.thrift.transport.TTransport;
31 
32 import java.util.Date;
33 
34 /*
35 Contact support@mapd.com with any questions
36 
37 Install Thrift and generate MapD Thrift client
38 thrift -gen java mapd.thrift
39 
40 Dependencies:
41 /thrift-0.10.0/lib/java/src/
42 /slf4j-api-1.7.21.jar
43 /slf4j-simple-1.7.21.jar
44 /thrift/gen-java/
45 /httpcore-4.2.3.jar
46 /httpclient-4.2.3.jar
47 
48 Compile statement:
49 javac -cp
50 /path/to/thrift-0.10.0/lib/java/src:/path/to/slf4j-api-1.7.21.jar:/path/to/thrift/gen-java/:/path/to/httpcore-4.2.3.jar:/path/to/httpclient-4.2.3.jar:.
51 MapDExample.java
52 
53 Execution example:
54 java -cp
55 /path/to/thrift-0.10.0/lib/java/src:/path/to/slf4j-api-1.7.21.jar:/path/to/gen-java/:/path/to/httpcore-4.2.3.jar:/path/to/httpclient-4.2.3.jar:.
56 MapDExample
57 
58 Connection samples:
59 HTTP client - get_client('http://test.mapd.com:6274', null, true)
60 Binary protocol - get_client('locahost', 6274, false)
61 
62 */
63 
64 public class MapDExample {
65  public static void main(String[] args) {
66  String db_name = "mapd";
67  String user_name = "mapd";
68  String passwd = "HyperInteractive";
69  String hostname = "http://test.mapd.com:6274";
70  int portno = 6274;
71  MapD.Client client;
72  int session;
73  String query;
74  TQueryResult results;
75  int numRows, numCols;
76  boolean fieldIsArray;
77  TDatumType fieldType;
78  String fieldName, fieldType2;
79 
80  try {
81  client = get_client(hostname, portno, false);
82  session = client.connect(user_name, passwd, db_name);
83  System.out.println("Connection complete");
84  query = "select a,b from table1 limit 25;";
85  System.out.println("Query is: " + query);
86 
87  // always use True for is columnar
88  results = client.sql_execute(session, query, true, null, -1, -1);
89 
90  if (results.row_set.is_columnar) {
91  numRows = results.row_set.columns.get(0).nulls.size();
92  numCols = results.row_set.row_desc.size();
93  for (int r = 0; r < numRows; r++) {
94  for (int c = 0; c < numCols; c++) {
95  fieldName = results.row_set.row_desc.get(c).col_name;
96  fieldType = results.row_set.row_desc.get(c).col_type.type;
97  fieldType2 = fieldType.toString();
98  fieldIsArray = results.row_set.row_desc.get(c).col_type.is_array;
99  System.out.println(fieldName);
100  if (fieldIsArray) {
101  System.out.println(
102  results.row_set.columns.get(c).data.arr_col.get(r).data.str_col);
103  } else {
104  switch (fieldType2) {
105  case "BOOL":
106  break;
107  case "SMALLINT":
108  case "INT":
109  case "BIGINT":
110  System.out.println(results.row_set.columns.get(c).data.int_col.get(r));
111  break;
112  case "FLOAT":
113  case "DOUBLE":
114  case "DECIMAL":
115  System.out.println(results.row_set.columns.get(c).data.real_col.get(r));
116  break;
117  case "STR":
118  System.out.println(results.row_set.columns.get(c).data.str_col.get(r));
119  break;
120  case "TIME":
121  case "TIMESTAMP":
122  case "DATE":
123  System.out.println(new Date(
124  results.row_set.columns.get(c).data.int_col.get(r) * 1000));
125  break;
126  default:
127  break;
128  }
129  }
130  }
131  }
132 
133  } else {
134  System.out.println("Please use columns not row in query execution");
135  client.disconnect(session);
136  System.exit(0);
137  }
138  client.disconnect(session);
139  } catch (Exception x) {
140  x.printStackTrace();
141  }
142  }
143 
144  public static MapD.Client get_client(String host_or_uri, int port, boolean http) {
145  THttpClient httpTransport;
146  TTransport transport;
147  TBinaryProtocol protocol;
148  TJSONProtocol jsonProtocol;
149  TSocket socket;
150  MapD.Client client;
151 
152  try {
153  if (http) {
154  httpTransport = new THttpClient(host_or_uri);
155  jsonProtocol = new TJSONProtocol(httpTransport);
156  client = new MapD.Client(jsonProtocol);
157  httpTransport.open();
158  return client;
159  } else {
160  transport = new TSocket(host_or_uri, port);
161  protocol = new TBinaryProtocol(transport);
162  client = new MapD.Client(protocol);
163  transport.open();
164  return client;
165  }
166  } catch (TException x) {
167  x.printStackTrace();
168  }
169  return null;
170  }
171 }
void c(const std::string &query_string, const ExecutorDeviceType device_type)
mapd::shared_ptr< MapDClient > client
static void main(String[] args)
static MapD.Client get_client(String host_or_uri, int port, boolean http)
TSessionId session