HbaseDMLAPI
小白的Hbase学习笔记
目录
1.添加具体的数据内容
package com.shujia.hbase;
import org.apache.hadoop.conf.Configuration;
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 Code08PutData {
private static Connection connection;
static {
//创建Configuration对象 用来设置zookeeper 连接
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum","node1:2181,node2:2181,master:2181");//如果端口不是默认2181 那么可以输入
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
System.out.println("未正确获取到连接信息");
}
}
public static void main(String[] args) throws IOException {
//1.Put操作需要先获取Table类对象
Table table = connection.getTable(TableName.valueOf("jan:tbl1"));
//3.创建Put类对象 需要提供RowKey的Bytes数组
//Params: row - row key
Put put=new Put(Bytes.toBytes("1001"));
//添加具体的数据内容
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("22"));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("gender"),Bytes.toBytes("nan"));
//2.put方法需要提供Put类对象
table.put(put);
//关闭连接
table.close();
connection.close();
}
}
scan 'jan:tbl1'
2.获取部分列数据
package com.shujia.hbase;
import org.apache.hadoop.conf.Configuration;
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 Code09GetData {
private static Connection connection;
static {
//创建Configuration对象 用来设置zookeeper 连接
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum","node1:2181,node2:2181,master:2181");//如果端口不是默认2181 那么可以输入
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
System.out.println("未正确获取到连接信息");
}
}
public static void main(String[] args) throws IOException {
//1.Put操作需要先获取Table类对象
Table table = connection.getTable(TableName.valueOf("jan:tbl1"));
Get get = new Get(Bytes.toBytes("1001"));
Result result = table.get(get);
String rowKey=Bytes.toString(result.getRow());
String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
String age = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")));
String gender = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("gender")));
System.out.println("rowKey =>"+rowKey+" "+name+":"+age+":"+gender);
//关闭连接
table.close();
connection.close();
}
}
3.删除添加的数据内容
package com.shujia.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class Code10deleteData {
private static Connection connection;
static {
//创建Configuration对象 用来设置zookeeper 连接
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum","node1:2181,node2:2181,master:2181");//如果端口不是默认2181 那么可以输入
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
System.out.println("未正确获取到连接信息");
}
}
public static void main(String[] args) throws IOException {
Table table = connection.getTable(TableName.valueOf("jan:tbl1"));
Delete delete = new Delete(Bytes.toBytes("1001"));
table.delete(delete);
//关闭连接
table.close();
connection.close();
}
}
scan 'jan:tbl1'
4.查看数据内容
package com.shujia.hbase;
import org.apache.hadoop.conf.Configuration;
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 Code11ScanData {
private static Connection connection;
static {
//创建Configuration对象 用来设置zookeeper 连接
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum","node1:2181,node2:2181,master:2181");//如果端口不是默认2181 那么可以输入
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
System.out.println("未正确获取到连接信息");
}
}
public static void main(String[] args) throws IOException {
Table table = connection.getTable(TableName.valueOf("jan:tbl1"));
Scan scan = new Scan();
//alter 'jan:tbl1',NAME => 'info',VERSIONS = 2
//put数据
//scan 'jan:tbl1',{RAW = true,VERSIONS = 3}
//scan 'jan:tbl1',{RAW = false,VERSIONS = 3}
scan.setMaxVersions(3);
scan.setRaw(true);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
String rowKey= Bytes.toString(result.getRow());
String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
String age = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")));
String gender = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("gender")));
System.out.println("rowKey =>"+rowKey+" "+name+":"+age+":"+gender);
}
//关闭连接
table.close();
connection.close();
//Put put=new Put(Bytes.toBytes("1002"));
}
}
5.查看一个RowKey中所有数据内容
package com.shujia.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import java.io.IOException;
public class Code12ScanData {
private static Connection connection;
static {
//创建Configuration对象 用来设置zookeeper 连接
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum","node1:2181,node2:2181,master:2181");//如果端口不是默认2181 那么可以输入
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
System.out.println("未正确获取到连接信息");
}
}
public static void main(String[] args) throws IOException {
Table table = connection.getTable(TableName.valueOf("jan:tbl1"));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
String rowKey = Bytes.toString(result.getRow());
//获取一个RowKey中的所有Cells
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
String Family = Bytes.toString(CellUtil.cloneFamily(cell));
String Qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String Value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("rowKey:"+rowKey+"Family:"+Family+"Qualifier:"+ Qualifier +"Value:"+Value);
}
}
//关闭连接
table.close();
connection.close();
}
}
刚才我们删除掉了所有添加的信息 现在我们重新Put一下 再查询
现在我们多put几条数据 如果还是使用上面的代码 只显示一个添加的数据
我们做一下修改 显示全部
在这之前 我们需要先修改一下版本号
alter 'jan:tbl1',NAME => 'info',VERSIONS => 2
package com.shujia.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import java.io.IOException;
public class Code12ScanData {
private static Connection connection;
static {
//创建Configuration对象 用来设置zookeeper 连接
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum","node1:2181,node2:2181,master:2181");//如果端口不是默认2181 那么可以输入
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
System.out.println("未正确获取到连接信息");
}
}
public static void main(String[] args) throws IOException {
Table table = connection.getTable(TableName.valueOf("jan:tbl1"));
Scan scan = new Scan();
//alter 'jan:tbl1',NAME => 'info',VERSIONS = 2
//put数据
//scan 'jan:tbl1',{RAW = true,VERSIONS = 3}
//scan 'jan:tbl1',{RAW = false,VERSIONS = 3}
scan.setMaxVersions(3);
scan.setRaw(true);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
String rowKey = Bytes.toString(result.getRow());
//获取一个RowKey中的所有Cells
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
String Family = Bytes.toString(CellUtil.cloneFamily(cell));
String Qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String Value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("rowKey:"+rowKey+"Family:"+Family+"Qualifier:"+ Qualifier +"Value:"+Value);
}
}
//关闭连接
table.close();
connection.close();
}
}
6.需求:将多条数据put至表中
package com.shujia.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
//需求: 将多条数据Put至表中
//不推荐按使用
public class Code13PutData {
private static Connection connection;
static {
//创建Configuration对象 用来设置zookeeper 连接
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum","node1:2181,node2:2181,master:2181");//如果端口不是默认2181 那么可以输入
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
System.out.println("未正确获取到连接信息");
}
}
public static void main(String[] args) throws IOException {
Table table = connection.getTable(TableName.valueOf("jan:tbl1"));
BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\IDEA_workplace\\hbasedemo15\\data\\students.txt"));
String line;
while( (line = bufferedReader.readLine()) != null){
String[] column=line.split(",");
Put put = new Put(Bytes.toBytes(column[0])); //RowKey
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes(column[1]));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(column[2]));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("gender"),Bytes.toBytes(column[3]));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("clazz"),Bytes.toBytes(column[4]));
//table.put操作每次都会去Hbase中访问 耗费时间
//如果数据量大 那么访问消耗的时间会很长
table.put(put);
}
//scan 'jan:tbl1',{RAW => true,VERSION => 3}
//关闭连接
table.close();
connection.close();
}
}
7.需求:将多条数据put至表中(工作中推荐使用)
truncate 'jan:tbl1' 清空表
package com.shujia.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
//工作中推荐使用如下方法
public class Code14PutData {
private static Connection connection;
static {
//创建Configuration对象 用来设置zookeeper 连接
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum","node1:2181,node2:2181,master:2181");//如果端口不是默认2181 那么可以输入
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
System.out.println("未正确获取到连接信息");
}
}
public static void main(String[] args) throws IOException {
Table table = connection.getTable(TableName.valueOf("jan:tbl1"));
//创建读取IO流 进行数据读取
BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\IDEA_workplace\\hbasedemo15\\data\\students.txt"));
String line;
List<Put> putList = new ArrayList<>();
while( (line = bufferedReader.readLine()) != null){
String[] columns=line.split(",");
Put put = new Put(Bytes.toBytes(columns[0])); //RowKey
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes(columns[1]));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(columns[2]));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("gender"),Bytes.toBytes(columns[3]));
put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("clazz"),Bytes.toBytes(columns[4]));
putList.add(put);
//putList中的数据>=100时 对数据进行上传并清空putList 防止造成数据重复上传
if(putList.size() >= 100){
table.put(putList);
putList.clear();
}
//putList中的数据没有上传完 继续上传
if(!putList.isEmpty()){
table.put(putList);
}
}
//truncate 'jan:tbl1'
//scan 'jan:tbl1',{RAW => true,VERSION => 3}
//关闭连接
table.close();
connection.close();
}
}