OmniSciDB  04ee39c94c
MapDExample.cs
Go to the documentation of this file.
1 using System;
2 using Thrift;
3 using Thrift.Protocol;
4 using Thrift.Server;
5 using Thrift.Transport;
6 using static MapD;
7 using static TDatumType;
8 using static TQueryResult;
9 
10 /*
11 
12 Contact support@mapd.com with any questions
13 
14 Install Thrift and generate MapD Thrift client
15 thrift -gen csharp mapd.thrift
16 
17 Dependencies:
18 /thrift/gen-csharp
19 ApacheThrift 0.10.0
20 
21 Connection samples:
22 HTTP client - get_client('http://test.mapd.com:6274', null, true)
23 Binary protocol - get_client('locahost', 6274, false)
24 
25 */
26 
27 namespace MapDExample {
28 public
29 class MapDExample {
30  public
31  static void Main() {
32  string db_name = "omnisci";
33  string user_name = "admin";
34  string passwd = "HyperInteractive";
35  string hostname = "http://test.mapd.com";
36  int portno = 6274;
37  MapD.Client client;
38  int session;
39  string query;
40  TQueryResult results;
41  int numRows, numCols;
42  bool fieldIsArray;
43  TDatumType fieldType;
44  string fieldName, fieldType2;
45  try {
46  client = get_client(hostname, portno, true);
47  session = client.connect(user_name, passwd, db_name);
48  Console.WriteLine("Connection Completed");
49  query = "select a, b from table1 limit 10;";
50  Console.WriteLine("Query is: " + query);
51  results = client.sql_execute(session, query, true, null, -1, -1);
52 
53  if (results.Row_set.Is_columnar) {
54  numRows = results.Row_set.Columns[0].Nulls.Count;
55  numCols = results.Row_set.Row_desc.Count;
56  for (int r = 0; r < numRows; r++) {
57  for (int c = 0; c < numCols; c++) {
58  fieldName = results.Row_set.Row_desc[c].Col_name;
59  fieldType = results.Row_set.Row_desc[c].Col_type.Type;
60  fieldType2 = fieldType.ToString();
61  fieldIsArray = results.Row_set.Row_desc[c].Col_type.Is_array;
62  Console.WriteLine(fieldName);
63  if (fieldIsArray) {
64  Console.WriteLine(results.Row_set.Columns[c].Data.Arr_col[r].Data.Str_col);
65  } else {
66  switch (fieldType2) {
67  case "BOOL":
68  break;
69  case "SMALLINT":
70  case "INT":
71  case "BIGINT":
72  Console.WriteLine(results.Row_set.Columns[c].Data.Int_col[r]);
73  break;
74  case "FLOAT":
75  case "DOUBLE":
76  case "DECIMAL":
77  Console.WriteLine(results.Row_set.Columns[c].Data.Real_col[r]);
78  break;
79  case "STR":
80  Console.WriteLine(results.Row_set.Columns[c].Data.Str_col[r]);
81  break;
82  case "TIME":
83  case "TIMESTAMP":
84  case "DATE":
85  Console.WriteLine(
86  new DateTime(1970, 1, 1, 0, 0, 0)
87  .AddMilliseconds(Convert.ToDouble(
88  results.Row_set.Columns[c].Data.Int_col[r] * 1000)));
89  break;
90  default:
91  break;
92  }
93  }
94  }
95  }
96  }
97  } catch (TApplicationException x) {
98  Console.WriteLine(x.StackTrace);
99  }
100  }
101 
102  public
103  static MapD.Client get_client(string host_or_uri, int port, bool http) {
104  THttpClient httpTransport;
105  TTransport transport;
106  TBinaryProtocol protocol;
107  TJSONProtocol jsonProtocol;
108  MapD.Client client;
109 
110  try {
111  if (http) {
112  Uri httpuri = new Uri(host_or_uri);
113  httpTransport = new THttpClient(httpuri);
114  jsonProtocol = new TJSONProtocol(httpTransport);
115  client = new MapD.Client(jsonProtocol);
116  httpTransport.Open();
117  return client;
118  } else {
119  transport = new TSocket(host_or_uri, port);
120  protocol = new TBinaryProtocol(transport);
121  client = new MapD.Client(protocol);
122  transport.Open();
123  return client;
124  }
125  } catch (TException x) {
126  Console.WriteLine(x.StackTrace);
127  }
128  return null;
129  }
130 }
131 } // namespace MapDExample
void c(const std::string &query_string, const ExecutorDeviceType device_type)
static MapD.Client get_client(string host_or_uri, int port, bool http)
Definition: MapDExample.cs:103
static void Main()
Definition: MapDExample.cs:31
mapd::shared_ptr< MapDClient > client
static MapD.Client get_client(String host_or_uri, int port, boolean http)
TSessionId session