一:在eclipse創建一個普通的java項目


二:新建一個文件夾,把hbase需要的jar放進去,我這里把hbase/lib/*.jar 下所有的jar都放進去了,最后發現就用到了下面三個jar包
hadoop-common-2.6.4.jar
hbase-common-1.2.4.jar
hbase-client-1.2.4.jar
三:
將你在服務器中配置的hbase-site.xml文件復制到ecplise工程里
(在工程的跟目錄下新建一個文件夾命名conf,右鍵conf文件--->build path---->use as source folder)

四:新創建一個測試類,就可以寫代碼了
package com.zhang.insert; import java.io.IOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; 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.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.util.Bytes; import org.junit.Test; public class HbaseInsert1 { public static Log log=LogFactory.getLog(HbaseInsert1.class); public static String tableName="employees";//表名 public static String columnFamily="student_name";//列族 public static String rowFamily="h2";//行鍵 static Configuration conf=null; static{ Configuration confi=new HBaseConfiguration().create(); conf=confi; //指定zookeeper conf.set("hbase.zookeeper.quorum", "node4"); System.setProperty("hadoop.home.dir", "E:\\hadoop-2.6.4");//設置hadoop的安裝目錄,如果在eclipse已經設置過可以不用設置 } /** * 創建表 * HBaseAdmin * HTableDescriptor */ @Test public void testCreateTable(){ HBaseAdmin admin=null; HTableDescriptor htd=null; try { admin=new HBaseAdmin(conf); //判斷要創建的表是否已經存在 if(admin.tableExists(al_TableName)){ System.out.println(tableName+"表已經存在"); return; }else{ htd=new HTableDescriptor(al_TableName.getBytes()); //設置列族 htd.addFamily(new HColumnDescriptor(columnFamily)); admin.createTable(htd); System.out.println(tableName+"表創建成功!!!"); } }catch (IOException e) { e.printStackTrace(); } } /** * 刪除表 */ @Test public void testDeletTable(){ HBaseAdmin admin=null; try { admin=new HBaseAdmin(conf); if(admin.tableExists(tableName)){ System.out.println("table is exits"); //如果表存在,則注為失效狀態 admin.disableTable(tableName);//刪除表 admin.deleteTable(tableName); System.out.println("刪除成功"); } } catch (IOException e) { e.printStackTrace(); }
問題:
1,在創建表的時候拋出異常
java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
這個異常是因為你缺少
winutils.exe
需要下載這個文件,然后放到你的/hadoop/bin/ 即可

2:
2017-01-04 22:20:05,567 INFO [main-SendThread(127.0.0.1:2181)] zookeeper.ClientCnxn (ClientCnxn.java:logStartConnect(975)) - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
下面異常是因為連接的zk不對,需要指定你的zk
Configuration conf=new HBaseConfiguration().create();
conf.set("hbase.zookeeper.quorum", "node4");//node4 是你的服務器的名稱
3:這個異常是我在創建表后,使用'scan'和'put'都會拋出下面錯誤
ERROR: No server address listed in hbase:meta for region employess_info,,1483706778862.6d09a04dec7bfba654d6b393ba966534. containing row
我使用 scan 'hbase:meta'查詢hbase元數據
ROW COLUMN+CELL
hbase:namespace,,1482832588064.6899522395b4ee74 column=info:regioninfo, timestamp=1482832590934, value={ENCODED => 6899522395b4ee74d8a55be4b8f31fb9, NAME => 'hbase:namespace,,1482832588064.6
d8a55be4b8f31fb9. 899522395b4ee74d8a55be4b8f31fb9.', STARTKEY => '', ENDKEY => ''}
hbase:namespace,,1482832588064.6899522395b4ee74 column=info:seqnumDuringOpen, timestamp=1483554191715, value=\x00\x00\x00\x00\x00\x00\x00\x0D
d8a55be4b8f31fb9.
hbase:namespace,,1482832588064.6899522395b4ee74 column=info:server, timestamp=1483554191715, value=hadoop-node4.com:16201
d8a55be4b8f31fb9.
hbase:namespace,,1482832588064.6899522395b4ee74 column=info:serverstartcode, timestamp=1483554191715, value=1483554113504
d8a55be4b8f31fb9.
student,,1482855412525.d49d109d9fe387bdcda9f748 column=info:regioninfo, timestamp=1482855420324, value={ENCODED => d49d109d9fe387bdcda9f748f2d959e3, NAME => 'student,,1482855412525.d49d109d9
f2d959e3. fe387bdcda9f748f2d959e3.', STARTKEY => '', ENDKEY => ''}
student,,1482855412525.d49d109d9fe387bdcda9f748 column=info:seqnumDuringOpen, timestamp=1483554191720, value=\x00\x00\x00\x00\x00\x00\x00\x0D
f2d959e3.
student,,1482855412525.d49d109d9fe387bdcda9f748 column=info:server, timestamp=1483554191720, value=hadoop-node4.com:16201
f2d959e3.
student,,1482855412525.d49d109d9fe387bdcda9f748 column=info:serverstartcode, timestamp=1483554191720, value=1483554113504
f2d959e3.
student_1,,1483556027914.ded9aeece2edc985bf9999 column=info:regioninfo, timestamp=1483556033173, value={ENCODED => ded9aeece2edc985bf9999d0744fad1e, NAME => 'student_1,,1483556027914.ded9aee
d0744fad1e. ce2edc985bf9999d0744fad1e.', STARTKEY => '', ENDKEY => ''}
3 row(s) in 0.4820 seconds
(這個異常是因為我的eclipse工程里沒有hbase-site.xml文件,所以操作是habse默認的。因為我在創建表后,對表進行操作雖然報錯,但是我重啟集群之后,再對表進行操作就沒問題了,所以我覺得是ecplise的問題,不是hbase的問題,查看hbase:meta數據也是正常的)