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:WindowsSystem32driversetc路径下的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