10.18周总结

  

1)   Hadoop学习------Hbase配置

学习内容链接:

入门HBase : https://www.jianshu.com/p/b23800d9b227

 

2)  Hbase学习

8.1 启动HBase

cd /usr/local/hbase
start-hbase.sh

结束HBase

stop-hbase.sh

 

8.2 进入HBase的Shell

hbase shell

退出shell

quit

 

8.3 表的操作

 create 'test', 'cf'                #创建表    必须有表名称和列簇名

 

 

3)Hbase的api学习

 


package main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.commons.math3.analysis.function.Add;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TableNotFoundException;
import org.apache.hadoop.hbase.client.Admin;
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.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;


public class hebeidiaoyan {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;

//**********连接操作**********
//建立链接
public static void getConnection() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://192.168.78.131:9000/hbase");
configuration.set("hbase.zookeeper.quorum","192.168.78.131");
configuration.set("hbase.zookeeper.property.clientPort","2181");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//关闭链接
public static void close() {
try {
if(admin!=null) {
admin.close();
}
if(null!=connection) {
connection.close();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

//**********命名空间操作**********
//创建namespace
public static void createNamespace(String namespace) throws IOException {
getConnection();
NamespaceDescriptor nDescriptor = NamespaceDescriptor.create(namespace).build();
admin.createNamespace(nDescriptor);
close();
}
//查询所有的namespace
public static ArrayList<String> listNamespace() throws IOException {
getConnection();
NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
ArrayList<String> list = new ArrayList<>();
for(NamespaceDescriptor nd:namespaceDescriptors) {
System.out.println(nd.getName());
list.add(nd.getName());
}
close();
return list;
}
//获取指定namespace中所有的表名
public static ArrayList<String> listTables(String namespace) throws IOException {
getConnection();
HTableDescriptor[] tables = admin.listTableDescriptorsByNamespace(namespace);
ArrayList<String> list = new ArrayList<>();
for(HTableDescriptor table:tables) {
// System.out.println(table.getNameAsString());
list.add(table.getTableName().getNameAsString());
}
close();
return list;
}
//删除namespace
public static boolean dropNamespace(String namespace) {
getConnection();
try {
admin.deleteNamespace(namespace);
close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
close();
return false;
}
}

//**********表操作**********
//创建表
public static void createTable(String tablename,String[] colFamily) throws IOException {
getConnection();
TableName tName = TableName.valueOf(tablename);
if(admin.tableExists(tName)) {
System.out.println("table exists");
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tName);
for(String str:colFamily) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
}
close();
}
//获取所有列族
public static ArrayList<String> listColFamilies(String tablename) throws TableNotFoundException, IOException{
getConnection();
HTableDescriptor tableDescriptor = admin.getTableDescriptor(TableName.valueOf(tablename));
HColumnDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();
ArrayList<String> list = new ArrayList<>();
for(HColumnDescriptor hcd:columnDescriptors) {
System.out.println(hcd.getNameAsString());
list.add(hcd.getNameAsString());
}
return list;
}
//删除列族
public static boolean deleteColFamily(String tablename,String colname) {
getConnection();
try {
admin.deleteColumn(TableName.valueOf(tablename), Bytes.toBytes(colname));
close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
close();
return false;
}
}
//删除表
@SuppressWarnings("finally")
public static boolean dropTable(String tablename) {
getConnection();
try {
HTableDescriptor descriptor = admin.getTableDescriptor(TableName.valueOf(tablename));
if(admin.tableExists(TableName.valueOf(tablename))) {
if(admin.isTableEnabled(TableName.valueOf(tablename))) {
admin.disableTable(TableName.valueOf(tablename));
}
admin.deleteTable(TableName.valueOf(tablename));
}
close();
return true;
} catch (TableNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
close();
return false;
}
}

//添加数据(表名,行健,列族,列名,值)/如果存在则修改
public static boolean insertData(String tablename,String rowkey,String colFamily,String col,String val){
getConnection();
Table table;
try {
table = connection.getTable(TableName.valueOf(tablename));
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
table.put(put);
table.close();
close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
close();
return false;
}
}
//删除数据(表名,行健)
public static boolean deleteData(String tablename,String rowkey){
getConnection();
Table table;
try {
table = connection.getTable(TableName.valueOf(tablename));
Delete delete = new Delete(Bytes.toBytes(rowkey));
table.delete(delete);
table.close();
close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
close();
return false;
}
}
//浏览数据
public static void getData(String tablename,String rowkey,String colFamily,String col) throws IOException {
getConnection();
Table table = connection.getTable(TableName.valueOf(tablename));
Get get = new Get(Bytes.toBytes(rowkey));
get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
Result result = table.get(get);

System.out.println(new String(result.getValue(colFamily.getBytes(), col==null?null:col.getBytes())));

table.close();
close();
}
//扫描数据
public static Iterator<Result> scan(String tablename) {
getConnection();
try {
Table table = connection.getTable(TableName.valueOf(tablename));
Scan scan = new Scan();
// scan.setStartRow(Bytes.toBytes(startRow));
// scan.setStopRow(Bytes.toBytes(endrow));
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
return iterator;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}


//**********显示内容**********
//显示scan
public static Iterator<Result> showScan(Table table,Scan scan) {
ResultScanner scanner;
try {
scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
return iterator;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
//显示result
public static void showResult(Result result) {
CellScanner cellScanner = result.cellScanner();
try {
while(cellScanner.advance()) {
Cell cell = cellScanner.current();
String rowkey = new String(CellUtil.cloneRow(cell));
String colFamily = new String(CellUtil.cloneFamily(cell));
String qualifier = new String(CellUtil.cloneQualifier(cell));
String value = new String(CellUtil.cloneValue(cell));
System.out.println(rowkey+":"+colFamily+"."+qualifier+"="+value);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//迭代器result
public static void showIterResult(Iterator<Result> iterator) {
while(iterator.hasNext()) {
Result result = iterator.next();
showResult(result);
}
}

//**********过滤器**********

//创建单列值过滤器
public static SingleColumnValueFilter singleColumnValueFilter(String family,String qualifier,CompareFilter.CompareOp compareOp,String value,boolean isNull) {
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(qualifier), compareOp, Bytes.toBytes(value));
filter.setFilterIfMissing(isNull);
return filter;
}
//过滤器链
public static Iterator<Result> filterList(String tablename,String type,SingleColumnValueFilter[] lists) {
FilterList filterList = null;
if(type.equals("and")) {
filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
}else if(type.equals("or")){
filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
}
for(SingleColumnValueFilter filter:lists) {
filterList.addFilter(filter);
}
Scan scan = new Scan();
scan.setFilter(filterList);
getConnection();
try {
Table table = connection.getTable(TableName.valueOf(tablename));
return showScan(table, scan);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

// 负责为重大技术征集表添加信息。
public static void addBiao(String tablename,String username)throws IOException
{
// 不能为空
insertData(tablename, username, "bixu", "JGQC", "物理实验");
insertData(tablename, username, "bixu", "TXDZ", "石家庄铁道大学");
insertData(tablename, username, "bixu", "SZDY", "石家庄");
insertData(tablename, username, "bixu", "DZYX", "123@qq.com");
insertData(tablename, username, "bixu", "FRDB", "zxc");
insertData(tablename, username, "bixu", "YZBM", "031000");
insertData(tablename, username, "bixu", "LXR", "aas");
insertData(tablename, username, "bixu", "DH", "134651");
insertData(tablename, username, "bixu", "JGSX", "物理实验");
insertData(tablename, username, "bixu", "JGJJ", "1324");
insertData(tablename, username, "bixu", "JSXQMC", "aavv");
insertData(tablename, username, "bixu", "QSSX", "2018");
insertData(tablename, username, "bixu", "ZZSX", "2020");
insertData(tablename, username, "bixu", "JSXQGS", "aavv");
insertData(tablename, username, "bixu", "GJZ", "aavv");
insertData(tablename, username, "bixu", "JSXQJJFS", "aavv");
insertData(tablename, username, "bixu", "KJHDLX", "aavv");

// 可以为空
insertData(tablename, username, "feibixu", "GKGLBM", "aavv");
insertData(tablename, username, "feibixu", "WZ", "aavv");
insertData(tablename, username, "feibixu", "CZ", "aavv");
insertData(tablename, username, "feibixu", "GKGLBM", "aavv");
insertData(tablename, username, "feibixu", "NTRZJZE", "123");
insertData(tablename, username, "feibixu", "XKFL", "aavv");
insertData(tablename, username, "feibixu", "XQJSSSLY", "aavv");
insertData(tablename, username, "feibixu", "XQJSYYHY", "aavv");
}

/*
* 程序入口
*/
public static void main(String[] args) throws IOException {
String[] colFamily = new String[] {"bixu","feibixu"};
String tablename = "ZDXQZJB";
createTable(tablename,colFamily);



//添加数据
addBiao(tablename,"001");

getData(tablename, "001", "bixu", "TXDZ");
// getData(tablename, "002", "score", "english");
// getData(tablename, "001", "score", "english");

// getData(tablename, "001", "score", "english");

}
}

 

 

  

posted @ 2020-10-18 22:08  西西里啊  阅读(190)  评论(0编辑  收藏  举报