Eclipse 進行Hbase開發步驟
阿新 • • 發佈:2018-11-11
將Hadoop和Hbase拷貝到本地並配置環境變數,如果不配置環境變數會出現如下異常
java.io.IOException: HADOOP_HOME or hadoop.home.dir are not set
將Hbase下lib中的jar包匯入開發環境
在本地開發環境中的Hosts檔案中配置Hbase的IP並把127.0.0.1 xxx註釋
我在沒有配置時發生如下錯誤
簡單建立表的Demo
package club.yuit;
import java.io.IOException;
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.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class App
{
private static final String table="yuit_table";
private static Configuration cf=null;
private static Connection cnn=null;
/**
*
* @param tableName 表名
* @param familys 列族列表
* @throws IOException
*/
public void createTable(TableName tableName,String[] familys) throws IOException {
Admin admin=cnn.getAdmin();
if(admin.tableExists(tableName)){
System.out.println(tableName.toString()+"表已經存在");
}else{
HTableDescriptor descriptor=new HTableDescriptor(tableName);//表物件
HColumnDescriptor columnDescriptor=null;//一個列族物件
for (String fm : familys) {
columnDescriptor=new HColumnDescriptor(fm);
descriptor.addFamily(columnDescriptor);
}
admin.createTable(descriptor);//建立表
System.out.println("表建立成功!!");
}
}
@Before
public void before(){
//System.out.println("程式初始化........");
cf=HBaseConfiguration.create();
try {
cnn=ConnectionFactory.createConnection(cf);//通過配置獲取連結
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println(cf.get("hbase.zookeeper.quorum"));
}
@Test
public void test() throws IOException {
TableName tableName=TableName.valueOf(table);
this.createTable(tableName, new String[]{"fm1","fm2"});
}
@After
public void after() throws IOException {
}
}