Community Articles

Find and share helpful community-sourced technical articles.
Labels (1)
avatar

Sharing source code I wrote to a prospect that wants to try Phoenix + .NET.

It's a simple code that POST the query to Phoenix Server and parse result JSON and prints results on console:

using System;
using System.Net;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text;
 
namespace ConsoleApplication1
{
    class PhoenixSample
    {
    
        public class FirstFrame
        {
            public String offset;
            public List<List<String>> rows;
        }
        public class Result
        {
            public FirstFrame firstFrame;
            public int updateCount;
        }
        public class PhoenixResult
        {
            public string response { get; set; }
            public List<Result> results { get; set; }
        }
 
        static void Main(string[] args)
        {
            try
            {
                string url = "http://192.168.56.203:8765";
                string query = "select * from test";
                var syncClient = new WebClient();
                syncClient.Headers["request"] = "{\"request\":\"prepareAndExecute\",\"connectionId\":\"b27cbc83-a514-49f0-9bbe-f15d8bfb3532\",\"sql\":\"" + query + "\",\"maxRowCount\":-1}";
                syncClient.Headers["Content-Type"] = "application/json";
                byte[] responseArray = syncClient.UploadData(url, "POST", Encoding.ASCII.GetBytes(""));
                string json = Encoding.ASCII.GetString(responseArray);
 
                System.Diagnostics.Debug.WriteLine("teste");
                System.Diagnostics.Debug.WriteLine("json=" + json);
 
                PhoenixResult test = JsonConvert.DeserializeObject<PhoenixResult>(json);
                System.Diagnostics.Debug.WriteLine(test.response);
                System.Diagnostics.Debug.WriteLine(test.results[0].updateCount);
                for (int i = 0; i < test.results[0].firstFrame.rows.Count; i++)
                {
                    for (int j = 0; j < test.results[0].firstFrame.rows[i].Count; j++)
                    {
                        System.Diagnostics.Debug.Write("row # " + i + " col # " + j + " = ");
                        System.Diagnostics.Debug.WriteLine(test.results[0].firstFrame.rows[i][j]);
                    }
                }
 
            }  
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.Read();
            }
 
        }
    }
    }
9,889 Views
Comments
avatar
Guru

Nice! I had made something similar a few months back so this might be my opportunity to share that as well https://github.com/jdye64/PhoenixRESTServer-Client

avatar
New Contributor

Microsoft released a preview of their .NET ProtoBuf client for Phoenix (hdinsight-phoenix-sharp) on Nuget.org in June 2016. I've found it worked well against my HDP 2.4 (HBASE 1.1.2 / Phoenix 4.4) cluster, but the API was unfamiliar to me. As I learned it, I implemented a System.Data IDbConnection, IDbCommand et al wrapper API around it, named Garuda.Data. It makes access to Phoenix from .NET code feel almost like SqlConnection, SqlCommand, etc - Check it out on Nuget: https://www.nuget.org/packages/Garuda.Data/

Blog post on the topic: http://dwdii.github.io/2016/08/13/Garuda.Data-Apache-Phoenix-For-.NET-Developers.html

Garuda.Data Github Repo: https://github.com/dwdii/GarudaUtil