环境准备:在HBase shell中创建一个新表curd, 包含两个列族cfa和cfb:

create 'curd', 'cfa', 'cfb'

为行键修改/添加列族和值

使用put操作进行添加或修改数据

package demo;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

class HBaseAPI {
	private final Connection conn;
	private final Admin admin;

	public HBaseAPI() throws IOException {
		Configuration conf = HBaseConfiguration.create();
		conf.addResource("hbase-site.xml");
		conn = ConnectionFactory.createConnection(conf);
		admin = conn.getAdmin();
	}

	public void put(String tb, String row, String[][] cols) throws IOException {
		// 获取表的实例
		Table table = conn.getTable(TableName.valueOf(tb));
		// 修改或增加数据需要指定行键,还需要将行键转换为Bytes[]
		Put put = new Put(Bytes.toBytes(row));
		for (String[] col : cols) {
			// 添加时需要转换为Bytes[]
			// 参数为: 列族:列族描述, 数据
			put.addColumn(Bytes.toBytes(col[0]), Bytes.toBytes(col[1]), Bytes.toBytes(col[2]));
		}
		table.put(put);
		table.close();
	}

	// destructor
	public void close() throws IOException {
		admin.close();
		conn.close();
	}
}

public class Main {
	public static void main(String[] args) throws IOException {
		HBaseAPI hbase = new HBaseAPI();
        hbase.put("curd", "1001", new String[][] { { "cfa", "name", "张三" }, { "cfb", "age", "18" } }); // 先添加数据
		hbase.put("curd", "1001", new String[][] { { "cfa", "name", "111" }, { "cfb", "age", "128" } }); // 再将其修改
		hbase.close();
	}
}

put 修改数据前后

单行扫描

使用get操作获取一行数据,返回Result。通过遍历Result来查看

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

class HBaseAPI {
	...

	public HBaseAPI() throws IOException { ... }

	public void get(String tb, String row) throws IOException {
		Table table = conn.getTable(TableName.valueOf(tb));
		Get get = new Get(Bytes.toBytes(row));
		Result result = table.get(get);
		for (Cell cell : result.listCells()) {
            // result中有多个cell,每个cell包含行号,列族,属性和值
            // CellUtil.cloneRow(cell): 辅助类,获取cell对象中的各属性,最后将属性转换为String
			System.out.println("Row key: " + Bytes.toString(CellUtil.cloneRow(cell)) +
					"\tFamily: " + Bytes.toString(CellUtil.cloneFamily(cell)) +
					"\tQualifier: " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
					"\tValue: " + Bytes.toString(CellUtil.cloneValue(cell)));
		}
	}

	public void close() throws IOException { ... }
}

public class Main {
	public static void main(String[] args) throws IOException {
		HBaseAPI hbase = new HBaseAPI();
		hbase.get("curd", "1001");
		hbase.close();
	}
}

get

多行扫描

使用Scan进行多行扫描,返回ResultScanner,包含多个Result。

class HBaseAPI {
	...

	public HBaseAPI() throws IOException { ... }

    public void scan(String tb) throws IOException {
		Table table = conn.getTable(TableName.valueOf(tb));
		Scan scan = new Scan();
        // ResultScanner包含多个Result,可以遍历
		ResultScanner sc = table.getScanner(scan);
        // 还可以指定列族或者指定列族:属性
        // ResultScanner sc = table.getScanner("cfa".getBytes()); // 指定列族
        // ResultScanner sc = table.getScanner("cfa".getBytes(), 'age'); // 指定列族:属性
		for (Result result : sc) {
			for (Cell cell : result.listCells()) {
                // result中有多个cell,每个cell包含行号,列族,属性和值
                // CellUtil.cloneRow(cell): 辅助类,获取cell对象中的各属性,最后将属性转换为String
				System.out.println("Row key: " + Bytes.toString(CellUtil.cloneRow(cell)) +
						"\tFamily: " + Bytes.toString(CellUtil.cloneFamily(cell)) +
						"\tQualifier: " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
						"\tValue: " + Bytes.toString(CellUtil.cloneValue(cell)));
			}
		}
	}

	public void close() throws IOException { ... }
}

public class Main {
	public static void main(String[] args) throws IOException {
		HBaseAPI hbase = new HBaseAPI();
		hbase.scan("curd");
		hbase.close();
	}
}

scan

判断单行数据是否存在

使用TableName中的exists()判断Get对象是否存在

class HBaseAPI {
	...

	public HBaseAPI() throws IOException { ... }

    public void rowExist(String tb, String row) throws IOException {
		Table table = conn.getTable(TableName.valueOf(tb));
		Get get = new Get(row.getBytes());
		boolean ret = table.exists(get);
		System.out.println("Row exist: " + ret);
	}

	public void close() throws IOException { ... }
}

public class Main {
	public static void main(String[] args) throws IOException {
		HBaseAPI hbase = new HBaseAPI();
		hbase.rowExist("curd", "1001"); // true
		hbase.rowExist("curd", "1111"); // false
		hbase.close();
	}
}

删除数据

class HBaseAPI {
	...

	public HBaseAPI() throws IOException { ... }

	public void delRow(String tb, String row) throws IOException {
		Table table = conn.getTable(TableName.valueOf(tb));
		Delete del = new Delete(row.getBytes()); // 行必须指定
		table.delete(del); // 删除行
		table.close();
	}

	public void delCf(String tb, String row, String cf) throws IOException {
		Table table = conn.getTable(TableName.valueOf(tb));
		Delete del = new Delete(row.getBytes());  // 行必须指定
		del.addFamily(cf.getBytes()); // 覆盖列族
		table.delete(del); // 提交更改
		table.close();
	}

	public void delCfQual(String tb, String row, String cf, String qual) throws IOException {
		Table table = conn.getTable(TableName.valueOf(tb));
		Delete del = new Delete(row.getBytes());  // 行必须指定
        // 如果这里使用addColumn()则只会删除最新版本数据,使用addColumns()则全部删除
		del.addColumns(cf.getBytes(), qual.getBytes()); // 覆盖列族:属性
		table.delete(del); // 提交更改
		table.close();
	}

	// destructor
	public void close() throws IOException { ... }
}

public class Main {
	public static void main(String[] args) throws IOException { ... }
}