package com.hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class HTest {
private static Configuration sConf;
private static Connection sCon;
private static Admin sAdmin;
public static void main(String[] args) throws IOException {
String[] s = { "f1", "f2", "f3" };
init();
createTable("table2", s);
putData("table2", "r2", "f1", "c1", "panpan");
Result res = getDataByRow("table2", "r1");
System.out.println(res.value());
getAllData("table2");
delete("table1");
deleteDatayRow("table2","r1");
close();
}
public static void init() throws IOException {
sConf = HBaseConfiguration.create();
sConf.set("root.dir", "dhfs://localhost:9000/hbase");
sCon = ConnectionFactory.createConnection(sConf);
sAdmin = sCon.getAdmin();
}
public static void close() {
try {
if (null != sAdmin) {
sAdmin.close();
}
if (sCon != null) {
sCon.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void createTable(String tableName, String[] columnFamilys) throws IOException {
TableName _tableName = TableName.valueOf(tableName);
if (sAdmin.tableExists(_tableName)) {
System.out.println("table exist");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(_tableName);
for (String columnFamily : columnFamilys)
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
sAdmin.createTable(tableDesc);
System.out.println("create table success!");
}
}
// add data
public static void putData(String tableName, String rowKey, String columnFamily, String col, String value)
throws IOException {
Table table = sCon.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(col), Bytes.toBytes(value));
table.put(put);
System.out.println("put data success");
table.close();
}
public static void getAllData(String tableName) throws IOException {
Table table = sCon.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println("Scan: "+result);
}
}
public static Result getDataByRow(String tableName, String row) throws IOException {
Table table = sCon.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
return result;
}
//delete row
public static void deleteDatayRow(String tableName,String row) throws IOException{
Table table = sCon.getTable(TableName.valueOf(tableName));
table.delete(new Delete(row.getBytes()));
}
//删除表
public static void delete(String tableName) throws IOException{
TableName _tableName = TableName.valueOf(tableName);
if(sAdmin.tableExists(_tableName)){
try {
sAdmin.disableTable(_tableName);
sAdmin.deleteTable(_tableName);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Delete "+tableName+" 失败");
}
}else{
System.out.println("table not exist");
return ;
}
System.out.println("Delete "+tableName+" 成功");
}
}
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class HTest {
private static Configuration sConf;
private static Connection sCon;
private static Admin sAdmin;
public static void main(String[] args) throws IOException {
String[] s = { "f1", "f2", "f3" };
init();
createTable("table2", s);
putData("table2", "r2", "f1", "c1", "panpan");
Result res = getDataByRow("table2", "r1");
System.out.println(res.value());
getAllData("table2");
delete("table1");
deleteDatayRow("table2","r1");
close();
}
public static void init() throws IOException {
sConf = HBaseConfiguration.create();
sConf.set("root.dir", "dhfs://localhost:9000/hbase");
sCon = ConnectionFactory.createConnection(sConf);
sAdmin = sCon.getAdmin();
}
public static void close() {
try {
if (null != sAdmin) {
sAdmin.close();
}
if (sCon != null) {
sCon.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void createTable(String tableName, String[] columnFamilys) throws IOException {
TableName _tableName = TableName.valueOf(tableName);
if (sAdmin.tableExists(_tableName)) {
System.out.println("table exist");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(_tableName);
for (String columnFamily : columnFamilys)
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
sAdmin.createTable(tableDesc);
System.out.println("create table success!");
}
}
// add data
public static void putData(String tableName, String rowKey, String columnFamily, String col, String value)
throws IOException {
Table table = sCon.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(col), Bytes.toBytes(value));
table.put(put);
System.out.println("put data success");
table.close();
}
public static void getAllData(String tableName) throws IOException {
Table table = sCon.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println("Scan: "+result);
}
}
public static Result getDataByRow(String tableName, String row) throws IOException {
Table table = sCon.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
return result;
}
//delete row
public static void deleteDatayRow(String tableName,String row) throws IOException{
Table table = sCon.getTable(TableName.valueOf(tableName));
table.delete(new Delete(row.getBytes()));
}
//删除表
public static void delete(String tableName) throws IOException{
TableName _tableName = TableName.valueOf(tableName);
if(sAdmin.tableExists(_tableName)){
try {
sAdmin.disableTable(_tableName);
sAdmin.deleteTable(_tableName);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Delete "+tableName+" 失败");
}
}else{
System.out.println("table not exist");
return ;
}
System.out.println("Delete "+tableName+" 成功");
}
}