Ejemplo n.º 1
0
        /// <inheritdoc />
        public async Task <bool> ReadNextBatch()
        {
            EnsureNotDisposed();

            HttpResponseMessage response;

            using (var httpClient = CreateHttpClient())
            {
                response = await httpClient.GetAsync(string.Empty);
            }

            if (!response.IsSuccessStatusCode)
            {
                throw new HBaseException("HBase 返回了表示操作不成功的 HTTP 状态码:" + response.StatusCode);
            }

            if (response.StatusCode == HttpStatusCode.NoContent)
            {
                // 返回的 HTTP 状态码为 204 No Content
                _batch = null;
                return(false);
            }

            var responseBody = await response.Content.ReadAsStringAsync();

            var responseObject = HBaseSerializationHelper.DeserializeObject <HBaseRowWrapper>(responseBody);

            _batch = responseObject.Rows;

            return(true);
        }
Ejemplo n.º 2
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.º 3
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.º 4
0
        public void ColumnDeserialization()
        {
            var json   = "\"ZmFtaWx5OmNvbHVtbg==\"";
            var column = HBaseSerializationHelper.DeserializeObject <HBaseColumn>(json);

            Assert.Equal("family", column.ColumnFamilyName);
            Assert.Equal("column", column.ColumnName);
        }
Ejemplo n.º 5
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.º 6
0
        public void CellDeserialization()
        {
            var json = "{\"column\":\"ZmFtaWx5OmNvbHVtbg==\",\"timestamp\":325,\"$\":\"aGVsbG8gd29ybGQ=\"}";
            var cell = HBaseSerializationHelper.DeserializeObject <HBaseCell>(json);

            Assert.Equal(new HBaseColumn("family", "column"), cell.Column, new HBaseColumnEqualityComparer());
            Assert.Equal(325, cell.Timestamp);
            Assert.Equal(Encoding.UTF8.GetBytes("hello world"), cell.Data);
        }
Ejemplo n.º 7
0
        public void ColumnCollectionDeserialization()
        {
            var json    = "[\"ZmFtaWx5MTpjb2x1bW4x\",\"ZmFtaWx5Mjpjb2x1bW4y\",\"ZmFtaWx5Mzpjb2x1bW4z\"]";
            var columns = HBaseSerializationHelper.DeserializeObject <HBaseColumn[]>(json);

            Assert.Equal(new []
            {
                new HBaseColumn("family1", "column1"),
                new HBaseColumn("family2", "column2"),
                new HBaseColumn("family3", "column3"),
            }, columns, new HBaseColumnEqualityComparer());
        }
Ejemplo n.º 8
0
        /// <inheritdoc />
        public async Task <HBaseRow> Find(string tableName, string rowKey, HBaseFindOptions options = null)
        {
            if (tableName == null)
            {
                throw new ArgumentNullException(nameof(tableName));
            }
            if (rowKey == null)
            {
                throw new ArgumentNullException(nameof(rowKey));
            }

            var urlBuilder = new StringBuilder($"{tableName}/{rowKey}");

            if (options?.Column != null)
            {
                urlBuilder.AppendFormat("/{0}", options.Column);
            }

            if (options?.Timestamp != null)
            {
                urlBuilder.AppendFormat("/{0}", options.Timestamp);
            }

            if (options?.NumberOfVersions != null)
            {
                urlBuilder.AppendFormat("?v={0}", options.NumberOfVersions);
            }

            var url = urlBuilder.ToString();

            HttpResponseMessage response;

            using (var httpClient = CreateHttpClient())
            {
                response = await httpClient.GetAsync(url);
            }

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

            var responseBody = await response.Content.ReadAsStringAsync();

            var data = HBaseSerializationHelper.DeserializeObject <HBaseRowWrapper>(responseBody);

            if (data.Rows == null || data.Rows.Count == 0)
            {
                return(null);
            }

            return(data.Rows[0]);
        }
Ejemplo n.º 9
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.º 10
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.º 11
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.º 12
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.º 13
0
        public void CellCollectionDeserialization()
        {
            var json  = "[{\"column\":\"ZmFtaWx5MTpjb2x1bW4x\",\"timestamp\":325,\"$\":\"aGVsbG8gd29ybGQ=\"},{\"column\":\"ZmFtaWx5Mjpjb2x1bW4y\",\"timestamp\":1015,\"$\":\"Z29vZGJ5ZSB3b3JsZA==\"}]";
            var cells = HBaseSerializationHelper.DeserializeObject <HBaseCell[]>(json);

            Assert.Equal(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")
                }
            }, cells, new HBaseCellEqualityComparer());
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
0
        public void RowDeserialization()
        {
            var json = "{\"key\":\"cm93IGtleQ==\",\"Cell\":[{\"column\":\"ZmFtaWx5MTpjb2x1bW4x\",\"timestamp\":325,\"$\":\"aGVsbG8gd29ybGQ=\"},{\"column\":\"ZmFtaWx5Mjpjb2x1bW4y\",\"timestamp\":1015,\"$\":\"Z29vZGJ5ZSB3b3JsZA==\"}]}";
            var row  = HBaseSerializationHelper.DeserializeObject <HBaseRow>(json);

            var expectedRow = new HBaseRow {
                Key = "row key"
            };

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

            Assert.Equal(expectedRow, row, new HBaseRowEqualityComparer());
        }