代码示例
有很多的代码是重复的,只是为形成记忆,见谅,
另外需要将 hbase-site.xml,hdfs-site.xml,core-site.xml三个文件放到Resources上目录中
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.coprocessor.Batch;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.util.Bytes;
import java.util.ArrayList;
import java.util.List;
public class HbaseApi_test_1 {
private static Connection hbaseConn;
private static Configuration hbaseConf;
//HBaseAdmin 提供了一个接口来管理 HBase 数据库的表信息
private static Admin hbaseAdmin;
/*** 静态构造,在调用静态方法前运行, 初始化连接对象 * */
static {
hbaseConf = HBaseConfiguration.create();
try {
hbaseConn = ConnectionFactory.createConnection(hbaseConf);
System.out.println("连接上了?" + !hbaseConn.isClosed());
hbaseAdmin = hbaseConn.getAdmin();
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
//****************************为某列的值 追加记录 ,在原有的value后面追加新的value, "a" + "bc" --> "abc"
public static void row_appendData() throws java.lang.Exception {
TableName tableName = TableName.valueOf("ns1:mytest_4");
//取得一个要操作的表
Table table = hbaseConn.getTable(tableName);
//设置要追加的行的rowkey
Append append = new Append(Bytes.toBytes("zhangshan"));
//为某列的值,追加新的值
append.add(Bytes.toBytes("course"), Bytes.toBytes("yuwen"), Bytes.toBytes(" 零蛋"));
append.add(Bytes.toBytes("grade"), Bytes.toBytes(""), Bytes.toBytes(" dao ji dian le"));
//开始追加
table.append(append);
//关闭资源
table.close();
}
//自增incr
//incr '表名', '行键', '列族:列名', 步长值
public static void incr() throws java.lang.Exception
{
TableName tableName = TableName.valueOf("ns1:mytest_4");
//取得一个要操作的表
Table table = hbaseConn.getTable(tableName);
//只为一个列incr
table.incrementColumnValue(Bytes.toBytes("xiaoming"),Bytes.toBytes("grade"), Bytes.toBytes(""),5);
//为多个列incr
Increment increment = new Increment(Bytes.toBytes("zhangshan"));
increment.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"),1);
increment.addColumn(Bytes.toBytes("course"), Bytes.toBytes("shuxue"),2);
increment.addColumn(Bytes.toBytes("course"), Bytes.toBytes("english"),3);
increment.addColumn(Bytes.toBytes("grade"), Bytes.toBytes(""),4);
table.increment(increment);
//关闭资源
table.close();
}
//****************************批处理****************************
public static void pichuli() throws java.lang.Exception {
TableName tableName = TableName.valueOf("ns1:mytest_4");
//取得一个要操作的表
Table table = hbaseConn.getTable(tableName);
ArrayList<Row> rows = new ArrayList<>();
//设置要操作的行,rowkey
Put put_1 = new Put(Bytes.toBytes("xiaoming"));
//参数1:列族 ,参数2:列,参数3:值,(这里的时间戳会自动生成)
put_1.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"), Bytes.toBytes("50"));
put_1.addColumn(Bytes.toBytes("grade"), Bytes.toBytes(""), Bytes.toBytes("A"));
Get get_1 = new Get(Bytes.toBytes("xiaoming"));
Delete del_1 = new Delete(Bytes.toBytes("zhangshan"));
Append append_1 = new Append(Bytes.toBytes("xiaoming"));
append_1.add(Bytes.toBytes("course"), Bytes.toBytes("yuwen"), Bytes.toBytes("分"));
append_1.add(Bytes.toBytes("grade"), Bytes.toBytes(""), Bytes.toBytes("组"));
rows.add(put_1);
rows.add(get_1);
rows.add(del_1);
rows.add(append_1);
Object[] results = new Object[rows.size()];
//执行批处理
//table.batch(rows,results);
//执行批处理,有回调函数
table.batchCallback(rows, results, new Batch.Callback<Result>() {
@Override
public void update(byte[] rowKeyBytes, byte[] keyValueBytes, Result result) {
System.out.println(Bytes.toString(rowKeyBytes));
System.out.println();
System.out.println(Bytes.toString(keyValueBytes));
System.out.println();
System.out.println(result);
}
});
//关闭资源
table.close();
}
}