This project aims to simplify the interaction between HBase and an application written in C#.
What can you do with this project:
- Map an object to a hbase row
- Map a column from a column family to a property
- Map an entire column family into a list of objects or a dictionary.
- Issue SCAN commands that return a list of objects or an object (With filters as delegates)
- Issue GET commands that return one or more objects.
- Issue PUT commands to persist objects into HBase rows
- Issue DELETE commands for row/columns removal.
This project is currently a work in progress and should NOT be used in a production environment.
NOTE: This project DOES NOT provide relational mapping!
- Create a configuration object and add the relevant information in it:
var configuration = new HBaseConfiguration() { ThriftHost = "localhost", ThriftPort = 9090 };
- Create the factory object and configure it with the above configuration object and the relevant mappings:
var factory = new HBaseSessionFactory(); factory.Configure(configuration); factory.Map(new UserConfig());
- Register the factory object as a singleton with your favorite DI container.
- Inject the factory as a dependency in your constructor by using the interface:
private readonly IHBaseSessionFactory _repoFactory;
public MyConsumerObject(IHBaseSessionFactory repoFactory)
{
_repoFactory = repoFactory;
}
- Use the repository like this:
private void MyDalMethod()
{
var repo = factory.BuildSession();
//Get command - single row
var user = repo.Get<User>().Row("my-row-key").SingleOrDefault();
//Get command - multiple rows
var users = repo.Get<User>().Rows("my-row-key-1","my-row-key-2","my-row-key-3").List();
// or
var sameUsers = repo.Get<User>().Row("my-row-key-1").Row("my-row-key-2").Row("my-row-key-3").List();
//Scan - no filters
var usersScan = repo.Scan<User>().List();
//Scan - ValueFilter NOTE: all of the filters are implemented as methods of x
var usersScanValueFilter = repo.Scan<User>().Filter(x=> x.ValueFilter().Value("Test Name")
.Comparator(Comparator.Binary)
.ComparisonOperator(ComparisonOperator.Equal)).List();
// Put - Single object
repo.Put<User>().Entity(user).Execute();
// Put - List
repo.Put<User>().Entities(users).Execute();
}
In order to map a c# object to a HBase row you need to create:
- The domain entity or entities. For example:
public class User
{
public virtual string Rowkey { get; set; }
public virtual bool IsActive { get; set; }
public virtual string Name { get; set; }
public virtual string Email { get; set; }
public virtual List<Permission> Permissions { get; set; }
public virtual List<Role> Roles { get; set; }
public override string ToString() => $"{Name} (Active: {IsActive})";
}
public class Permission
{
public virtual string Name { get; set; }
public virtual int Id { get; set; }
public override string ToString() => $"({Id}) {Name}";
}
public class Role
{
public virtual string Name { get; set; }
public virtual int Id { get; set; }
public override string ToString() => $"({Id}) {Name}";
}
- The mapping configuration(s):
If you want to map an entire row then create a new class that extends ClassMap like so:
public class UserConfig : ClassMap<User>
{
public UserConfig()
{
Table("ai_Users");
RowKey(x=> x.Email);
Property(x => x.Name).FromColumnFamily("u").WithColumn("Name");
Property(x => x.IsActive).FromColumnFamily("u").WithColumn("IsActive");
//Note: this transforms a column family to a IEnumerable<T> if you specify what property is the column name and what property is the column value.
Property(x => x.Roles).EntireCFAsObject("r", x => x.Name, x => x.Id);
Property(x => x.Permissions).EntireCFAsObject("p", x => x.Name, x => x.Id);
}
}