mutateRow() public method

public mutateRow ( byte tableName, byte row, List mutations ) : void
tableName byte
row byte
mutations List
return void
Example #1
0
        protected void HBaseMutateImage(string rowKey, byte[] image)
        {
            var socket    = new TSocket("172.24.2.144", 9090);
            var transport = new TBufferedTransport(socket);
            var protocol  = new TBinaryProtocol(transport);

            Hbase.Client hc = new Hbase.Client(protocol);
            transport.Open();

            //List<byte[]> tableNames = hc.getTableNames();

            byte[] table  = Encoding.UTF8.GetBytes("FiservImages");
            byte[] row    = Encoding.UTF8.GetBytes(rowKey);
            byte[] column = Encoding.UTF8.GetBytes("ImageData:Image");

            List <Mutation> mutation = new List <Mutation>();
            Mutation        m        = new Mutation();

            m.Column     = column;
            m.Value      = image;
            m.IsDelete   = false;
            m.WriteToWAL = true;
            mutation.Add(m);

            hc.mutateRow(table, row, mutation, null);
        }
    /// <summary>
    /// 删除单元数据,封装方法
    /// </summary>
    /// <param name="table"></param>
    /// <param name="rowKey"></param>
    /// <param name="writeToWal"></param>
    /// <param name="columns"></param>
    /// <param name="attributes"></param>
    /// <param name="client"></param>
    public void DeleteCells(string table, string rowKey, bool writeToWal, List <string> columns, Dictionary <string, string> attributes, Hbase.Client client)
    {
        byte[] tableName = table.ToUTF8Bytes();
        byte[] row       = rowKey.ToUTF8Bytes();


        Dictionary <byte[], byte[]> encodedAttributes = new Dictionary <byte[], byte[]>();

        if (attributes != null)
        {
            foreach (var item in attributes)
            {
                encodedAttributes.Add(item.Key.ToUTF8Bytes(), item.Value.ToUTF8Bytes());
            }
        }
        List <Mutation> mutations = new List <Mutation>();

        foreach (string column in columns)
        {
            Mutation mutation = new Mutation();
            mutation.IsDelete   = true;
            mutation.WriteToWAL = writeToWal;
            mutation.Column     = column.ToUTF8Bytes();
            mutations.Add(mutation);
        }
        client.mutateRow(tableName, row, mutations, encodedAttributes);
    }
    /// <summary>
    /// 数据库表名,及指定key的示例
    /// </summary>
    /// <param name="tabname"></param>
    /// <param name="key"></param>
    public void 当前创建表示例(string tabname, Hbase.Client client)
    {
        string key  = "示例数据_Key";
        var    list = new List <Mutation>();

        foreach (var item in txt列名集合.Text.Split(','))
        {
            //创建列
            if (!string.IsNullOrEmpty(item))
            {
                string coln = item;
                if (!coln.Contains(":"))
                {
                    coln = item + ":" + "示例";
                }

                var col = new Mutation()
                {
                    Column = coln.ToUTF8Bytes(), Value = ("abcdef" + item).ToUTF8Bytes()
                };

                list.Add(col);
            }
        }
        client.mutateRow(tabname.ToUTF8Bytes(), key.ToUTF8Bytes(), list, new Dictionary <byte[], byte[]>());


        //显示示例数据
        显示示例数据(tabname, key, client);
    }
Example #4
0
        static void Main(string[] args)
        {
            // Connection
            var socket    = new TSocket("hdp1.localdomain", 9090);
            var transport = new TBufferedTransport(socket);
            var protocol  = new TBinaryProtocol(transport);

            Hbase.Client hba = new Hbase.Client(protocol);
            transport.Open();

            // Get table names
            Console.WriteLine("<-GET LIST OF TABLES->");
            var tableNames = hba.getTableNames();

            foreach (var tableName in tableNames)
            {
                Console.WriteLine(Encoding.UTF8.GetString(tableName, 0, tableName.Length));
            }

            // Insert rows
            Console.WriteLine("<-INSERT->");
            Mutation _mutation = new Mutation();

            _mutation.IsDelete = false;
            _mutation.Column   = Encoding.UTF8.GetBytes("image:bodyimage");
            _mutation.Value    = Encoding.UTF8.GetBytes("newnew image 2.jpg");

            hba.mutateRow(Encoding.UTF8.GetBytes("blogposts"), Encoding.UTF8.GetBytes("post1"), new List <Mutation> {
                _mutation
            }, null);

            // Finished
            Console.WriteLine("<-FINISHED->");
            Console.ReadKey();
        }
Example #5
0
    /// <summary>
    /// 删除单元格数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button5_Click(object sender, EventArgs e)
    {
        string table = "abc";
        string key   = "aaaK";

        TTransport transport = null;

        try
        {
            //192.168.2.111:60010
            //实例化Socket连接
            transport = new TSocket("192.168.2.111", 9090);
            //实例化一个协议对象
            TProtocol tProtocol = new TBinaryProtocol(transport);
            //实例化一个Hbase的Client对象
            var client = new Hbase.Client(tProtocol);
            //打开连接
            transport.Open();

            foreach (var item in System.Linq.Enumerable.Range(1, 10))
            {
                string skey = (key + "_" + item);
                string scol = ("你好中文:" + item);
                string svla = (Guid.NewGuid().ToString());
                ///////////////删除单元格数据
                byte[] tableName = table.ToUTF8Bytes();
                byte[] row       = skey.ToUTF8Bytes();
                Dictionary <byte[], byte[]> encodedAttributes = new Dictionary <byte[], byte[]>();

                client.mutateRow(tableName, row, new List <Mutation>()
                {
                    new Mutation()
                    {
                        IsDelete = true, Column = scol.ToUTF8Bytes()
                    }
                }, encodedAttributes);

                Response.Write(string.Format("删除单元格数据:{0},数据{{ RowKey:{1},列:{2} }}<br/>", table, skey, scol));
            }
        }


        finally
        {
            if (transport != null)
            {
                transport.Close();
            }
        }
    }
Example #6
0
    /// <summary>
    /// 添加数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button3_Click(object sender, EventArgs e)
    {
        string table = "abc";
        string key   = "aaaK";

        TTransport transport = null;

        try
        {
            //192.168.2.111:60010
            //实例化Socket连接
            transport = new TSocket("192.168.2.111", 9090);
            //实例化一个协议对象
            TProtocol tProtocol = new TBinaryProtocol(transport);
            //实例化一个Hbase的Client对象
            var client = new Hbase.Client(tProtocol);
            //打开连接
            transport.Open();

            foreach (var item in System.Linq.Enumerable.Range(1, 10))
            {
                string skey  = (key + "_" + item);
                string scol  = ("你好中文:" + item);
                string scol2 = ("你好中文:单元格2" + item);
                string svla  = (Guid.NewGuid().ToString());
                client.mutateRow("abc".ToUTF8Bytes(), skey.ToUTF8Bytes(), new List <Mutation>()
                {
                    new Mutation()
                    {
                        Column = scol.ToUTF8Bytes(), Value = svla.ToUTF8Bytes()
                    },
                    new  Mutation()
                    {
                        Column = scol2.ToUTF8Bytes(), Value = svla.ToUTF8Bytes()
                    }
                }, new Dictionary <byte[], byte[]>());
                Response.Write(string.Format("添加tab:{0},数据{{ RowKey:{1} ,列{2},数据{3},列{4},数据{5} }}<br/>", table, skey, scol, svla, scol2, svla));
            }
        }


        finally
        {
            if (transport != null)
            {
                transport.Close();
            }
        }
    }
    /// <summary>
    /// 一次加入多行数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void 一次加入多行数据_Click(object sender, EventArgs e)
    {
        //请在步骤2输入表名
        if (string.IsNullOrEmpty(txt表名.Text))
        {
            lab添加数据状态.Text = "请在步骤2输入表名";

            return;
        }

        string table = txt表名.Text;

        byte[] tableName = table.ToUTF8Bytes();


        TTransport transport = null;

        try
        {
            //192.168.2.111:60010
            //实例化Socket连接
            transport = new TSocket("192.168.2.111", 9090);
            //实例化一个协议对象
            TProtocol tProtocol = new TBinaryProtocol(transport);
            //实例化一个Hbase的Client对象
            var client = new Hbase.Client(tProtocol);
            //打开连接
            transport.Open();


            //获取所有列



            #region 具体功能代码
            //批量提交
            var bat = new List <BatchMutation>();
            //value:列数据集合 目前是单列
            #region 生成批量提交的数据集合
            var mu = new Mutation
            {
                /////////////////////////////////////////////////////////注意这里的列名表里必须有.
                Column = System.Text.Encoding.UTF8.GetBytes("Data:Collections"),
                Value  = Guid.NewGuid().ToByteArray()
            };
            string row;

            row = string.Format("{0:d15}{1}{2}", 99999, "ABCDEF", DateTime.Now.ToString("yyyyMMddHHmmssfff"));

            Dictionary <byte[], byte[]> encodedAttributes = new Dictionary <byte[], byte[]>();

            var batch = new BatchMutation
            {
                Row       = System.Text.Encoding.UTF8.GetBytes(row),
                Mutations = new List <Mutation>()
                {
                    mu
                }
            };
            #endregion
            //添加到集合中
            bat.Add(batch);

            client.mutateRow(tableName, bat[0].Row, bat[0].Mutations, encodedAttributes);
            client.mutateRows(tableName, bat, encodedAttributes);

            #endregion
        }


        finally
        {
            if (transport != null)
            {
                transport.Close();
            }
        }
    }
    protected void 添加数据_Click(object sender, EventArgs e)
    {
        lab添加数据状态.Text = string.Empty;
        //请在步骤2输入表名
        if (string.IsNullOrEmpty(txt表名.Text))
        {
            lab添加数据状态.Text = "请在步骤2输入表名";

            return;
        }

        string table = txt表名.Text;

        TTransport transport = null;

        try
        {
            //192.168.2.111:60010
            //实例化Socket连接
            transport = new TSocket("192.168.2.111", 9090);
            //实例化一个协议对象
            TProtocol tProtocol = new TBinaryProtocol(transport);
            //实例化一个Hbase的Client对象
            var client = new Hbase.Client(tProtocol);
            //打开连接
            transport.Open();

            var id = client.scannerOpenWithScan(Encoding.UTF8.GetBytes(table), new TScan(), new Dictionary <byte[], byte[]>());
            List <TRowResult> reslut;
            reslut = client.scannerGet(id);

            List <string> cols = new List <string>();
            //创建表头
            foreach (var item in reslut)
            {
                //所有列
                foreach (var xaa in item.Columns)
                {
                    string col = xaa.Key.ToUTF8String();
                    cols.Add(col);
                }
            }


            foreach (var item in System.Linq.Enumerable.Range(1, 30))
            {
                List <Mutation> mus  = new List <Mutation>();
                List <string>   info = new List <string>();
                string          skey = "_" + item;
                string          strx = string.Empty;
                foreach (var col in cols)
                {
                    string scol = col;
                    string svla = (Guid.NewGuid().ToString());
                    mus.Add(new Mutation()
                    {
                        Column = scol.ToUTF8Bytes(), Value = svla.ToUTF8Bytes()
                    });
                    strx = string.Format("col:{0},val:{1},key{2}", scol, svla, skey);
                    info.Add(strx);
                }
                client.mutateRow(table.ToUTF8Bytes(), skey.ToUTF8Bytes(), mus, new Dictionary <byte[], byte[]>());


                lab添加数据状态.Text += string.Format("添加tab:{0},数据{{ {1} }}<br/>", table, string.Join(",", info));
            }
        }


        finally
        {
            if (transport != null)
            {
                transport.Close();
            }
        }
    }
Example #9
0
    protected void Button1_Click(object sender, EventArgs e)
    {
        TTransport transport = null;

        try
        {
            //192.168.2.111:60010
            //实例化Socket连接
            transport = new TSocket("192.168.2.111", 9090);
            //实例化一个协议对象
            TProtocol tProtocol = new TBinaryProtocol(transport);
            //实例化一个Hbase的Client对象

            var client = new Hbase.Client(tProtocol);



            //打开连接

            transport.Open();

            //判断 表是不是存在
            if (!client.getTableNames().Select(p => p.ToUTF8String()).Contains("abc"))
            {
                //创建数据表
                client.createTable("abc".ToUTF8Bytes(), new List <ColumnDescriptor>()
                {
                    new ColumnDescriptor()
                    {
                        Name = "你好中文".ToUTF8Bytes(), BloomFilterVectorSize = 30,
                    },
                    new ColumnDescriptor()
                    {
                        Name = "aa".ToUTF8Bytes(), BloomFilterVectorSize = 30,
                    }
                });
            }

            //向表里加数据
            #region 向表里加数据
            //代码错误.不会用
            //client.m("abc".ToUTF8Bytes(), new List<BatchMutation>() {
            //     new BatchMutation() { Mutations= new List<Mutation>() {
            //                        new Mutation() {  Column="你好中文".ToUTF8Bytes(), Value="我是,".ToUTF8Bytes() },
            //                        new Mutation() {  Column="aa".ToUTF8Bytes(), Value="我是,111".ToUTF8Bytes() },
            //                        new Mutation() {  Column="你好中文".ToUTF8Bytes(), Value="我是,Habse".ToUTF8Bytes() },

            //     }
            //     ,  Row = "aaaa_1".ToUTF8Bytes(),


            //      }

            //}, new Dictionary<byte[], byte[]>());
            client.mutateRow("abc".ToUTF8Bytes(), "aaaa_1".ToUTF8Bytes(), new List <Mutation>()
            {
                new Mutation()
                {
                    Column = "你好中文:11".ToUTF8Bytes(), Value = "abcdef".ToUTF8Bytes()
                }
            }, new Dictionary <byte[], byte[]>());

            #endregion


            //遍历结果集
            List <string> sss = new List <string>();

            //获取表名:
            foreach (var item in client.getTableNames())
            {
                var strs = System.Text.ASCIIEncoding.ASCII.GetString(item);
                sss.Add("表名:" + strs);
                //根据表名,RowKey名来获取结果集



                List <TRowResult> reslut = client.getRow(Encoding.UTF8.GetBytes(strs), Encoding.UTF8.GetBytes("aaaa_1"), null);

                foreach (var key in reslut)
                {
                    sss.Add(string.Format("&nbsp;&nbsp;RowKey:\n{0}", Encoding.UTF8.GetString(key.Row)));
                    //打印Qualifier和对应的Value
                    foreach (var k in key.Columns)
                    {
                        sss.Add(string.Format("&nbsp;&nbsp;&nbsp;Family:Qualifier:" + "\n" + Encoding.UTF8.GetString(k.Key)));
                        sss.Add(string.Format("&nbsp;&nbsp;&nbsp;Value:" + Encoding.UTF8.GetString(k.Value.Value)));
                    }
                }
            }
            GridView1.DataSource = sss;
            GridView1.DataBind();
        }

        catch (Exception exxx)

        {
            Response.Write(exxx);
        }

        finally

        {
            if (null != transport)

            {
                transport.Close();
            }
        }
    }
Example #10
0
        protected void HBaseMutateImage(string rowKey, byte[] image)
        {
            var socket = new TSocket("172.24.2.144", 9090);
            var transport = new TBufferedTransport(socket);
            var protocol = new TBinaryProtocol(transport);
            Hbase.Client hc = new Hbase.Client(protocol);
            transport.Open();

            //List<byte[]> tableNames = hc.getTableNames();

            byte[] table = Encoding.UTF8.GetBytes("FiservImages");
            byte[] row = Encoding.UTF8.GetBytes(rowKey);
            byte[] column = Encoding.UTF8.GetBytes("ImageData:Image");

            List<Mutation> mutation = new List<Mutation>();
            Mutation m = new Mutation();
            m.Column = column;
            m.Value = image;
            m.IsDelete = false;
            m.WriteToWAL = true;
            mutation.Add(m);

            hc.mutateRow(table, row, mutation, null);
        }