HBase1.0.x JAVA 例項
HBase1.0.x 最少jar包依賴
圖片裡的jar包是我根據下邊的程式一個一個找的,
一直想找個最小的jar包依賴,一整就用maven把所有包都下下來了,好幾百M,有點太大了,而我部署專案的機器上又沒有hbase所以我想找個最小的jar包依賴。
之後是個程式碼例子,網上搜到的大部分程式碼都是Hbase0.98版的,有些類已經@Deprecated了,雖然能用但是看著特別不爽,從網上找了一個算是比較新的,但是還是有
很多@Deprecated的函式,所以我這裡找了一下,然後對例子進行了修改。
我用的版本是CDH5.4.1普通的話把cdh換成對應的原生的jar包應該也是可以的。
package test;
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.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
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.util.Bytes;
public class HBaseUtil {
public static Configuration configuration;
public static Connection con;
static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "10.10.92.151");
// configuration.set("hbase.master", "192.168.1.103:600000");
}
public static void main(String[] args) throws Exception {
con = ConnectionFactory.createConnection(configuration);
createTable("abc");
insertData("abc");
QueryAll("abc");
}
/**
* 建立表
*
* @param tableName
*/
public static void createTable(String tableName) {
System.out.println("start create table ......");
try {
Admin hBaseAdmin = con.getAdmin();// 新用法
// HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration); //老用法
if (hBaseAdmin.tableExists(TableName.valueOf(tableName))) {// 如果存在要建立的表,那麼先刪除,再建立
hBaseAdmin.disableTable(TableName.valueOf(tableName));
hBaseAdmin.deleteTable(TableName.valueOf(tableName));
System.out.println(tableName + " is exist,detele....");
}
HTableDescriptor tableDescriptor = new HTableDescriptor(
TableName.valueOf(tableName));
tableDescriptor.addFamily(new HColumnDescriptor("column1"));
tableDescriptor.addFamily(new HColumnDescriptor("column2"));
tableDescriptor.addFamily(new HColumnDescriptor("column3"));
hBaseAdmin.createTable(tableDescriptor);
hBaseAdmin.close();
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end create table ......");
}
/**
* 插入資料
*
* @param tableName
* @throws IOException
*/
public static void insertData(String tableName) throws IOException {
System.out.println("start insert data ......");
Connection connection = ConnectionFactory
.createConnection(configuration);
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put("112233bbbcccc".getBytes());// 一個PUT代表一行資料,再NEW一個PUT表示第二行資料,每行一個唯一的ROWKEY,此處rowkey為put構造方法中傳入的值
put.addColumn("column1".getBytes(), "col1".getBytes(), "aaa".getBytes());// 本行資料的第一列
put.addColumn("column2".getBytes(), "col2".getBytes(), "bbb".getBytes());// 本行資料的第三列
put.addColumn("column3".getBytes(), "col3".getBytes(), "ccc".getBytes());// 本行資料的第三列
try {
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end insert data ......");
}
/**
* 刪除一張表
*
* @param tableName
*/
public static void dropTable(String tableName) {
try {
Admin admin = con.getAdmin();
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(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 {
Table table = con.getTable(TableName.valueOf(tablename));
// List list = new ArrayList();
Delete d1 = new Delete(rowkey.getBytes());
// list.add(d1);
//
// table.delete(list);
table.delete(d1);
System.out.println("刪除行成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 組合條件刪除
*
* @param tablename
* @param rowkey
*/
public static void deleteByCondition(String tablename, String rowkey) {
// 目前還沒有發現有效的API能夠實現 根據非rowkey的條件刪除 這個功能能,還有清空表全部資料的API操作
}
/**
* 查詢所有資料
*
* @param tableName
* @throws IOException
*/
public static void QueryAll(String tableName) throws IOException {
Connection connection = ConnectionFactory
.createConnection(configuration);
Table table = connection.getTable(TableName.valueOf(tableName));
try {
ResultScanner rs = table.getScanner(new Scan());
for (Result r : rs) {
System.out.println("獲得到rowkey:" + new String(r.getRow()));
for (Cell cell : r.rawCells()) {
System.out.println("family:"
+ Bytes.toString(cell.getFamilyArray(),
cell.getFamilyOffset(),
cell.getFamilyLength()));
System.out.println("Qualifier:"
+ Bytes.toString(cell.getQualifierArray(),
cell.getQualifierOffset(),
cell.getQualifierLength()));
System.out.println("value:"
+ Bytes.toString(cell.getValueArray(),
cell.getValueOffset(),
cell.getValueLength()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 單條件查詢,根據rowkey查詢唯一一條記錄
*
* @param tableName
*/
public static void QueryByCondition1(String tableName) {
// HTablePool pool = new HTablePool(configuration, 1000);
try {
HTable table = (HTable) con.getTable(TableName.valueOf(tableName));
Get scan = new Get("abcdef".getBytes());// 根據rowkey查詢
Result r = table.get(scan);
System.out.println("獲得到rowkey:" + new String(r.getRow()));
for (Cell cell : r.rawCells()) {
System.out
.println("family:"
+ Bytes.toString(cell.getFamilyArray(),
cell.getFamilyOffset(),
cell.getFamilyLength()));
System.out.println("Qualifier:"
+ Bytes.toString(cell.getQualifierArray(),
cell.getQualifierOffset(),
cell.getQualifierLength()));
System.out.println("value:"
+ Bytes.toString(cell.getValueArray(),
cell.getValueOffset(), cell.getValueLength()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 單條件按查詢,查詢多條記錄
*
* @param tableName
*/
public static void QueryByCondition2(String tableName) {
try {
HTable table = (HTable) con.getTable(TableName.valueOf(tableName));
Filter filter = new SingleColumnValueFilter(
Bytes.toBytes("column1"), null, CompareOp.EQUAL,
Bytes.toBytes("aaa")); // 當列column1的值為aaa時進行查詢
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 (Cell cell : r.rawCells()) {
System.out.println("family:"
+ Bytes.toString(cell.getFamilyArray(),
cell.getFamilyOffset(),
cell.getFamilyLength()));
System.out.println("Qualifier:"
+ Bytes.toString(cell.getQualifierArray(),
cell.getQualifierOffset(),
cell.getQualifierLength()));
System.out.println("value:"
+ Bytes.toString(cell.getValueArray(),
cell.getValueOffset(),
cell.getValueLength()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 組合條件查詢
*
* @param tableName
*/
public static void QueryByCondition3(String tableName) {
try {
HTable table = (HTable) con.getTable(TableName.valueOf(tableName));
List<Filter> filters = new ArrayList<Filter>();
Filter filter1 = new SingleColumnValueFilter(
Bytes.toBytes("column1"), null, CompareOp.EQUAL,
Bytes.toBytes("aaa"));
filters.add(filter1);
Filter filter2 = new SingleColumnValueFilter(
Bytes.toBytes("column2"), null, CompareOp.EQUAL,
Bytes.toBytes("bbb"));
filters.add(filter2);
Filter filter3 = new SingleColumnValueFilter(
Bytes.toBytes("column3"), null, CompareOp.EQUAL,
Bytes.toBytes("ccc"));
filters.add(filter3);
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 (Cell cell : r.rawCells()) {
System.out.println("family:"
+ Bytes.toString(cell.getFamilyArray(),
cell.getFamilyOffset(),
cell.getFamilyLength()));
System.out.println("Qualifier:"
+ Bytes.toString(cell.getQualifierArray(),
cell.getQualifierOffset(),
cell.getQualifierLength()));
System.out.println("value:"
+ Bytes.toString(cell.getValueArray(),
cell.getValueOffset(),
cell.getValueLength()));
}
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
相關文章
- Java例項教程Java
- java介面例項Java
- java多型例項Java多型
- Thrift-java例項Java
- Java--例項化Java
- java反射方式建立例項Java反射
- Java解析Excel例項解析JavaExcel
- Java CXF介紹與例項Java
- Java 例項 - 檔案寫入Java
- java狀態模式例項解析Java模式
- java多型抽象類例項Java多型抽象
- KafKa Java程式設計例項KafkaJava程式設計
- postgresql的java例項肯定可用SQLJava
- Java HashMap例項原始碼分析JavaHashMap原始碼
- java多執行緒例項Java執行緒
- java串列埠通訊例項 -Java串列埠
- java正規表示式例項Java
- 例項分析理解Java位元組碼Java
- java class例項代表哪些結構Java
- Java 正規表示式例項操作Java
- Java中Websocket使用例項解讀JavaWeb
- 例項講解Java工廠模式Java模式
- 策略模式與簡單java例項模式Java
- java 例項變數初始化Java變數
- Java&CORBA程式設計例項JavaORB程式設計
- Java XML程式設計例項解析JavaXML程式設計
- Java介面為什麼不能例項化Java
- Java十六進位制補0(例項)Java
- java_隨機數(統計例項)Java隨機
- Java的Socket通訊簡單例項Java單例
- java多型-優化上個例項Java多型優化
- Java的類的例項化順序Java
- Java 經典例項:自定義迭代器Java
- Java類初始化和例項化Java
- Java 下 SSL 通訊原理及例項Java
- Java延時例項分析:Lock vs SynchronizedJavasynchronized
- Java中內部類的例項化Java
- JAVA例項 陰陽曆演算法(轉)Java演算法