生活随笔
收集整理的這篇文章主要介紹了
hbase伪分布式配置
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.在單機模式的基礎(chǔ)上進行配置,打開hbase-env.sh。
vim /usr/local/hbase/conf/hbase-env.sh
2.配置HBASE_CLASSPATH為hadoop安裝目錄下的conf目錄,即 /usr/local/hadoop/conf。JAVA_HOME、HBASE_MANAGES_ZK之前已經(jīng)配置好了。
export HBASE_CLASSPATH
=/usr/local/hadoop/conf
3.打開hbase-site.xml文件,hbase.rootdir指定hbase數(shù)據(jù)在HDFS的存儲路徑;將hbase.cluter.distributed設(shè)置為true。
vim /usr/local/hbase/conf/hbase-site.xml
配置信息如下
<configuration
><property
><name
>hbase.rootdir
</name
><value
>hdfs://localhost:9000/hbase
</value
></property
><property
><name
>hbase.cluster.distributed
</name
><value
>true
</value
></property
><property
><name
>hbase.unsafe.stream.capability.enforce
</name
><value
>false
</value
></property
>
</configuration
>
4.Hbase的啟動測試,先啟動hadoop,在啟動hbase。
ssh localhost
cd /usr/local/hadoop
./sbin/start-dfs.sh
jps
cd /usr/local/hbase
bin/start-hbase.sh
jps
最終看到如下信息,則成功開啟。
5.關(guān)閉, 先關(guān)閉hbase,在關(guān)閉hadoop。
bin/stop-hbase.sh
cd /usr/local/hadoop
./sbin/stop-dfs.sh
6.Hbase shell 命令
hbase shell
在這里插入代碼片
7.Java API編程
- 導(dǎo)入hbase的lib目錄下的所有jar包。
package my
.hbase
;
import org
.apache
.hadoop
.conf
.Configuration
;
import org
.apache
.hadoop
.hbase
.*
;
import org
.apache
.hadoop
.hbase
.client
.*
;
import org
.apache
.hadoop
.hbase
.util
.Bytes
;import java
.io
.IOException
;public class HbaseCode {public static Configuration configuration
;public static Connection connection
;public static Admin admin
;public static void main(String
[] args
)throws IOException
{init();createTable("student-info",new String[]{"score"});insertData("student-info","zhangsan","score","English","69");insertData("student-info","zhangsan","score","Math","86");insertData("student-info","zhangsan","score","Computer","77");getData("student-info", "zhangsan", "score","English");close();}public static void init(){configuration
= HBaseConfiguration
.create();configuration
.set("hbase.rootdir","hdfs://localhost:9000/hbase");try{connection
= ConnectionFactory
.createConnection(configuration
);admin
= connection
.getAdmin();}catch (IOException e
){e
.printStackTrace();}}public static void close(){try{if(admin
!= null
){admin
.close();}if(null
!= connection
){connection
.close();}}catch (IOException e
){e
.printStackTrace();}}public static void createTable(String myTableName
,String
[] colFamily
) throws IOException
{TableName tableName
= TableName
.valueOf(myTableName
);if(admin
.tableExists(tableName
)){System
.out
.println("talbe is exists!");}else {TableDescriptorBuilder tableDescriptor
= TableDescriptorBuilder
.newBuilder(tableName
);for(String str
:colFamily
){ColumnFamilyDescriptor family
= ColumnFamilyDescriptorBuilder
.newBuilder(Bytes
.toBytes(str
)).build();tableDescriptor
.setColumnFamily(family
);}admin
.createTable(tableDescriptor
.build());} }public static void insertData(String tableName
,String rowKey
,String colFamily
,String col
,String val
) throws IOException
{ Table table
= connection
.getTable(TableName
.valueOf(tableName
));Put put
= new Put(rowKey
.getBytes());put
.addColumn(colFamily
.getBytes(),col
.getBytes(), val
.getBytes());table
.put(put
);table
.close(); }public static void getData(String tableName
,String rowKey
,String colFamily
, String col
)throws IOException
{ Table table
= connection
.getTable(TableName
.valueOf(tableName
));Get get
= new Get(rowKey
.getBytes());get
.addColumn(colFamily
.getBytes(),col
.getBytes());Result result
= table
.get(get
);System
.out
.println(new String(result
.getValue(colFamily
.getBytes(),col
==null
?null
:col
.getBytes())));table
.close(); }
}
運行結(jié)果查看如下student-info表。
8.源鏈接
廈門大學(xué)數(shù)據(jù)庫實驗室
總結(jié)
以上是生活随笔為你收集整理的hbase伪分布式配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。