import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;
public class Hbase_Test {
public static Configuration configuration;
static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "192.168.1.200,192.168.1.201,192.168.1.202");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
//configuration.set("hbase.master", "192.168.1.200:60000");
}
public static void main(String[] args) throws IOException {
createTable("zyf123");
// insertData("student");
// QueryAll("student");
// QueryByCondition1("student","xiaoming");
//QueryByCondition2("student");
//QueryByCondition3("student");
// deleteRow("student","address");
}
/**
* 创建表
*
* @param tableName
*/
public static void createTable(String tableName) {
System.out.println("开始创建表......");
try {
HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
if (hBaseAdmin.tableExists(tableName)) {// 如果存在要创建的表,那么先删除,再创建
hBaseAdmin.disableTable(tableName);
hBaseAdmin.deleteTable(tableName);
System.out.println(tableName + " 表存在,已删除....");
}
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
tableDescriptor.addFamily(new HColumnDescriptor("info"));
tableDescriptor.addFamily(new HColumnDescriptor("address"));
hBaseAdmin.createTable(tableDescriptor);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("创建表" + tableName + "成功");
}
/**
* 插入数据
*
* @param tableName
* @throws IOException
*/
public static void insertData(String tableName) throws IOException {
System.out.println("开始导入数据 ......");
// HTablePool pool = new HTablePool(configuration, 1000);
// HTable table = (HTable) pool.getTable(tableName);
HTable table = new HTable(configuration, tableName);
Put put = new Put("xiaoming".getBytes());// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
put.add("info".getBytes(), "age".getBytes(), "25".getBytes());// 本行数据的第一列
put.add("info".getBytes(), "birthday".getBytes(),
"1987-06-17".getBytes());// 本行数据的第一列
put.add("info".getBytes(), "company".getBytes(), "alik".getBytes());// 本行数据的第一列
put.add("address".getBytes(), "contry".getBytes(), "china".getBytes());// 本行数据的第二列
put.add("address".getBytes(), "province".getBytes(), "hubei".getBytes());// 本行数据的第二列
try {
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
put = new Put("xiaofeng".getBytes());// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
put.add("info".getBytes(), "age".getBytes(), "28".getBytes());// 本行数据的第一列
put.add("info".getBytes(), "birthday".getBytes(),
"1987-4-17".getBytes());// 本行数据的第一列
put.add("info".getBytes(), "favorite".getBytes(), "movie".getBytes());// 本行数据的第一列
put.add("address".getBytes(), "contry".getBytes(), "china".getBytes());// 本行数据的第二列
put.add("address".getBytes(), "city".getBytes(), "haerbin".getBytes());// 本行数据的第二列
try {
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("数据导入成功 ......");
}
/**
* 删除一张表
*
* @param tableName
*/
public static void dropTable(String tableName) {
try {
HBaseAdmin admin = new HBaseAdmin(configuration);
admin.disableTable(tableName);
admin.deleteTable(tableName);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据 rowkey删除一条记录
*
* @param tablename
* @param rowkey
*/
public static void deleteRow(String tablename, String rowkey) {
try {
HTable table = new HTable(configuration, tablename);
Delete d1 = new Delete(rowkey.getBytes());
table.delete(d1);
System.out.println("删除行成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 查询所有数据
*
* @param tableName
* @throws IOException
*/
public static void QueryAll(String tableName) throws IOException {
HTable table = new HTable(configuration, tableName);
try {
ResultScanner rs = table.getScanner(new Scan());
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (Cell keyValue : r.rawCells()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "标识符" + new String(keyValue.getQualifier())
+ "====值:" + new String(keyValue.getValue()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 单条件查询,根据rowkey查询唯一一条记录
*
* @param tableName
* @throws IOException
*/
public static void QueryByCondition1(String tableName, String row_id)
throws IOException {
HTable table = new HTable(configuration, tableName);
try {
Get scan = new Get(row_id.getBytes());// 根据rowkey查询
Result r = table.get(scan);
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "标识符" + new String(keyValue.getQualifier())
+ "====值:" + new String(keyValue.getValue()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 单条件查询,查询多条记录
*
* @param tableName
*/
public static void QueryByCondition2(String tableName) {
try {
HTable table = new HTable(configuration, tableName);
Filter filter = new SingleColumnValueFilter(Bytes.toBytes("address"),
Bytes.toBytes("city"), CompareOp.EQUAL,
Bytes.toBytes("haerbin")); // 当列info:company的值为ailk时进行查询
Scan s = new Scan();
s.setFilter(filter);
ResultScanner rs = table.getScanner(s);
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "标识符" + new String(keyValue.getQualifier())
+ "====值:" + new String(keyValue.getValue()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 组合条件查询
*
* @param tableName
*/
public static void QueryByCondition3(String tableName) {
try {
HTable table = new HTable(configuration, tableName);
List<Filter> filters = new ArrayList<Filter>();
Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes("info"),
Bytes.toBytes("company"), CompareOp.EQUAL,
Bytes.toBytes("ailk"));
filters.add(filter1);
Filter filter2 = new SingleColumnValueFilter(
Bytes.toBytes("address"), Bytes.toBytes("contry"),
CompareOp.EQUAL, Bytes.toBytes("china"));
filters.add(filter2);
FilterList filterList1 = new FilterList(filters);
Scan scan = new Scan();
scan.setFilter(filterList1);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "标识符" + new String(keyValue.getQualifier())
+ "====值:" + new String(keyValue.getValue()));
}
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;
public class Hbase_Test {
public static Configuration configuration;
static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "192.168.1.200,192.168.1.201,192.168.1.202");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
//configuration.set("hbase.master", "192.168.1.200:60000");
}
public static void main(String[] args) throws IOException {
createTable("zyf123");
// insertData("student");
// QueryAll("student");
// QueryByCondition1("student","xiaoming");
//QueryByCondition2("student");
//QueryByCondition3("student");
// deleteRow("student","address");
}
/**
* 创建表
*
* @param tableName
*/
public static void createTable(String tableName) {
System.out.println("开始创建表......");
try {
HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
if (hBaseAdmin.tableExists(tableName)) {// 如果存在要创建的表,那么先删除,再创建
hBaseAdmin.disableTable(tableName);
hBaseAdmin.deleteTable(tableName);
System.out.println(tableName + " 表存在,已删除....");
}
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
tableDescriptor.addFamily(new HColumnDescriptor("info"));
tableDescriptor.addFamily(new HColumnDescriptor("address"));
hBaseAdmin.createTable(tableDescriptor);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("创建表" + tableName + "成功");
}
/**
* 插入数据
*
* @param tableName
* @throws IOException
*/
public static void insertData(String tableName) throws IOException {
System.out.println("开始导入数据 ......");
// HTablePool pool = new HTablePool(configuration, 1000);
// HTable table = (HTable) pool.getTable(tableName);
HTable table = new HTable(configuration, tableName);
Put put = new Put("xiaoming".getBytes());// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
put.add("info".getBytes(), "age".getBytes(), "25".getBytes());// 本行数据的第一列
put.add("info".getBytes(), "birthday".getBytes(),
"1987-06-17".getBytes());// 本行数据的第一列
put.add("info".getBytes(), "company".getBytes(), "alik".getBytes());// 本行数据的第一列
put.add("address".getBytes(), "contry".getBytes(), "china".getBytes());// 本行数据的第二列
put.add("address".getBytes(), "province".getBytes(), "hubei".getBytes());// 本行数据的第二列
try {
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
put = new Put("xiaofeng".getBytes());// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
put.add("info".getBytes(), "age".getBytes(), "28".getBytes());// 本行数据的第一列
put.add("info".getBytes(), "birthday".getBytes(),
"1987-4-17".getBytes());// 本行数据的第一列
put.add("info".getBytes(), "favorite".getBytes(), "movie".getBytes());// 本行数据的第一列
put.add("address".getBytes(), "contry".getBytes(), "china".getBytes());// 本行数据的第二列
put.add("address".getBytes(), "city".getBytes(), "haerbin".getBytes());// 本行数据的第二列
try {
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("数据导入成功 ......");
}
/**
* 删除一张表
*
* @param tableName
*/
public static void dropTable(String tableName) {
try {
HBaseAdmin admin = new HBaseAdmin(configuration);
admin.disableTable(tableName);
admin.deleteTable(tableName);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据 rowkey删除一条记录
*
* @param tablename
* @param rowkey
*/
public static void deleteRow(String tablename, String rowkey) {
try {
HTable table = new HTable(configuration, tablename);
Delete d1 = new Delete(rowkey.getBytes());
table.delete(d1);
System.out.println("删除行成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 查询所有数据
*
* @param tableName
* @throws IOException
*/
public static void QueryAll(String tableName) throws IOException {
HTable table = new HTable(configuration, tableName);
try {
ResultScanner rs = table.getScanner(new Scan());
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (Cell keyValue : r.rawCells()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "标识符" + new String(keyValue.getQualifier())
+ "====值:" + new String(keyValue.getValue()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 单条件查询,根据rowkey查询唯一一条记录
*
* @param tableName
* @throws IOException
*/
public static void QueryByCondition1(String tableName, String row_id)
throws IOException {
HTable table = new HTable(configuration, tableName);
try {
Get scan = new Get(row_id.getBytes());// 根据rowkey查询
Result r = table.get(scan);
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "标识符" + new String(keyValue.getQualifier())
+ "====值:" + new String(keyValue.getValue()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 单条件查询,查询多条记录
*
* @param tableName
*/
public static void QueryByCondition2(String tableName) {
try {
HTable table = new HTable(configuration, tableName);
Filter filter = new SingleColumnValueFilter(Bytes.toBytes("address"),
Bytes.toBytes("city"), CompareOp.EQUAL,
Bytes.toBytes("haerbin")); // 当列info:company的值为ailk时进行查询
Scan s = new Scan();
s.setFilter(filter);
ResultScanner rs = table.getScanner(s);
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "标识符" + new String(keyValue.getQualifier())
+ "====值:" + new String(keyValue.getValue()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 组合条件查询
*
* @param tableName
*/
public static void QueryByCondition3(String tableName) {
try {
HTable table = new HTable(configuration, tableName);
List<Filter> filters = new ArrayList<Filter>();
Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes("info"),
Bytes.toBytes("company"), CompareOp.EQUAL,
Bytes.toBytes("ailk"));
filters.add(filter1);
Filter filter2 = new SingleColumnValueFilter(
Bytes.toBytes("address"), Bytes.toBytes("contry"),
CompareOp.EQUAL, Bytes.toBytes("china"));
filters.add(filter2);
FilterList filterList1 = new FilterList(filters);
Scan scan = new Scan();
scan.setFilter(filterList1);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "标识符" + new String(keyValue.getQualifier())
+ "====值:" + new String(keyValue.getValue()));
}
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}