1.保證集群開啟:
jps有如下進程
2.pom文件中的依賴
<?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.zhiyou100</groupId>
<artifactId>hbasedemo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hbase.version>1.4.8</hbase.version>
</properties>
<dependencies>
<!--hbase-->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>${hbase.version}</version>
</dependency>
<!--要使用HBase的MapReduce API-->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>${hbase.version}</version>
</dependency>
</dependencies>
</project>
3.編寫配置文件:hbase-site.xml
將集群上$HBASE_HOME/conf/hbase-site.xml拷貝過來就可以,也可以直接復制下面內容
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://master2:9000/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<!--conf.set("hbase.zookeeper.quorum", "master2");-->
<!--// 設置Zookeeper,直接設置IP地址-->
<property>
<name>hbase.zookeeper.quorum</name>
<value>master2</value>
</property>
</configuration>
---注意:如果沒有再本地的C:\Windows\System32\drivers\etc路徑下的hosts文件中配置IP 與之對應的主機名
請將上面的主機名(master2)填寫成自己的IP
4.編寫java代碼--創建類:HBaseDDL
package com.hbase.DDL;
import com.google.common.io.Resources;
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.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBaseDDL {
private static Configuration configuration;
private static Connection connection;
private static Admin admin;
static {
//1.獲得Configuration實例並進行相關設置
configuration = HBaseConfiguration.create();
configuration.addResource(Resources.getResource("hbase-site.xml"));
//2.獲得Connection實例
try {
connection = ConnectionFactory.createConnection(configuration);
//3.1獲得Admin接口
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
//創建表
String familyNames[]={"nature","social"};
createTable("test_hbase",familyNames);
//向表中插入數據
insert("test_hbase","libai","nature","height","25");
//刪除表
dropTable("test_hbase");
}
/**
* 創建表
* @param tableName 表名
* @param familyNames 列族名
* */
public static void createTable(String tableName, String familyNames[]) throws IOException {
//如果表存在退出
if (admin.tableExists(TableName.valueOf(tableName))) {
System.out.println("Table exists!");
return;
}
//通過HTableDescriptor類來描述一個表,HColumnDescriptor描述一個列族
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
for (String familyName : familyNames) {
tableDescriptor.addFamily(new HColumnDescriptor(familyName));
}
//tableDescriptor.addFamily(new HColumnDescriptor(familyName));
admin.createTable(tableDescriptor);
System.out.println("createtable success!");
}
/**
* 刪除表
* @param tableName 表名
* */
public static void dropTable(String tableName) throws IOException {
//如果表不存在報異常
if (!admin.tableExists(TableName.valueOf(tableName))) {
System.out.println(tableName+"不存在");
return;
}
//刪除之前要將表disable
if (!admin.isTableDisabled(TableName.valueOf(tableName))) {
admin.disableTable(TableName.valueOf(tableName));
}
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("deletetable " + tableName + "ok.");
}
/**
* 指定行/列中插入數據
* @param tableName 表名
* @param rowKey 主鍵rowkey
* @param family 列族
* @param column 列
* @param value 值
* TODO: 批量PUT
*/
public static void insert(String tableName, String rowKey, String family, String column, String value) throws IOException {
//3.2獲得Table接口,需要傳入表名
Table table =connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
System.out.println("insertrecored " + rowKey + " totable " + tableName + "ok.");
}
/**
* 刪除表中的指定行
* @param tableName 表名
* @param rowKey rowkey
* TODO: 批量刪除
*/
public static void delete(String tableName, String rowKey) throws IOException {
//3.2獲得Table接口,需要傳入表名
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
}
}
測試:1.創建表-----createTable("test_hbase",familyNames);
在hbase shell中list查看表
2.向創建的表中插入數據-----insert("test_hbase","luban","nature","height","250");
在hbase shell中scan 'test_hbase'查看表內容
3.刪除表------dropTable("test_hbase");
在hbase shell中list查看表,已經沒有了test_hbase