IDEA遠程連接HBase及其Java API實戰詳解
開放端口
安全組沒開放端口是原罪!!!導致好多BUG費時費力。Hbase悄悄咪咪的用瞭好多端口,比如被我抓到的42239,直接搜索報錯藥不對癥。
Hbase安裝
下載壓縮包
可以去官網下載http://hbase.apache.org/downloads.html
也可以去鏡像下載歷史版本http://archive.apache.org/dist/hbase/
以1.3.2版本為例:
直接下載或者下載到本地再上傳都行,看你哪個快。
wget http://archive.apache.org/dist/hbase/1.3.2/hbase-1.3.2-bin.tar.gz tar -zxvf hbase-1.3.2-bin.tar.gz #解壓 mv hbase-1.3.2-bin /urs/local/hbase
配置hbase-site.xml
cd /usr/local/hbase/conf vi hbase-site.xml
<property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <property> <name>hbase.rootdir</name> <value>/hbase-data</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>master:2181</value> </property>
配置hbase-env.sh
cd /usr/local/hbase/conf echo $JAVA_HOME #若沒有安裝jdk可百度(偷懶) vi hbase-env.sh #添加要一致 export JAVA_HOME=/usr/local/java
運行測試
cd /usr/local/hbase/bin ./start-hbase.sh
ip:16010訪問
域名配置
服務器 vi /etc/hosts
私網ip master
本地 C:\Windows\System32\drivers\etc\hosts
公網ip master
IDEA源碼
目錄結構:
創建一個maven項目並在pom.xml添加依賴:
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.3.2</version> </dependency>
版本是1.3.2,註意和你自己的一致,可以登錄hbase shell時查看。
(
插播反爬信息)博主CSDN地址:https://wzlodq.blog.csdn.net/
log4j.properties配置
log4j.rootLogger=debug, stdout, R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=firestorm.log log4j.appender.R.MaxFileSize=100KB log4j.appender.R.MaxBackupIndex=1 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n log4j.logger.com.codefutures=DEBUG
hbase-site.xml配置
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl" rel="external nofollow" ?> <configuration> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <property> <name>hbase.rootdir</name> <value>/hbase-data</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>master:2181</value> </property> </configuration>
HBaseCRUD.java
package ex3; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import java.io.IOException; public class HbaseCRUD { private static Configuration configuration; private static Connection connection; private static Admin admin; /** * 建立連接 */ public static void init(){ configuration=HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","121.36.240.205"); // 換成你自己的IP configuration.set("hbase.zookeeper.property.clientPort","2181"); try{ connection=ConnectionFactory.createConnection(configuration); admin=connection.getAdmin(); }catch (IOException e){ e.printStackTrace(); } } /** * 關閉連接 */ public static void close(){ try{ if(admin!=null) admin.close(); }catch (IOException e){ e.printStackTrace(); } } /** * 創建表 * @param myTableName 表名 * @param colFamily 列族數組 * @throws IOException */ public static void createTable(String myTableName,String[]colFamily)throws IOException{ TableName tablename = TableName.valueOf(myTableName); if(admin.tableExists(tablename)){ System.out.println("表名已存在!"); } else{ HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename); for(String str:colFamily){ //增加一列 HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); //建表 } } /** * 添加數據 * @param tablename 表名 * @param rowKey 行鍵 * @param colFamily 列族 * @param col 列限定符 * @param val 數據 * @throws IOException */ 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(); } /** * 獲取數據 * @param tablename 表名 * @param rowKey 行鍵 * @param colFamily 列族 * @param col 列限定符 * @throws IOException */ 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); String val = new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())); System.out.println("值為:"+val); table.close(); } }
Client.java
package ex3; import org.apache.log4j.BasicConfigurator; import java.io.IOException; public class Client { public static void main(String[] args) throws IOException { HbaseCRUD.init(); //建表 HbaseCRUD.createTable("student",new String[]{"score"}); //插入數據 HbaseCRUD.insertData("student","lisa","score","Java","85"); HbaseCRUD.insertData("student","lisa","score","C/C++","80"); HbaseCRUD.insertData("student","lisa","score","MySQL","82"); HbaseCRUD.insertData("student","jerry","score","Java","65"); //查詢數據 HbaseCRUD.getData("student","lisa","score","Java"); HbaseCRUD.getData("student","lisa","score","MySQL"); HbaseCRUD.getData("student","jerry","score","Java"); HbaseCRUD.close(); System.out.println("記得一鍵三連~"); } }
點擊運行後需要點耐心等待,灰色INFO、DEBUG信息都可不用管,報錯的話會是紅色字體(評論區交流),有tomcat內味瞭。
原創不易,請勿轉載(
本不富裕的訪問量雪上加霜)
博主首頁:https://wzlodq.blog.csdn.net/
到此這篇關於IDEA遠程連接HBase及其Java API實戰的文章就介紹到這瞭,更多相關idea連接HBase內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- None Found