本文目錄一覽:
- 1、如何使用Java API操作Hbase
- 2、java 操作hbase數據庫讀取數據時運行代碼到new h1table就不動了,跟卡住了一樣。會的大神們可以加
- 3、求助帖,hbase新手,windows中的java怎麼連接linux中的hbase
- 4、如何使用Java API讀寫Hbase
- 5、hbase java端調用
如何使用Java API操作Hbase
寫了個Hbase新的api的增刪改查的工具類,以供參考,代碼如下:
package com.dhgate.hbase.test;
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.CellUtil;
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.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.filter.PageFilter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
/**
* 基於新的API
* Hbase0.96版本
* 寫的工具類
*
* **/
public class HbaseCommons {
static Configuration conf=HBaseConfiguration.create();
static String tableName=””;
public static void main(String[] args)throws Exception {
//String tableName=”test”;
//createTable(tableName, null);
}
/**
* 批量添加數據
* @param tableName 標名字
* @param rows rowkey行健的集合
* 本方法僅作示例,其他的內容需要看自己義務改變
*
* **/
public static void insertList(String tableName,String rows[])throws Exception{
HTable table=new HTable(conf, tableName);
ListPut list=new ArrayListPut();
for(String r:rows){
Put p=new Put(Bytes.toBytes(r));
//此處示例添加其他信息
//p.add(Bytes.toBytes(“family”),Bytes.toBytes(“column”), 1000, Bytes.toBytes(“value”));
list.add(p);
}
table.put(list);//批量添加
table.close();//釋放資源
}
/**
* 創建一個表
* @param tableName 表名字
* @param columnFamilys 列簇
*
* **/
public static void createTable(String tableName,String[] columnFamilys)throws Exception{
//admin 對象
HBaseAdmin admin=new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
System.out.println(“此表,已存在!”);
}else{
//舊的寫法
//HTableDescriptor tableDesc=new HTableDescriptor(tableName);
//新的api
HTableDescriptor tableDesc=new HTableDescriptor(TableName.valueOf(tableName));
for(String columnFamily:columnFamilys){
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
}
admin.createTable(tableDesc);
System.out.println(“建表成功!”);
}
admin.close();//關閉釋放資源
}
/**
* 刪除一個表
* @param tableName 刪除的表名
* */
public static void deleteTable(String tableName)throws Exception{
HBaseAdmin admin=new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);//禁用表
admin.deleteTable(tableName);//刪除表
System.out.println(“刪除表成功!”);
}else{
System.out.println(“刪除的表不存在!”);
}
admin.close();
}
/**
* 插入一條數據
* @param tableName 表明
* @param columnFamily 列簇
* @param column 列
* @param value 值
*
* ***/
public static void insertOneRow(String tableName,String rowkey,String columnFamily,String column,String value)throws Exception{
HTable table=new HTable(conf, tableName);
Put put=new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);//放入表
table.close();//釋放資源
}
/**
* 刪除一條數據
* @param tableName 表名
* @param row rowkey行鍵
*
* */
public static void deleteOneRow(String tableName,String row)throws Exception{
HTable table=new HTable(conf, tableName);
Delete delete=new Delete(Bytes.toBytes(row));
table.delete(delete);
table.close();
}
/**
* 刪除多條數據
* @param tableName 表名
* @param rows 行健集合
*
* **/
public static void deleteList(String tableName,String rows[])throws Exception{
HTable table=new HTable(conf, tableName);
ListDelete list=new ArrayListDelete();
for(String row:rows){
Delete del=new Delete(Bytes.toBytes(row));
list.add(del);
}
table.delete(list);
table.close();//釋放資源
}
/**
* 獲取一條數據,根據rowkey
* @param tableName 表名
* @param row 行健
*
* **/
public static void getOneRow(String tableName,String row)throws Exception{
HTable table=new HTable(conf, tableName);
Get get=new Get(Bytes.toBytes(row));
Result result=table.get(get);
printRecoder(result);//打印記錄
table.close();//釋放資源
}
/**
* 查看某個表下的所有數據
*
* @param tableName 表名
* */
public static void showAll(String tableName)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);//打印記錄
}
table.close();//釋放資源
}
/**
* 查看某個表下的所有數據
*
* @param tableName 表名
* @param rowKey 行健
* */
public static void ScanPrefixByRowKey(String tableName,String rowKey)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);//打印記錄
}
table.close();//釋放資源
}
/**
* 查看某個表下的所有數據
*
* @param tableName 表名
* @param rowKey 行健掃描
* @param limit 限制返回數據量
* */
public static void ScanPrefixByRowKeyAndLimit(String tableName,String rowKey,long limit)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));
scan.setFilter(new PageFilter(limit));
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);//打印記錄
}
table.close();//釋放資源
}
/**
* 根據rowkey掃描一段範圍
* @param tableName 表名
* @param startRow 開始的行健
* @param stopRow 結束的行健
* **/
public void scanByStartAndStopRow(String tableName,String startRow,String stopRow)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
scan.setStartRow(Bytes.toBytes(startRow));
scan.setStopRow(Bytes.toBytes(stopRow));
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);
}
table.close();//釋放資源
}
/**
* 掃描整個表裡面具體的某個字段的值
* @param tableName 表名
* @param columnFalimy 列簇
* @param column 列
* **/
public static void getValueDetail(String tableName,String columnFalimy,String column)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
System.out.println(“值: ” +new String(r.getValue(Bytes.toBytes(columnFalimy), Bytes.toBytes(column))));
}
table.close();//釋放資源
}
/**
* 打印一條記錄的詳情
*
* */
public static void printRecoder(Result result)throws Exception{
for(Cell cell:result.rawCells()){
System.out.print(“行健: “+new String(CellUtil.cloneRow(cell)));
System.out.print(“列簇: “+new String(CellUtil.cloneFamily(cell)));
System.out.print(” 列: “+new String(CellUtil.cloneQualifier(cell)));
System.out.print(” 值: “+new String(CellUtil.cloneValue(cell)));
System.out.println(“時間戳: “+cell.getTimestamp());
}
}
}
java 操作hbase數據庫讀取數據時運行代碼到new h1table就不動了,跟卡住了一樣。會的大神們可以加
首先你應該看Master進程是否已經成功啟動,檢查下master的60010監控界面。這日誌報的是連接拒絕 ,或者關閉防火牆 極有可能是你PC機網絡無法連接到虛擬機裡邊,你可以從本機telnet下虛擬機上master的端口,看下能連上不6646
求助帖,hbase新手,windows中的java怎麼連接linux中的hbase
一、新建本地java工程
file-new-java project
二、添加jar包和配置文件
1、添加JAR包
右擊Propertie在彈出的快捷菜單中選擇Java Build Path對話框,在該對話框中單擊Libraries選項卡,在該選項卡下單擊
Add External JARs按鈕,定位到$HBASE/lib目錄下,並選取如下JAR包。
hadoop-core-1.0.0.jar
commons-loggings-version.jar
commons-cli-version.jar
commons-lang-version.jar
commons-configuration-version.jar
hbase-0.94.1.jar
zookeeper-3.4.3.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
log4j-1.2.16.jar
protobuf-java-2.4.1.jar
2、添加hbase-site.xml配置文件
在工程根目錄下創建conf文件夾,將$HBASE_HOME/conf/目錄中的hbase-site.xml文件複製到該文件夾中。通過右鍵
選擇Propertie-Java Build Path-Libraries-Add Class Folder。
3、windows下開發HBase應用程序,HBase部署在linux環境中,在運行調試時可能會出現無法找到主機,類似異常信息如下:java.net.UnknownHostException: unknown host: master
解決辦法如下:在C:\WINDOWS\system32\drivers\etc\hosts文件中添加如下信息
192.168.2.34 master
如何使用Java API讀寫Hbase
一般情況下,我們使用Linux的shell命令,就可以非常輕鬆的操作Hbase,例如一些建表,建列簇,插值,顯示所有表,統計數量等等,但有時為了提高靈活性,我們也需要使用編程語言來操作Hbase,當然Hbase通過Thrift接口提供了對大多數主流編程語言的支持,例如C++,PHP,Python,Ruby等等,那麼本篇,散仙給出的例子是基於Java原生的API操作Hbase,相比其他的一些編程語言,使用Java操作Hbase,會更加高效一些,因為Hbase本身就是使用Java語言編寫的。轉載
下面,散仙給出源碼,以供參考:
package com.hbase;
import java.util.ArrayList;
import java.util.List;
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.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;
/**
* @author 三劫散仙
*
* **/
public class Test {
static Configuration conf=null;
static{
conf=HBaseConfiguration.create();//hbase的配置信息
conf.set(“hbase.zookeeper.quorum”, “10.2.143.5”); //zookeeper的地址
}
public static void main(String[] args)throws Exception {
Test t=new Test();
//t.createTable(“temp”, new String[]{“name”,”age”});
//t.insertRow(“temp”, “2”, “age”, “myage”, “100”);
// t.getOneDataByRowKey(“temp”, “2”);
t.showAll(“temp”);
}
/***
* 創建一張表
* 並指定列簇
* */
public void createTable(String tableName,String cols[])throws Exception{
HBaseAdmin admin=new HBaseAdmin(conf);//客戶端管理工具類
if(admin.tableExists(tableName)){
System.out.println(“此表已經存在…….”);
}else{
HTableDescriptor table=new HTableDescriptor(tableName);
for(String c:cols){
HColumnDescriptor col=new HColumnDescriptor(c);//列簇名
table.addFamily(col);//添加到此表中
}
admin.createTable(table);//創建一個表
admin.close();
System.out.println(“創建表成功!”);
}
}
/**
* 添加數據,
* 建議使用批量添加
* @param tableName 表名
* @param row 行號
* @param columnFamily 列簇
* @param column 列
* @param value 具體的值
*
* **/
public void insertRow(String tableName, String row,
String columnFamily, String column, String value) throws Exception {
HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(row));
// 參數出分別:列族、列、值
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
Bytes.toBytes(value));
table.put(put);
table.close();//關閉
System.out.println(“插入一條數據成功!”);
}
/**
* 刪除一條數據
* @param tableName 表名
* @param row rowkey
* **/
public void deleteByRow(String tableName,String rowkey)throws Exception{
HTable h=new HTable(conf, tableName);
Delete d=new Delete(Bytes.toBytes(rowkey));
h.delete(d);//刪除一條數據
h.close();
}
/**
* 刪除多條數據
* @param tableName 表名
* @param row rowkey
* **/
public void deleteByRow(String tableName,String rowkey[])throws Exception{
HTable h=new HTable(conf, tableName);
ListDelete list=new ArrayListDelete();
for(String k:rowkey){
Delete d=new Delete(Bytes.toBytes(k));
list.add(d);
}
h.delete(list);//刪除
h.close();//釋放資源
}
/**
* 得到一條數據
*
* @param tableName 表名
* @param rowkey 行號
* ***/
public void getOneDataByRowKey(String tableName,String rowkey)throws Exception{
HTable h=new HTable(conf, tableName);
Get g=new Get(Bytes.toBytes(rowkey));
Result r=h.get(g);
for(KeyValue k:r.raw()){
System.out.println(“行號: “+Bytes.toStringBinary(k.getRow()));
System.out.println(“時間戳: “+k.getTimestamp());
System.out.println(“列簇: “+Bytes.toStringBinary(k.getFamily()));
System.out.println(“列: “+Bytes.toStringBinary(k.getQualifier()));
//if(Bytes.toStringBinary(k.getQualifier()).equals(“myage”)){
// System.out.println(“值: “+Bytes.toInt(k.getValue()));
//}else{
String ss= Bytes.toString(k.getValue());
System.out.println(“值: “+ss);
//}
}
h.close();
}
/**
* 掃描所有數據或特定數據
* @param tableName
* **/
public void showAll(String tableName)throws Exception{
HTable h=new HTable(conf, tableName);
Scan scan=new Scan();
//掃描特定區間
//Scan scan=new Scan(Bytes.toBytes(“開始行號”),Bytes.toBytes(“結束行號”));
ResultScanner scanner=h.getScanner(scan);
for(Result r:scanner){
System.out.println(“==================================”);
for(KeyValue k:r.raw()){
System.out.println(“行號: “+Bytes.toStringBinary(k.getRow()));
System.out.println(“時間戳: “+k.getTimestamp());
System.out.println(“列簇: “+Bytes.toStringBinary(k.getFamily()));
System.out.println(“列: “+Bytes.toStringBinary(k.getQualifier()));
//if(Bytes.toStringBinary(k.getQualifier()).equals(“myage”)){
// System.out.println(“值: “+Bytes.toInt(k.getValue()));
//}else{
String ss= Bytes.toString(k.getValue());
System.out.println(“值: “+ss);
//}
}
}
h.close();
}
}
顯示所有數據的打印輸出如下:
==================================
行號: 1
時間戳: 1385597699287
列簇: name
列: myname
值: 秦東亮
==================================
行號: 2
時間戳: 1385598393306
列簇: age
列: myage
值: 100
行號: 2
時間戳: 1385597723900
列簇: name
列: myname
值: 三劫散仙
由此,可以看出Hbase的對外的API提供接口,是非常簡單易用的
hbase java端調用
這是缺少必要的類org/apache/hadoop/thirdparty/guava/common/primitives/UnsignedBytes
你可以到jarsearch上搜索含有這個類的jar包,然後把它放到classpath下就行了
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/306633.html