好久沒搭起來測測了,今天順手搭起來用用。結果遇到了很多問題,下面一一說明
一、部署
簡單起見,我使用了stand-alone模式。直接下載解壓即可,注意的是需要改動hbase-site.xml:
<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:///root/zhangtieying/hbase</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/root/zhangtieying/zookeeper</value>
</property>
</configuration>127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 10.1.102.104 ccf04 10.20.14.16 dt16 10.20.14.17 dt17 10.20.14.18 dt18 10.20.14.19 dt19 10.20.14.20 dt20 10.20.14.21 dt21 10.20.14.22 dt22 10.20.14.23 dt23 10.20.14.24 dt24 10.20.14.25 dt25
ccf04添加為主機名的方法:vim /etc/sysconfig/network:
NETWORKING=yes HOSTNAME=ccf04
二、java客戶端連接
這裡給出我的測試代碼(部分代碼是copy的)
/**
* Created by Michael on 2015/6/20.
*/
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;
public class TestHbase {
static Configuration conf=null;
static{
conf=HBaseConfiguration.create();//hbase的配置信息
conf.set("hbase.zookeeper.quorum", "10.1.102.104:2181"); //zookeeper的地址
}
public static void main(String[] args)throws Exception {
TestHbase t=new TestHbase();
t.createTable("test-zty", new String[]{"name","age"});
//t.insertRow("test-zty", "2", "age", "myage", "100");
// t.getOneDataByRowKey("test-zty", "2");
//t.showAll("test");
}
/***
* 創建一張表
* 並指定列簇
* */
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);
List<Delete> list=new ArrayList<Delete>();
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();
}
}
工程使用maven,其pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.codesolid</groupId>
<artifactId>HelloJUnit</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase</artifactId>
<version>0.94.5</version>
</dependency>
</dependencies>
</project>This server is in the failed servers list: localhost/127.0.0.1:60718
出現該問題的原因是沒有在服務端配置主機名和真實ip的映射,因此需要再/etc/hosts中加上10.1.102.104 ccf04(ccf04為服務器的主機名)
同時,需要在你跑程序的機器上添加ccf04的映射,我的是在windows上跑的java程序,需要修改windows的host文件,最後一行加入10.1.102.104 ccf04即可