Ejemplo n.º 1
0
        public void RowSerialization()
        {
            var row = new HBaseRow
            {
                Key = "row key"
            };

            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family1", "column1"),
                Timestamp = 325,
                Data      = Encoding.UTF8.GetBytes("hello world")
            });
            row.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family2", "column2"),
                Timestamp = 1015,
                Data      = Encoding.UTF8.GetBytes("goodbye world")
            });

            var json        = HBaseSerializationHelper.SerializeObject(row);
            var compactJson = Regex.Replace(json, @"\s+", string.Empty);

            Assert.Equal("{\"key\":\"cm93IGtleQ==\",\"Cell\":[{\"column\":\"ZmFtaWx5MTpjb2x1bW4x\",\"timestamp\":325,\"$\":\"aGVsbG8gd29ybGQ=\"},{\"column\":\"ZmFtaWx5Mjpjb2x1bW4y\",\"timestamp\":1015,\"$\":\"Z29vZGJ5ZSB3b3JsZA==\"}]}",
                         compactJson);
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public async Task Add(string tableName, IEnumerable <HBaseRow> rows)
        {
            if (tableName == null)
            {
                throw new ArgumentNullException(nameof(tableName));
            }
            if (rows == null)
            {
                throw new ArgumentNullException(nameof(rows));
            }

            var wrapper = new HBaseRowWrapper {
                Rows = rows.ToList()
            };
            var json = HBaseSerializationHelper.SerializeObject(wrapper);

            var url     = $"{tableName}/row_key";
            var content = HttpRequestContentFactory.CreateJsonContent(json);

            HttpResponseMessage response;

            using (var httpClient = CreateHttpClient())
            {
                response = await httpClient.PutAsync(url, content);
            }

            if (!response.IsSuccessStatusCode)
            {
                throw new HBaseException("HBase REST API 返回了表示错误的 HTTP 状态码:" + response.StatusCode);
            }
        }
Ejemplo n.º 3
0
        public void ColumnSerialization()
        {
            var column = new HBaseColumn("family", "column");

            var json        = HBaseSerializationHelper.SerializeObject(column);
            var compactJson = Regex.Replace(json, @"\s+", string.Empty);

            Assert.Equal("\"ZmFtaWx5OmNvbHVtbg==\"", compactJson);
        }
Ejemplo n.º 4
0
        public void CellSerialization()
        {
            var cell = new HBaseCell
            {
                Column    = new HBaseColumn("family", "column"),
                Timestamp = 325,
                Data      = Encoding.UTF8.GetBytes("hello world")
            };

            var json        = HBaseSerializationHelper.SerializeObject(cell);
            var compactJson = Regex.Replace(json, @"\s+", string.Empty);

            Assert.Equal("{\"column\":\"ZmFtaWx5OmNvbHVtbg==\",\"timestamp\":325,\"$\":\"aGVsbG8gd29ybGQ=\"}",
                         compactJson);
        }
Ejemplo n.º 5
0
        public void ColumnCollectionSerialization()
        {
            var columns = new[]
            {
                new HBaseColumn("family1", "column1"),
                new HBaseColumn("family2", "column2"),
                new HBaseColumn("family3", "column3")
            };

            var json        = HBaseSerializationHelper.SerializeObject(columns);
            var compactJson = Regex.Replace(json, @"\s+", string.Empty);

            Assert.Equal("[\"ZmFtaWx5MTpjb2x1bW4x\",\"ZmFtaWx5Mjpjb2x1bW4y\",\"ZmFtaWx5Mzpjb2x1bW4z\"]",
                         compactJson);
        }
Ejemplo n.º 6
0
        /// <inheritdoc />
        public async Task <IHBaseScanner> OpenScanner(string tableName, HBaseScannerCreationOptions options = null)
        {
            if (tableName == null)
            {
                throw new ArgumentNullException(nameof(tableName));
            }

            // 构造请求负载
            var contentJson = string.Empty;

            if (options != null)
            {
                contentJson = HBaseSerializationHelper.SerializeObject(options);
            }

            // 发送 HTTP 请求
            var url     = $"{tableName}/scanner";
            var content = HttpRequestContentFactory.CreateJsonContent(contentJson);

            HttpResponseMessage response;

            using (var httpClient = CreateHttpClient())
            {
                response = await httpClient.PutAsync(url, content);
            }

            if (!response.IsSuccessStatusCode)
            {
                throw new HBaseException("HBase REST API 返回了表示错误的 HTTP 状态码:" + response.StatusCode);
            }

            // 从响应的 Location 头部拿到 Scanner 的接入点
            if (!response.Headers.TryGetValues("Location", out var scannerEndpoints))
            {
                throw new HBaseException("HBase REST API 没有返回任何有效的 Scanner 终结点。");
            }

            var scannerEpt = scannerEndpoints.FirstOrDefault();

            if (scannerEpt == null)
            {
                throw new HBaseException("HBase REST API 没有返回任何有效的 Scanner 终结点。");
            }

            return(new DefaultHBaseScanner(scannerEpt, _httpClientFactory));
        }
Ejemplo n.º 7
0
        public void RowCollectionSerialization()
        {
            var row1 = new HBaseRow
            {
                Key = "row key 1"
            };

            row1.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family1", "column1"),
                Timestamp = 325,
                Data      = Encoding.UTF8.GetBytes("hello world")
            });
            row1.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family2", "column2"),
                Timestamp = 1015,
                Data      = Encoding.UTF8.GetBytes("goodbye world")
            });

            var row2 = new HBaseRow
            {
                Key = "row key 2"
            };

            row2.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family3", "column3"),
                Timestamp = 76,
                Data      = Encoding.UTF8.GetBytes("你好世界")
            });
            row2.Cells.Add(new HBaseCell
            {
                Column    = new HBaseColumn("family4", "column4"),
                Timestamp = 1111,
                Data      = Encoding.UTF8.GetBytes("再见世界")
            });

            var json        = HBaseSerializationHelper.SerializeObject(new[] { row1, row2 });
            var compactJson = Regex.Replace(json, @"\s+", string.Empty);

            Assert.Equal("[{\"key\":\"cm93IGtleSAx\",\"Cell\":[{\"column\":\"ZmFtaWx5MTpjb2x1bW4x\",\"timestamp\":325,\"$\":\"aGVsbG8gd29ybGQ=\"},{\"column\":\"ZmFtaWx5Mjpjb2x1bW4y\",\"timestamp\":1015,\"$\":\"Z29vZGJ5ZSB3b3JsZA==\"}]},{\"key\":\"cm93IGtleSAy\",\"Cell\":[{\"column\":\"ZmFtaWx5Mzpjb2x1bW4z\",\"timestamp\":76,\"$\":\"5L2g5aW95LiW55WM\"},{\"column\":\"ZmFtaWx5NDpjb2x1bW40\",\"timestamp\":1111,\"$\":\"5YaN6KeB5LiW55WM\"}]}]",
                         compactJson);
        }
Ejemplo n.º 8
0
        public void CellCollectionSerialization()
        {
            var cells = new[]
            {
                new HBaseCell
                {
                    Column    = new HBaseColumn("family1", "column1"),
                    Timestamp = 325,
                    Data      = Encoding.UTF8.GetBytes("hello world")
                },
                new HBaseCell
                {
                    Column    = new HBaseColumn("family2", "column2"),
                    Timestamp = 1015,
                    Data      = Encoding.UTF8.GetBytes("goodbye world")
                }
            };

            var json        = HBaseSerializationHelper.SerializeObject(cells);
            var compactJson = Regex.Replace(json, @"\s+", string.Empty);

            Assert.Equal("[{\"column\":\"ZmFtaWx5MTpjb2x1bW4x\",\"timestamp\":325,\"$\":\"aGVsbG8gd29ybGQ=\"},{\"column\":\"ZmFtaWx5Mjpjb2x1bW4y\",\"timestamp\":1015,\"$\":\"Z29vZGJ5ZSB3b3JsZA==\"}]",
                         compactJson);
        }