當前位置:首頁 » 編程語言 » java調用hbase

java調用hbase

發布時間: 2025-07-29 01:58:14

⑴ eclipse中java程序實現對HBase操作報錯:java.io.IOException: Attempt to start meta tracker failed.

我hadoop和Hbase都是採用偽分布式的
代碼如下:
public class HbaseTest {
private static Configuration conf;
static{
conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://192.168.56.2:9000/hbase");
//使用eclipse時必須添加這個,否則無法定位
conf.set("hbase.zookeeper.quorum", "192.168.56.2");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
public static void createTable(String tableName){
System.out.println("start create table ......");
try {
HBaseAdmin hbAdmin = new HBaseAdmin(conf);
if(hbAdmin.tableExists(tableName)){
hbAdmin.disableTable(tableName);
hbAdmin.deleteTable(tableName);
System.out.println(tableName + " is exist,detele ....");
}
// HTableDescriptor htd = new HTableDescriptor(tableName);
HTableDescriptor htd = new HTableDescriptor(tableName);
htd.addFamily(new HColumnDescriptor("cf1"));
htd.addFamily(new HColumnDescriptor("cf2"));
htd.addFamily(new HColumnDescriptor("cf3"));
htd.addFamily(new HColumnDescriptor("cf4"));
hbAdmin.createTable(htd);
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("end create table ......");

}
public static void main(String[] args) {
createTable("testTable");
}
更多0

⑵ 如何用java redis hbase

比如 MongoDB 和 CouchDB。每個數據存儲都有其優勢和劣勢,特別是當應用於特定領域時。 本期的 Java 開發 2.0 關注的是 Redis,一種輕量級鍵值對數據存儲。多數 NoSQL 實現本質上都是鍵值對,但是 Redis 支持非常豐富的值集,其中包括字元串、列表、集以及散列。因此,Redis 通常被稱為數據結構伺服器。Redis 也以異常快速而聞名,這使得它成為某一特定類型使用案例的最優選擇。 當我們想要了解一種新事物時,將其同熟知的事物進行比較可能會有所幫助,因此,我們將通過對比其與 memcached 的相似性以開啟 Redis 探索之旅。接著我們將介紹 Redis 的主要功能,這些功能可以使其在某些應用場景可以勝過 memcached。最後我將向您展示如何將 Redis 作為一個傳統數據存儲用於模型對象。Redis 和 memcached Memcached 是一個眾所周知的內存對象緩存系統,通過將目標鍵和值導入內存緩存運行。因此,Memcached 能迴避讀取磁碟時發生的 I/O 成本問題。在 Web 應用程序和資料庫之間粘貼 memcached 時會產生更好的讀取性能。因此,對於那些需要快速數據查詢的應用程序,Memcached 是一個不錯的選擇。其中的一個例子為股票查詢服務,需要另外訪問資料庫獲取相對靜態數據,如股票名稱或價格信息。 MemcacheDB 將Redis 與 memcached 相比較並不公平,它與 MemcacheDB 相比要好的多,MemcacheDB 是一個分布式鍵值對存儲系統,專為數據持久化而設計。MemcacheDB 與 Redis 較為相似,其新增優勢可以使其輕松地與 memcached 實現的客戶端進行通信。 但是memcached 也有其局限性,其中一個事實就是它所有的值均是簡單的字元串。Redis 作為 memcached 的替代者,支持更加豐富的功能集。一些基準 (benchmarks) 也表明 Redis 的速度要比 memcached 快很多。Redis 提供的豐富數據類型使其可以在內存中存儲更為復雜的數據,這是使用 memcached 無法實現的。同 memcached 不一樣,Redis 可以持久化其數據。 Redis 解決了一個重大的緩存問題,而其豐富的功能集又為其找到了其他用途。由於 Redis 能夠在磁碟上存儲數據以及跨節點復制數據,因而可以作為數據倉庫用於傳統數據模式(也就是說,您可以使用 Redis,就像使用 RDBMS 一樣)。Redis 還經常被用作隊列系統。在本用例中,Redis 是備份和工作隊列持久化存儲(利用 Redis 的列表類型)的基礎。GitHub 是以此種方法使用 Redis 的大規模基礎架構示例准備好 Redis,立即開始! 要開始使用 Redis,您需要訪問它,可以通過本地安裝或者託管供應商來實現訪問。如果您使用的 MAC,安裝過程可能就不那麼簡單。

⑶ 如何使用Java API操作Hbase

先導入hbase的相關jar包。

再根據api進行操作。

package com.util;
import java.io.IOException;
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.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.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;
static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "192.168.1.103");
configuration.set("hbase.master", "192.168.1.103:600000");
}

public static void main(String[] args) throws IOException {
createTable("abc");
insertData("abc");
QueryAll("abc");
}

/**
* 創建表
* @param tableName
*/
public static void createTable(String tableName) {
System.out.println("start create table ......");
try {
HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
if (hBaseAdmin.tableExists(tableName)) {// 如果存在要創建的表,那麼先刪除,再創建
hBaseAdmin.disableTable(tableName);
hBaseAdmin.deleteTable(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.add("column1".getBytes(), null, "aaa".getBytes());// 本行數據的第一列
put.add("column2".getBytes(), null, "bbb".getBytes());// 本行數據的第三列
put.add("column3".getBytes(), null, "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 {
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);
List list = new ArrayList();
Delete d1 = new Delete(rowkey.getBytes());
list.add(d1);

table.delete(list);
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 (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "====值:" + new String(keyValue.getValue()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 單條件查詢,根據rowkey查詢唯一一條記錄
* @param tableName
*/
public static void QueryByCondition1(String tableName) {

HTablePool pool = new HTablePool(configuration, 1000);
HTable table = (HTable) pool.getTable(tableName);
try {
Get scan = new Get("abcdef".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.getValue()));
}
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 單條件按查詢,查詢多條記錄
* @param tableName
*/
public static void QueryByCondition2(String tableName) {

try {
HTablePool pool = new HTablePool(configuration, 1000);
HTable table = (HTable) pool.getTable(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 (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "====值:" + new String(keyValue.getValue()));
}
}
} catch (Exception e) {
e.printStackTrace();
}

}

/**
* 組合條件查詢
* @param tableName
*/
public static void QueryByCondition3(String tableName) {

try {
HTablePool pool = new HTablePool(configuration, 1000);
HTable table = (HTable) pool.getTable(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 (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "====值:" + new String(keyValue.getValue()));
}
}
rs.close();

} catch (Exception e) {
e.printStackTrace();
}

}

}

熱點內容
c語言中0表示 發布:2025-07-29 10:15:29 瀏覽:246
資料庫單詞 發布:2025-07-29 10:10:23 瀏覽:684
深圳恆波加密軟體破解 發布:2025-07-29 09:24:16 瀏覽:964
本機的伺服器ip 發布:2025-07-29 09:24:05 瀏覽:872
買直播源碼 發布:2025-07-29 09:11:50 瀏覽:257
優學派家長管理初始密碼是多少 發布:2025-07-29 09:11:44 瀏覽:236
貪吃蛇python 發布:2025-07-29 08:57:36 瀏覽:970
微信在電腦文件夾 發布:2025-07-29 08:57:33 瀏覽:903
長安75買哪個配置 發布:2025-07-29 08:38:01 瀏覽:449
什麼安卓版本可以升級鴻蒙 發布:2025-07-29 08:19:12 瀏覽:955