溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

windows下Java操作hbase-----HBase(偽分布)學(xué)習(xí)筆記

發(fā)布時間:2020-03-04 06:23:22 來源:網(wǎng)絡(luò) 閱讀:1746 作者:陳安一 欄目:關(guān)系型數(shù)據(jù)庫

一、修改hosts文件

    進(jìn)入C:\Windows\System32\drivers\etc目錄

    編輯hosts文件,在底部添加  10.10.10.10(要跟linux機(jī)器中hosts里的ip一樣) hbase

    保存退出

二、安裝maven(方便管理jar包,也可以不安裝)

    具體安裝步驟參考http://blog.csdn.net/chenxuejiakaren/article/details/7938524

    在eclipse中安裝請參考http://blog.csdn.net/wode_dream/article/details/38052639

三、創(chuàng)建項(xiàng)目

    新建->項(xiàng)目->選擇maven文件夾下的maven project->類型選擇maven-archetype-quickstart->

group id 是和artifact id 組合起來是完整的 package,一般artifact id 是項(xiàng)目名->建好之后,雙擊pom.xml->切換到最后一個卡pom.xml->

 在 dependencies 里追加這么一截 

 <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>0.98.9-hadoop2</version>
</dependency> 

    保存就OK了!

如果報(bào)錯:

    Missing artifact jdk.tools:jdk.tools

再追加這一截

    <dependency>

    <groupId>jdk.tools</groupId>

    <artifactId>jdk.tools</artifactId>

    <version>1.8</version>

    <scope>system</scope>

    <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>

</dependency>

    

四、操作hbase

    在包下面自己新建class

    

注意,調(diào)用的時候要直接

    DBhelper.方法();

package 包名;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
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.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;
public class DBhelper {
 public static Configuration config = new Configuration();
 static{
config.set("hbase.zookeeper.property.clientPoint", "2181");
config.set("hbase.zookeeper.quorum", "hbase");
 }
 /*
  * 按條件查詢value
  * 參數(shù):表名,行,列簇,列名
  */
 public static void get(String tablename,String rowKey,String Family,String qualifier)throws Exception{
 HTable h=new HTable(config,tablename);
 System.out.println("開始獲取------嗶嗶嗶");
 Get get=new Get(rowKey.getBytes());
 get.addColumn(Family.getBytes(), qualifier.getBytes());
 Result r=h.get(get);
for(KeyValue kv:r.raw())
{
System.out.println("您要查的值為"+new String(kv.getValue()));
}
 }
 /*
  * 掃描table
  * 參數(shù):表名
  */
 public static void scan(String tablename)throws Exception{
 HTable h=new HTable(config,tablename);
 System.out.println("開始掃描");
 ResultScanner rs=h.getScanner(new Scan(tablename.getBytes()));
 for(Result r:rs){
 for(KeyValue kv:r.raw()){
  System.out.println(new String(kv.getRow())+"   "+new String(kv.getFamily())+"   "+new String(kv.getQualifier())+"   "+new String(kv.getValue())+"   "+kv.getTimestamp());
 }
 }
 System.out.println("掃描結(jié)束");
 }
 /*
  * 添加或者修改數(shù)據(jù)
  * 參數(shù):表名,行,列簇,列名,值
  */
 public static void put(String tablename,String rowKey,String Family,String qualifier,String value)throws Exception{
 HTable h=new HTable(config,tablename);
 System.out.println("- - - - - -  - -  - -  - 正在添加- - - - - - - - - - - - ");
 Put put =new Put(Bytes.toBytes(rowKey));
 put.add(Bytes.toBytes(Family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
 h.put(put);
 System.out.println("- - - - - -  - -  - -  - 添加完成- - - - - - - - - - - - ");
 }
 
 
 /*
  * 刪除table
  * 參數(shù):表名
  */
 public static void Delete(String tablename) throws Exception{
 HBaseAdmin h=new HBaseAdmin(config);
 if(h.tableExists(tablename)){
 System.out.println("正在刪除"+tablename);
    h.disableTable(tablename);
 h.deleteTable(tablename);
 System.out.println("刪除成功");
 }else{
 System.out.println("傻??!"+tablename+"不存在");
 }
 }
 
 
 /*
  * 創(chuàng)建table
  * 參數(shù):表名+列簇
  */
 public static void Create(String tablename,String Col)throws Exception{
 HBaseAdmin admin=new HBaseAdmin(config);
if(admin.tableExists(tablename)){
 System.out.println(tablename+"已存在了!");
}else{
System.out.println("正在創(chuàng)建table:"+tablename);
HTableDescriptor htd=new HTableDescriptor(tablename);
System.out.println("創(chuàng)建成功!正在添加列簇:"+Col);
htd.addFamily(new HColumnDescriptor(Col));
admin.createTable(htd);
System.out.println("創(chuàng)建完成:/t 表名:"+tablename+"\t 列簇:"+Col);
}
 }
}

    

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI