创建Maven项目
配置pom.xml
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hbaseDemo</groupId>
<artifactId>hbaseDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hbaseDemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!--https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.3</version>
</dependency>
<!--https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
</dependencies>
</project>
题外话,如果是WEB项目,还需要在pom.xml中导入javaee-api.jar
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
packagehbaseDemo.hbaseDemo;
importjava.io.IOException;
import java.util.*;
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.hbase.HBaseConfiguration;
importorg.apache.hadoop.hbase.HColumnDescriptor;
importorg.apache.hadoop.hbase.HTableDescriptor;
importorg.apache.hadoop.hbase.TableName;
importorg.apache.hadoop.hbase.util.Bytes;
importorg.apache.hadoop.hbase.client.Put;
importorg.apache.hadoop.hbase.client.Table;
importorg.apache.hadoop.hbase.client.ConnectionFactory;
importorg.apache.hadoop.hbase.client.Admin;
importorg.apache.hadoop.hbase.client.Connection;
public class App
{
private staticConfiguration conf = HBaseConfiguration.create();
static {
conf.set("hbase.rootdir", "hdfs://master:9000/hbase");
// 设置Zookeeper,直接设置IP地址
conf.set("hbase.zookeeper.quorum", "master,slave1,slave2");
}
// 创建表
public static voidcreateTable(String tablename, String columnFamily) throwsException {
Connection connection =ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
TableName tableNameObj = TableName.valueOf(tablename);
if (admin.tableExists(tableNameObj)) {
System.out.println("Tableexists!");
System.exit(0);
} else {
HTableDescriptor tableDesc = newHTableDescriptor(TableName.valueOf(tablename));
tableDesc.addFamily(newHColumnDescriptor(columnFamily));
admin.createTable(tableDesc);
System.out.println("createtable success!");
}
admin.close();
connection.close();
}
// 删除表
public static voiddeleteTable(String tableName) {
try {
Connection connection =ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
TableName table = TableName.valueOf(tableName);
admin.disableTable(table);
admin.deleteTable(table);
System.out.println("deletetable " + tableName + "ok.");
} catch (IOException e) {
e.printStackTrace();
}
}
// 插入一行记录
public static voidaddRecord(String tableName, String rowKey,String family, String qualifier,String value){
try {
Connection connection =ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier), Bytes.toBytes(value));
put.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier), Bytes.toBytes(value));
table.put(put);
table.close();
connection.close();
System.out.println("insertrecored " + rowKey + " totable " + tableName + "ok.");
} catch (IOException e) {
e.printStackTrace();
}
}
public static voidmain(String[] args) throws Exception {
App.createTable("testTb", "info");
App.addRecord("testTb", "001", "info", "name", "zhangsan");
App.addRecord("testTb", "001", "info", "age", "20");
//HbaseDao.deleteTable("testTb");
}
}