step3. 使用eclipse打开maven项目并配置
我们需要在项目根目录下执行mvn clean
进行项目的清理
上图提示正在删除target文件夹
然后给本地仓库加权限:
cd ~/.m2
chmod 777 repository
我们打开eclipse,依次点击:
window → \to→ prefiences → \to→ maven → \to→ user settings
浏览选中我们设置的conf/settings.xml
文件
然后点击Apply
然后选择:
在弹出的框中,选择我们安装Maven的路径:
然后点击finish
完成操作并一路确认回去
就会在界面显示有我们安装的maven:
勾选之后点击apply and close
然后给App.java加权限:
step4. 项目访问HBase
在这里,我们将会在项目根目录之下添加HBase目录并加载jar包
创建HBase目录:
cp -a /usr/local/hbase ~/workspace/source/maven_hello/HBase/
打开项目:
打开刚才的项目之后,然后添加jar包:
依次点击
弹框中选择/root/workspace/source/maven_hello/Hbase/hbase/lib下所有jar包
然后配置项目:
cp -a ~/workspace/source/maven_hello/HBase/hbase/conf/hbase-site.xml ~/workspace/source/maven_hello/conf/
在Eclipse菜单中选择Project → \to→ Properties → \to→ java build path→ \to→Libraries→ \to→Add Class Folder,将刚刚增加的conf目录加入到Libraries中
然后给pom.xml添加权限和依赖:
chmod 777 ~/workspace/source/maven_hello/pom.xml
加入:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.25</version> <scope>compile</scope> </dependency>
然后打开App.java,写入代码:
记得将里面的主机名等修改为自己的,切勿盲目CV
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.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.regionserver.BloomType; import org.apache.hadoop.hbase.util.Bytes; public class App { static Configuration conf=HBaseConfiguration.create(); static Connection connection; public static void main( String[] args ) { String tablename="hbase_tb"; try { App.getConnect(); App.createTable(tablename); App.addData(tablename); } catch (Exception e) { e.printStackTrace(); } } public static void getConnect() throws IOException { conf.set("hbase.zookeeper.quorum", "master315"); conf.set("hbase.zookeeper.property.clientPort", "2181"); //conf.set("zookeeper.znode.parent", "/hbase"); try{ connection=ConnectionFactory.createConnection(conf); } catch(IOException e){ } } //创建一张表,通过HBaseAdmin HTableDescriptor来创建 public static void createTable(String tablename) throws Exception { TableName tableName= TableName.valueOf(tablename); Admin admin = connection.getAdmin(); if (admin.tableExists(tableName)) { admin.disableTable(tableName); admin.deleteTable(tableName); System.out.println(tablename + " table Exists, delete ......"); } @SuppressWarnings("deprecation") HTableDescriptor desc = new HTableDescriptor(tableName); @SuppressWarnings("deprecation") HColumnDescriptor colDesc = new HColumnDescriptor("cf1"); colDesc.setBloomFilterType(BloomType.ROWCOL); desc.addFamily(colDesc); desc.addFamily(new HColumnDescriptor("cf2")); admin.createTable(desc); admin.close(); System.out.println("create table success!"); } public static void addData(String tablename) throws Exception { HTable table = (HTable)connection.getTable( TableName.valueOf(tablename)); Put p1=new Put(Bytes.toBytes("row1")); //row key p1.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("name"), Bytes.toBytes("Tom")); p1.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("age"), Bytes.toBytes("12")); p1.addColumn(Bytes.toBytes("cf2"), Bytes.toBytes("math"), Bytes.toBytes("80")); p1.addColumn(Bytes.toBytes("cf2"), Bytes.toBytes("english"), Bytes.toBytes("90")); table.put(p1); table.close(); } }
记得将里面的主机名等修改为自己的,切勿盲目CV
记得打开hadoop,zookeeper,hbase
start-all.sh zkServer.sh start start-hbase.sh
点击运行,则会看到:
表创建成功~
进入HBase的shell,手动查询插入的信息:
显然数据插入成功~
eclipse使用maven项目访问HBase