using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using System.Net;
using System.Threading;

namespace Baosight.HBase
{
    class TOKEN
    {
        public string token { get; set; }
        public string errcode { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {

            HbaseUnit hbase = new HbaseUnit("admin@crvm", "admin", "http://10.25.27.53/otsrest/api/");

            //##Create file record
            //byte[] data_file_in = hbase.File2Bytes("D:\\mp3\\Taylor Swift - Out Of The Woods.mp3");

            //for (int i = 0; i < 10; i++)
            //{
                
            //    string retString = hbase.PostBytes("record/file/T_CYC_SHAKE?key="+i.ToString()+".mp3&column=mp3", data_file_in);//huangming_mob是表名
            //    Console.WriteLine("up load file>{0}:{1}\n",i, retString);
            //}

            //for (int i = 0; i < 10; i++)
            //{
            //    //##Get file record
            //    byte[] data_file_out = hbase.GetBytes("record/file/T_CYC_SHAKE?key=" + i.ToString() + ".mp3&column=mp3");//huangming_mob是表名

            //    hbase.Bytes2File(data_file_out, "D:\\mp3\\" + i.ToString() + ".mp3");
            //    Console.WriteLine("down load file>{0}:{1}\n", i, i.ToString() + ".mp3");
            //}
            Console.WriteLine("start ");
            for (int i = 0; i < 10; i++)
            {
               
                Thread t = new Thread(new ThreadStart(delegate()
                {
                    //HbaseUnit hbase = new HbaseUnit("admin@crvm", "admin", "http://10.25.27.53/otsrest/api/");
                    //##Get file record
                    byte[] data_file_out = hbase.GetBytes("record/file/T_CYC_SHAKE?key=" + i.ToString() + ".mp3&column=mp3");//huangming_mob是表名

                    hbase.Bytes2File(data_file_out, "D:\\mp3\\" + i.ToString() + ".mp3");
                    Console.WriteLine("down load file>{0}:{1}\n", i, i.ToString() + ".mp3");

                }));
                t.Start();
            }
            Console.WriteLine("end ");

            

           
            ////##Create table
            ////String create_table = "{\"compression\":\"snappy\",\"max_versions\":1,\"description\":\"\",\"mob_enabled\":false,\"mob_threshold\":0}";            
            //StringWriter sw = new StringWriter();
            //JsonWriter writer = new JsonTextWriter(sw);
            //writer.WriteStartObject();
            //writer.WritePropertyName("description");
            //writer.WriteValue("");
            //writer.WritePropertyName("compression");
            //writer.WriteValue("snappy");
            //writer.WritePropertyName("max_versions");
            //writer.WriteValue(1);
            //writer.WritePropertyName("mob_enabled");
            //writer.WriteValue(false);
            //writer.WritePropertyName("mob_threshold");
            //writer.WriteValue(0);
            //writer.WriteEndObject();
            //writer.Flush();
            //String create_table = sw.GetStringBuilder().ToString();

            //string retString = hbase.Post("table/huangming", create_table);//huangming是表名
            //Console.WriteLine("\nC#======>{0}\n", retString);
            //retString = hbase.Get("table/huangming");
            //Console.WriteLine("\nC#======>{0}\n", retString);

            ////##Create normal record
            ////String create_record = "{\"records\":[ {\"id\":\"10000\", \"huang\":\"abc0\"} ] }";
            //StringWriter sw_rec = new StringWriter();
            //JsonWriter writer_rec = new JsonTextWriter(sw_rec);
            //writer_rec.WriteStartObject();
            //writer_rec.WritePropertyName("records");
            //writer_rec.WriteStartArray();

            //writer_rec.WriteStartObject();
            //writer_rec.WritePropertyName("id");
            //writer_rec.WriteValue("10000");
            //writer_rec.WritePropertyName("huang");
            //writer_rec.WriteValue("abc0");
            //writer_rec.WriteEndObject();

            //writer_rec.WriteEndArray();
            //writer_rec.WriteEndObject();
            //writer_rec.Flush();
            //String create_record = sw_rec.GetStringBuilder().ToString();
            //retString = hbase.Post("record/huangming", create_record);//huangming是表名
            //Console.WriteLine("\nC#======>{0}\n", retString);

            ////##Get normal record
            //retString = hbase.Get("record/huangming?key=10000");
            //Console.WriteLine("\nC#======>{0}\n", retString);

            //retString = hbase.Get("record/huangming?keyexp=*");
            //Console.WriteLine("\nC#======>{0}\n", retString);

            ////##Update normal record
            ////String update_record = "{\"records\":[ {\"id\":\"10000\", \"huang\":\"abc0_update\"} ] }";
            //StringWriter sw_rec2 = new StringWriter();
            //JsonWriter writer_rec2 = new JsonTextWriter(sw_rec2);
            //writer_rec2.WriteStartObject();
            //writer_rec2.WritePropertyName("records");
            //writer_rec2.WriteStartArray();

            //writer_rec2.WriteStartObject();
            //writer_rec2.WritePropertyName("id");
            //writer_rec2.WriteValue("10000");
            //writer_rec2.WritePropertyName("huang");
            //writer_rec2.WriteValue("abc0_update");
            //writer_rec2.WriteEndObject();

            //writer_rec2.WriteEndArray();
            //writer_rec2.WriteEndObject();
            //writer_rec2.Flush();
            //String update_record = sw_rec2.GetStringBuilder().ToString();
            //retString = hbase.Post("record/huangming", update_record);//huangming是表名
            //Console.WriteLine("\nC#======>{0}\n", retString);
            //retString = hbase.Get( "record/huangming?key=10000");
            //Console.WriteLine("\nC#======>{0}\n", retString);

            ////##Delete normal record
            //retString = hbase.Delete("record/huangming?key=10000");
            //Console.WriteLine("\nC#======>{0}\n", retString);
            //retString = hbase.Get("record/huangming?key=10000");
            //Console.WriteLine("\nC#======>{0}\n", retString);

            ////##Update table
            ////String update_table = "{\"description\": \"hello\"}";
            //StringWriter sw2 = new StringWriter();
            //JsonWriter writer2 = new JsonTextWriter(sw2);
            //writer2.WriteStartObject();
            //writer2.WritePropertyName("description");
            //writer2.WriteValue("hello");
            //writer2.WriteEndObject();
            //writer2.Flush();
            //String update_table = sw2.GetStringBuilder().ToString();
            //retString = hbase.Put("table/huangming", update_table);//huangming是表名
            //Console.WriteLine("\nC#======>{0}\n", retString);
            //retString = hbase.Get("table/huangming");
            //Console.WriteLine("\nC#======>{0}\n", retString);

            ////##Delete table
            //retString = hbase.Delete("table/huangming");//huangming是表名
            //Console.WriteLine("\nC#======>{0}\n", retString);
            //retString = hbase.Get("table/huangming");
            //Console.WriteLine("\nC#======>{0}\n", retString);

            ////##Create table with mob
            ////String create_table_mob = "{\"compression\":\"snappy\",\"max_versions\":1,\"description\":\"\",\"mob_enabled\":true,\"mob_threshold\":100}";            
            //StringWriter sw_mob = new StringWriter();
            //JsonWriter writer_mob = new JsonTextWriter(sw_mob);
            //writer_mob.WriteStartObject();
            //writer_mob.WritePropertyName("description");
            //writer_mob.WriteValue("");
            //writer_mob.WritePropertyName("compression");
            //writer_mob.WriteValue("snappy");
            //writer_mob.WritePropertyName("max_versions");
            //writer_mob.WriteValue(1);
            //writer_mob.WritePropertyName("mob_enabled");
            //writer_mob.WriteValue(true);
            //writer_mob.WritePropertyName("mob_threshold");
            //writer_mob.WriteValue(100);
            //writer_mob.WriteEndObject();
            //writer_mob.Flush();
            //String create_table_mob = sw_mob.GetStringBuilder().ToString();
            //retString = hbase.Post("table/huangming_mob", create_table_mob);//huangming_mob是表名
            //Console.WriteLine("\nC#======>{0}\n", retString);
            //retString = hbase.Get("table/huangming_mob");
            //Console.WriteLine("\nC#======>{0}\n", retString);

            ////##Create file record
            //byte[] data_file_in = hbase.File2Bytes("D:\\mp3\\Taylor Swift - Out Of The Woods.mp3");
            //retString = hbase.PostBytes("record/file/huangming_mob?key=away.mp3&column=mp3", data_file_in);//huangming_mob是表名
            //Console.WriteLine("\nC#======>{0}\n", retString);

            ////##Get file record
            //byte[] data_file_out = hbase.GetBytes("record/file/huangming_mob?key=away.mp3&column=mp3");//huangming_mob是表名
            //hbase.Bytes2File(data_file_out, "D:\\mp3\\Out Of The Woods.mp3");

            //##Delete table with mob
            //retString = hbase.Delete("table/huangming_mob");//huangming_mob是表名
            //Console.WriteLine("\nC#======>{0}\n", retString);
            //retString = hbase.Get("table/huangming_mob");
            //Console.WriteLine("\nC#======>{0}\n", retString);

            Console.ReadLine();
        }

        
    }
}
