本文主要包含hbase安装,hbase单机安装,hbase集群安装,hbase下载安装,hbase windows安装等服务器相关知识,网友希望可以进行参考
hbase,hbase安装
package com.utils;import java.io.IOException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
/**
* hbase
* @author xiaoming.liang
*
*/
public class HbaseUtils {
HBaseAdmin admin = null;
Configuration conf = null;
public HbaseUtils() {
conf = new Configuration();
conf.set("hbase.zookeeper.quorum",
"10.200.42.1,10.200.42.2,10.200.42.3");
conf.set("hbase.rootdir", "hdfs://cluster/hbase");
try {
admin = new HBaseAdmin(conf);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
HbaseUtils hbaseUtils = new HbaseUtils();
// 创建表1
//hbaseUtils.createTable("test_demo", "cfg");
String tableName = "blog2";
String[] family = { "article", "author" };
//hbaseUtils.creatTable(tableName, family);
// 为表添加数据
String[] column1 = { "title", "content", "tag" };
String[] value1 = {
"Head First HBase",
"HBase is the Hadoop database. Use it when you need random, realtime read/write access to your Big Data.",
"Hadoop,HBase,NoSQL" };
String[] column2 = { "name", "nickname" };
String[] value2 = { "nicholas", "lee" };
hbaseUtils.addData("rowkey1", "blog2", column1, value1, column2, value2);
hbaseUtils.addData("rowkey2", "blog2", column1, value1, column2, value2);
hbaseUtils.addData("rowkey3", "blog2", column1, value1, column2, value2);
// 遍历查询
hbaseUtils.getResultScann("blog2", "rowkey4", "rowkey5");
//查询
hbaseUtils.getResult("blog2", "rowkey1");
// 查询某一列的值
hbaseUtils.getResultByColumn("blog2", "rowkey1", "author", "name");
// 更新列
hbaseUtils.updateTable("blog2", "rowkey1", "author", "name", "bin");
// 查询某列的多版本
hbaseUtils.getResultByVersion("blog2", "rowkey1", "author", "name");
// 删除一列
hbaseUtils.deleteColumn("blog2", "rowkey1", "author", "nickname");
// 删除一列
hbaseUtils.deleteColumn("blog2", "rowkey1", "author", "nickname");
// 删除所有列
hbaseUtils.deleteAllColumn("blog2", "rowkey1");
// 删除表
hbaseUtils.deleteTable("blog2");
}
//删除表
public void deleteTable(String tableName) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName + "is deleted!");
}
public void deleteAllColumn(String tableName, String rowKey)
throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Delete deleteAll = new Delete(Bytes.toBytes(rowKey));
table.delete(deleteAll);
System.out.println("all columns are deleted!");
}
// 删除指定的列
public void deleteColumn(String tableName, String rowKey, String falilyName,
String columnName) throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Delete deleteColumn = new Delete(Bytes.toBytes(rowKey));
deleteColumn.deleteColumn(Bytes.toBytes(falilyName), Bytes.toBytes(columnName));
table.delete(deleteColumn);
System.out.println(falilyName + ":" + columnName + "is deleted!");
}
//查询某列数据的多个版本
public void getResultByVersion(String tableName, String rowKey,
String familyName, String columnName) throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
get.setMaxVersions(5);
Result result = table.get(get);
for (KeyValue kv : result.list()) {
System.out.println("family:" + Bytes.toString(kv.getFamily()));
System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));
System.out.println("value:" + Bytes.toString(kv.getValue()));
System.out.println("Timestamp:" + kv.getTimestamp());
System.out.println("-------------------------------------------");
}
}
public void updateT

