java API连接hbase_基础版
父工程pom.xml配置
<?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>org.example</groupId>
<artifactId>java-hadoop</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>hadoop-hdfs</module>
<module>hadoop-mapreduce</module>
<module>hadoop-sanguodemo</module>
<module>anli-tuijian</module>
<module>airPM25avg</module>
<module>weather</module>
<module>hadoop-hive</module>
<module>hadoop-hbase</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<hadoop-version>2.7.6</hadoop-version>
<junit-version>4.12</junit-version>
</properties>
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.janeluo/ikanalyzer -->
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
子项目pom.xml配置
<?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">
<parent>
<artifactId>java-hadoop</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hadoop-hbase</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
</dependency>
</dependencies>
</project>
java连接hbase_连接-操作-创建表-删除表-插入数据-关闭连接
package com.shujia;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* 思考:
* 1.如何将所有的学生成绩查询出来?有几种方式?
* 2. 假如给我们一个条件查询,你会不会?比如说:查姓王,年龄在24一下,理科的,女生
* 3. 如何用创建region呢?
* 4,如何设置过期时间?
* 5,hbase 的读写流程是什么样的?你可以自己画出来吗?
* 6.hbase中涉及的数据结构有哪些?
* 7.二级索引了解一下
* 8.如何通过代码实现二级索引,有什么好处?
*
*/
/**
* @author WangTao
* @date 2022/6/10 23:12
*/
public class ClintToHbase {
private HConnection conn = null;
private HBaseAdmin hAdmin = null;
@Before
public void connect(){
try {
//1.获取Hadoop的相关配置环境
Configuration conf = new Configuration();
//获取zookeeper的配置
conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");
//获取与Hbase 的连接,这个连接是将来可以用户获取hbase表的
conn = HConnectionManager.createConnection(conf);
//将来我们要对表做DDL相关的操作,而对表的操作在hbase架构中是有HMaster的
hAdmin = new HBaseAdmin(conf);
System.out.println("建立连接成功:"+conn+",HMaster建立连接成功:"+hAdmin);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 在HBase中创建表
*<p>
*create 'students','info'
*/
@Test
public void createTable(){
try {
//使用 HTableDescriptor类创建一个表对象
HTableDescriptor students = new HTableDescriptor("students");
//在创建表的时候,至少指定一个列簇
HColumnDescriptor info = new HColumnDescriptor("info");
//将列添加到表中
students.addFamily(info);
//真正的执行,是由 HMAster
//hAdmin
hAdmin.createTable(students);
System.out.println(Bytes.toString(students.getName())+"表创成功!!!");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void deleteTable(){
try {
if(hAdmin.tableExists("students")){
// 1.首先判断一下表是否存在
// 直接删除是删除不掉的
// 先禁用表,在删除表
hAdmin.disableTable("students");
hAdmin.deleteTable("students");
System.out.println(conn.getTable("students").getName()+"表,已经被删除!!!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 往表中添加一条数据
* <p>
* put 'student','1001','info:name','WTao'
*/
@Test
public void putData(){
try {
//1.获取表的实例
HTableInterface students = conn.getTable("students");
//创建一个put实例,并且给一个行键(rowkey)
Put put = new Put("1001".getBytes());
//将这个行键对应的值,添加列簇,列,具体的值
put.add("info".getBytes(), "name".getBytes(), "aTao".getBytes());
//表的实例将数据添加到表中
students.put(put);
String s = put.toString();
System.out.println(s);
System.out.println(students.getName() + "表,成功插入一条数据");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 一次性往表中添加多条数据
*
* students2.txt
*/
@Test
public void putAll(){
try {
//获取表的实例
HTableInterface students = conn.getTable("students");
//创建一个List集合对象
ArrayList<Put> puts = new ArrayList<>();
//读取数据----- IO
BufferedReader br = new BufferedReader(new FileReader("D:\\Softwore_java\\projects\\java-hadoop\\hadoop-hbase\\src\\main\\java\\com\\shujia\\students2.txt"));
String line = null;
while ((line = br.readLine()) != null){
String[] split = line.split(",");
String id = split[0];
String name = split[1];
String age = split[2];
String gender = split[3];
String clazz = split[4];
//把每一行组成一个put对象
Put put = new Put(id.getBytes());
//为一行添加多列
put.add("info".getBytes(),"name".getBytes(),name.getBytes());
put.add("info".getBytes(),"age".getBytes(),age.getBytes());
put.add("info".getBytes(),"gender".getBytes(),gender.getBytes());
put.add("info".getBytes(),"clazz".getBytes(),clazz.getBytes());
//每一行组成一个put对象后添加到集合中
//put 'students',id,'info:name',name
//put 'students',id,'info:age',age
//put 'students',id,'info:gender',gender
//put 'students',id,'info:clazz',clazz
puts.add(put);
}
//调用表的put方法,将集合数据添加到表中
students.put(puts);
System.out.println("学生信息表添加完毕!!");
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void close(){
if(conn!=null){
try {
conn.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("conn已经关闭...");
}
if(hAdmin!=null){
try {
hAdmin.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("hMaster已经关闭...");
}
}
}