今天上了软件构造,对于java的概念有加深了解了一些,对于打包生成exe文件有了了解,对于软件设计,进行了创建型模式的学习,同时对于zookeeper帮别人配置了好几遍,对于hbase,学习了idea进行连接集群,创建表,进行操作,也进行idea的连接配置:
新建连接
导入maven配置
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.4.11</version>
<exclusions>
<exclusion>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.1-b06</version>
</dependency>
</dependencies>
创建HbaseConnection
编写
public static void main(String[] args) throws IOException {
//1、创建连接配置对象
Configuration configuration = new Configuration();
//2、添加配置参数
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
//3、创建连接(默认同步连接)
Connection connection = ConnectionFactory.createConnection(configuration);
//异步连接
// CompletableFuture<AsyncConnection> asyncConnection = ConnectionFactory.createAsyncConnection(configuration);
//4、使用连接
System.out.println(connection);
//关闭连接
connection.close();
}
多线程进行连接:(推荐)
本地配置文件
<?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>
<!--
The following properties are set for running HBase as a single process on a
developer workstation. With this configuration, HBase is running in
"stand-alone" mode and without a distributed file system. In this mode, and
without further configuration, HBase and ZooKeeper data are stored on the
local filesystem, in a path under the value configured for `hbase.tmp.dir`.
This value is overridden from its default value of `/tmp` because many
systems clean `/tmp` on a regular basis. Instead, it points to a path within
this HBase installation directory.
Running against the `LocalFileSystem`, as opposed to a distributed
filesystem, runs the risk of data integrity issues and data loss. Normally
HBase will refuse to run in such an environment. Setting
`hbase.unsafe.stream.capability.enforce` to `false` overrides this behavior,
permitting operation. This configuration is for the developer workstation
only and __should not be used in production!__
See also https://hbase.apache.org/book.html#standalone_dist
-->
<property>
<name>hbase.zookeeper.quorum</name>
<value>hadoop102,hadoop103,hadoop104</value>
<description>The directory shared by RegionServers.
</description>
</property>
</configuration>
public static Connection connection = null;
static {
//1、创建连接(同步连接)
try {
//读取本地文件形式添加参数
connection = ConnectionFactory.createConnection();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void closeConnection() throws IOException {
//判断连接是否为空
if(connection != null)
{
connection.close();
}
}
public static void main(String[] args) throws IOException {
/*
//1、创建连接配置对象
Configuration configuration = new Configuration();
//2、添加配置参数
configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
//3、创建连接(默认同步连接)
Connection connection = ConnectionFactory.createConnection(configuration);
//异步连接
// CompletableFuture<AsyncConnection> asyncConnection = ConnectionFactory.createAsyncConnection(configuration);
//4、使用连接
System.out.println(connection);
//关闭连接
connection.close();
*/
// 直接使用创建好的连接,不在main线程里单独创建
System.out.println(HbaseConnection.connection);
//在main线程里进行关闭连接
HbaseConnection.closeConnection();
}
用idea进行DDL
创建HbaseDDL.class
import org.apache.hadoop.hbase.NamespaceDescriptor;
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 {
//声明一个静态属性获取到连接
public static Connection connection = HbaseConnection.connection;
/**
* 创建命名空间
* @param namespace 命名空间名称
*/
public static void createNamespace(String namespace) throws IOException {
//1、获取admin
// 此处异常先不抛出,等待方法写完,在进行统一处理
// admin连接轻量级的,不是线程安全的,不推荐池化或者缓存这个连接
Admin admin = connection.getAdmin();
//2、调用方法创建命名空间
//代码相对shell更加底层,所以shell可以实现的,代码一定可以实现
//2.1创建命名空间描述建造者 =>设计师
NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(namespace);
//2.2 给命名空间添加需求
builder.addConfiguration("user","atguigu");
//2.3使用builder构造出对应完整的对象,完成创建
//创建命名空间的问题,都属于本方法的问题,不应该抛出
try {
admin.createNamespace(builder.build());
} catch (IOException e) {
e.printStackTrace();
}
//3、关闭创建
admin.close();
}
/**
* 判断表格是否存在
* @param namespace 命名空间名称
* @param tableName 表格名称
* @return true 表示存在
*/
public static boolean isTableExists(String namespace,String tableName) throws IOException {
// 1、获取admin
Admin admin = connection.getAdmin();
//2、使用方法判断表格是否存在
boolean b = false;
try {
b = admin.tableExists(TableName.valueOf(namespace, tableName));
} catch (IOException e) {
e.printStackTrace();
}
//3、关闭连接
admin.close();
//返回结果
return b;
}
/**
* 创建表格
* @param namespace 命名空间名称
* @param tableName 表格名称
* @param columnFamilies 列族名称 可以有多个
*/
public static void createTable(String namespace,String tableName,String... columnFamilies) throws IOException {
//1、获取admin
Admin admin = connection.getAdmin();
//2、调用方法创建表格
//2.1 创建表格描述的建造者
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(namespace, tableName));
//2.2 添加参数
for (String columnFamily : columnFamilies) {
//2.3 创建列族描述
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder =
ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily));
//2.4 对应当前的列族添加参数
//添加版本参数
columnFamilyDescriptorBuilder.setMaxVersions(5);
//2.5 创建添加完参数的列族描述
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
}
// 创建对应表格描述
try {
admin.createTable(tableDescriptorBuilder.build());
} catch (IOException e) {
e.printStackTrace();
}
//关闭admin
admin.close();
}
public static void main(String[] args) throws IOException {
//测试创建命名空间
//应该先保证连接没有问题,再来调用相关方法
// createNamespace("atguigu");
//测试判断表格是否存在
System.out.println(isTableExists("bigdata", "student"));
//测试创建表格
createTable("atguigu","student","info","msg");
//其他代码
System.out.println("其他代码");
//关闭Hbase连接
HbaseConnection.closeConnection();
}
}
浙公网安备 33010602011771号