Excel对包含文字的单元格计算求值

Excel对包含文字的单元格计算求值

在录入Excel数据的时候,我们有可能为了便于直接记录,会在单元格中出现这样的公式

  • 1+2+3
  • 1米+2米+3米
  • 。。。

这种公式的奇特点在于Excel不能直接计算得出结果,接下来我们怎么使用一个通用的函数来实现计算

解题思路:

  • 去除掉公式中非数字、加减乘除运算符号
  • 通过VBA中的Evaluate对最终的公式进行计算

终极函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Function CalculateWithText(ParamArray args() As Variant)
Dim objFormula As String

Dim result As String
Dim current As String
Dim eachRange As Range
For Each Rng In args
For Each eachRange In Rng
If result <> "" Then
result = result + "+"
End If
result = result + CStr(CalculateText(eachRange.Value))
'result = result + CalculateText(eachRange.Value)
Next
result = CStr(Evaluate(result))
Next
Debug.Print result
CalculateWithText = Evaluate(result)
End Function

Function CalculateText(objFormula As String)
Dim current As String
Dim validSymbol As String
validSymbol = "+,-,*,/,."
'Ìæ»»»»Ðзû¡¢¿Õ¸ñµÈ
objFormula = Replace(objFormula, vbNewLine, "+")
objFormula = Replace(objFormula, vbCr, "+")
objFormula = Replace(objFormula, vbLf, "+")
objFormula = Replace(objFormula, vbCrLf, "+")
objFormula = Replace(objFormula, " ", "+")
For n = 1 To Len(objFormula)
current = Mid(objFormula, n, 1)
If IsNumeric(current) Or IsInArray(current, Split(validSymbol, ",")) Then
result = result + current
End If
Next n
CalculateText = Evaluate(result)
End Function

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

支持功能:

  • 计算带文本公式
  • 支持多区间和单元格计算
  • 支持单元格中内容包含换行
  • 支持单元格中内容包含空格
  • 解决单元格过多导致计算#Value!错误

VBA获取网页中的表格数

  • 添加引用Microsof HTML Object Library
  • 通过浏览器内F12获取到表格对应的id或者class
  • 示例代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    Sub Test()
    GetTableFromWeb "https://tianqi.2345.com/wea_history/71141.htm", ".history-table", Range("A1")
    End Sub
    Function GetTableFromWeb(url As String, tableSelector As String, target As Range)
    Dim sResponse As String, html As HTMLDocument, clipboard As Object
    Set html = New HTMLDocument
    With CreateObject("MSXML2.XMLHTTP")
    .Open "GET", url, False
    .send
    sResponse = ByteToText(.responseBody, "utf-8")
    End With

    html.body.innerHTML = sResponse

    With html
    Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    clipboard.SetText .querySelector(tableSelector).outerHTML
    clipboard.PutInClipboard
    End With

    target.PasteSpecial
    End Function

    Function ByteToText(body, Cset)
    Dim objstream
    Set objstream = CreateObject("adodb.stream")
    objstream.Type = 1
    objstream.Mode = 3
    objstream.Open
    objstream.Write body
    objstream.Position = 0
    objstream.Type = 2
    objstream.Charset = Cset
    ByteToText = objstream.ReadText
    objstream.Close
    Set objstream = Nothing
    End Function

安装Maven

Maven是用于构建Java(和其他)程序的命令行工具。为方便起见,Maven 项目提供了一个简单的 ZIP 文件,其中包含 Maven 的预编译版本。 没有安装程序。 您可以设置先决条件和环境以在 Windows 上运行 Maven。

要求

Maven 是用 Java 编写的(主要用于构建 Java 程序)。 因此,主要的先决条件是 Java SDK。 您需要安装 Java SDK(例如从 Oracle 的下载站点)

一旦安装了 Java,您必须确保来自 Java SDK 的命令在您的 PATH 环境变量中。 运行,例如,

1
java -version

安装 Apache Maven

Apache Maven 的安装是一个简单的过程,即提取存档并将使用 mvn 命令的 bin 文件夹添加到 PATH

详细步骤如下:

  • 确保JAVA_HOME环境变量已设置并指向您的 JDK 安装

  • 下载Apache Maven, 并解压

1
unzip apache-maven-3.8.1-bin.zip

或者

1
tar xzvf apache-maven-3.8.1-bin.tar.gz

或者使用您首选的存档提取工具。

  • 将创建目录的目录添加apache-maven-3.8.1下的binPATH环境变量中

  • mvn -v在新的 shell 中确认。结果应该类似于

    1
    2
    3
    4
    5
    Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
    Maven home: C:\Users\xx\Meekou\Meekou.Install\apache-maven-3.8.1-bin\apache-maven-3.8.1\bin\..
    Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: C:\Program Files (x86)\Java\jre1.8.0_201
    Default locale: en_US, platform encoding: GBK
    OS name: "windows 10", version: "10.0", arch: "x86", family: "windows"

Windows安装

  • 检查环境变量值,例如

    1
    2
    echo %JAVA_HOME%
    C:\Program Files (x86)\Java\jre1.8.0_201
  • 添加到PATH:通过打开系统属性(WinKey + Pause),选择”高级”选项卡和”环境变量”按钮,然后添加或选择_PATH_变量,将解压的的 bin 目录添加到您的用户 PATH 环境变量中值为 的用户变量C:\Program Files\apache-maven-3.8.1\bin。相同的对话框可用于设置JAVA_HOMEJDK 的位置,例如C:\Program Files (x86)\Java\jre1.8.0_201

  • 打开一个新的命令提示符(Winkey + R 然后键入cmd)并运行mvn -v以验证安装。

基于 Unix 的操作系统(Linux、Solaris 和 Mac OS X)

  • 检查环境变量值
1
2
echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
  • 添加到 PATH
1
export PATH=/opt/apache-maven-3.8.1/bin:$PATH

引用

Flink Windows环境配置

Flink项目初始化

  • 通过mvn来构建项目,依次输入groupdid, artifactid
    1
    2
    3
    4
    $ mvn archetype:generate                               \
    -DarchetypeGroupId=org.apache.flink \
    -DarchetypeArtifactId=flink-quickstart-java \
    -DarchetypeVersion=1.9.0
  • 通过官方脚本快速构建
    1
    curl https://flink.apache.org/q/quickstart.sh | bash -s 1.9.0

项目结构

项目结构

  • 其中 BatchJob 为批处理的样例代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    package meekou;

    import org.apache.flink.api.java.ExecutionEnvironment;

    /**
    * Skeleton for a Flink Batch Job.
    *
    * <p>For a tutorial how to write a Flink batch application, check the
    * tutorials and examples on the <a href="http://flink.apache.org/docs/stable/">Flink Website</a>.
    *
    * <p>To package your application into a JAR file for execution,
    * change the main class in the POM.xml file to this class (simply search for 'mainClass')
    * and run 'mvn clean package' on the command line.
    */
    public class BatchJob {

    public static void main(String[] args) throws Exception {
    // set up the batch execution environment
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

    /*
    * Here, you can start creating your execution plan for Flink.
    *
    * Start with getting some data from the environment, like
    * env.readTextFile(textPath);
    *
    * then, transform the resulting DataSet<String> using operations
    * like
    * .filter()
    * .flatMap()
    * .join()
    * .coGroup()
    *
    * and many more.
    * Have a look at the programming guide for the Java API:
    *
    * http://flink.apache.org/docs/latest/apis/batch/index.html
    *
    * and the examples
    *
    * http://flink.apache.org/docs/latest/apis/batch/examples.html
    *
    */

    // execute program
    env.execute("Flink Batch Java API Skeleton");
    }
    }
  • 其中 StreamingJob 为流处理的样例代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    package meekou;

    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

    /**
    * Skeleton for a Flink Streaming Job.
    *
    * <p>For a tutorial how to write a Flink streaming application, check the
    * tutorials and examples on the <a href="http://flink.apache.org/docs/stable/">Flink Website</a>.
    *
    * <p>To package your application into a JAR file for execution, run
    * 'mvn clean package' on the command line.
    *
    * <p>If you change the name of the main class (with the public static void main(String[] args))
    * method, change the respective entry in the POM.xml file (simply search for 'mainClass').
    */
    public class StreamingJob {

    public static void main(String[] args) throws Exception {
    // set up the streaming execution environment
    final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

    /*
    * Here, you can start creating your execution plan for Flink.
    *
    * Start with getting some data from the environment, like
    * env.readTextFile(textPath);
    *
    * then, transform the resulting DataStream<String> using operations
    * like
    * .filter()
    * .flatMap()
    * .join()
    * .coGroup()
    *
    * and many more.
    * Have a look at the programming guide for the Java API:
    *
    * http://flink.apache.org/docs/latest/apis/streaming/index.html
    *
    */

    // execute program
    env.execute("Flink Streaming Java API Skeleton");
    }
    }

    getExecutionEnvironment 代表获取批处理或流处理的执行环境,如果是本地运行则获取到的就是本地的执行环境;如果在集群上运行,得到的就是集群的执行环境。

FAQ

  • Error “The goal you specified requires a project to execute but there is no POM in this directory” after executing maven command
    • 方案一 对于Windows平台,通过cmd.exe来执行,通过PowerShell来执行这些命令会导致错误
    • 方案二 对参数加上""
      1
      $ mvn archetype:generate "-DarchetypeGroupId=org.apache.flink" "-DarchetypeArtifactId=flink-quickstart-java" "-DarchetypeVersion=1.9.0"
  • Error “Cannot find a class with the main method”
    • 关闭vscodeeclipse
    • 删除根目录下的.project.classpath文件
    • 在根目录执行mvn eclipse:eclipse
    • 通过VS Code打开根目录
    • F5执行Debug项目
  • Error “var cannot be resolved to a type”
    • 方案一 Updatepom.xml
      1
      2
      3
      4
      5
      6
      7
      <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.0</version>
      <configuration>
      <release>11</release>
      </configuration>
      </plugin>
    • 方案二
    • 关闭vscodeeclipse
    • 删除根目录下的.project.classpath文件
    • 在根目录执行mvn eclipse:eclipse
    • 通过VS Code打开根目录
    • F5执行Debug项目

      引用

  • Flink
  • Flink official demo debug will get error :Cannot find a class with the main method #478
  • Error “The goal you specified requires a project to execute but there is no POM in this directory” after executing maven command
  • Flink 开发环境搭建

Flink之计算words

项目初始化

根据Flink Windows环境配置创建项目

项目实践

  • resources目录下创建wordcount.txt, 内容如下
    1
    2
    3
    4
    a,a,a,a,a
    b,b,b
    c,c
    d,d
  • BatchJob实现对wordcount.txt内字母技术汇总
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    package org.myorg.quickstart;
    import java.net.URL;
    import java.net.URLDecoder;

    import org.apache.flink.api.common.functions.FilterFunction;
    import org.apache.flink.api.common.functions.FlatMapFunction;
    import org.apache.flink.api.java.DataSet;
    import org.apache.flink.api.java.ExecutionEnvironment;
    import org.apache.flink.api.java.operators.DataSource;
    import org.apache.flink.api.java.tuple.Tuple2;
    import org.apache.flink.util.Collector;

    public class BatchJob {

    public static void main(String[] args) throws Exception {
    // set up the batch execution environment
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    URL url = BatchJob.class.getClassLoader().getResource("wordcount.txt");
    DataSource<String> dataset = env.readTextFile(URLDecoder.decode(url.getFile(),"UTF-8") );
    DataSet<Tuple2<String, Integer>> result = dataset.flatMap(new Tokenizer())
    .filter(new FilterFunction<Tuple2<String, Integer>>(){
    @Override
    public boolean filter(Tuple2<String, Integer> arg0){
    return arg0.f1 >0;
    }
    })
    .groupBy(0)
    .sum(1);
    result.print();
    }
    public static class Tokenizer implements FlatMapFunction<String, Tuple2<String, Integer>>{
    @Override
    public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
    String[] tokens = value.toLowerCase().split(",");
    for (String token : tokens) {
    if ( !token.isEmpty() && token.length() > 0) {
    out.collect(new Tuple2<String, Integer>(token, 1));
    }
    }
    }
    }
    }
  • 测试结果
    测试结果

HDFS常用shell命

HDFS 常用 shell 命令

1. 显示当前目录结构

1
2
3
4
5
6
# 显示当前目录结构
hadoop fs -ls <path>
# 递归显示当前目录结构
hadoop fs -ls -R <path>
# 显示根目录下内容
hadoop fs -ls /

2. 创建目录

1
2
3
4
# 创建目录
hadoop fs -mkdir <path>
# 递归创建目录
hadoop fs -mkdir -p <path>

3. 删除操作

1
2
3
4
# 删除文件
hadoop fs -rm <path>
# 递归删除目录和文件
hadoop fs -rm -R <path>

4. 从本地加载文件到 HDFS

1
2
3
# 二选一执行即可
hadoop fs -put [localsrc] [dst]
hadoop fs - copyFromLocal [localsrc] [dst]

5. 从 HDFS 导出文件到本地

1
2
3
# 二选一执行即可
hadoop fs -get [dst] [localsrc]
hadoop fs -copyToLocal [dst] [localsrc]

6. 查看文件内容

1
2
3
# 二选一执行即可
hadoop fs -text <path>
hadoop fs -cat <path>

7. 显示文件的最后一千字节

1
2
3
hadoop fs -tail  <path> 
# 和Linux下一样,会持续监听文件内容变化 并显示文件的最后一千字节
hadoop fs -tail -f <path>

8. 拷贝文件

1
hadoop fs -cp [src] [dst]

9. 移动文件

1
hadoop fs -mv [src] [dst] 

10. 统计当前目录下各文件大小

  • 默认单位字节
  • -s : 显示所有文件大小总和,
  • -h : 将以更友好的方式显示文件大小(例如 64.0m 而不是 67108864)
    1
    hadoop fs -du  <path>  

11. 合并下载多个文件

  • -nl 在每个文件的末尾添加换行符(LF)
  • -skip-empty-file 跳过空文件
1
2
3
hadoop fs -getmerge
# 示例 将HDFS上的hbase-policy.xml和hbase-site.xml文件合并后下载到本地的/usr/test.xml
hadoop fs -getmerge -nl /test/hbase-policy.xml /test/hbase-site.xml /usr/test.xml

12. 统计文件系统的可用空间信息

1
hadoop fs -df -h /

13. 更改文件复制因子

1
hadoop fs -setrep [-R] [-w] <numReplicas> <path>
  • 更改文件的复制因子。如果 path 是目录,则更改其下所有文件的复制因子
  • -w : 请求命令是否等待复制完成
1
2
# 示例
hadoop fs -setrep -w 3 /user/hadoop/dir1

14. 权限控制

1
2
3
4
5
6
7
# 权限控制和Linux上使用方式一致
# 变更文件或目录的所属群组。 用户必须是文件的所有者或超级用户。
hadoop fs -chgrp [-R] GROUP URI [URI ...]
# 修改文件或目录的访问权限 用户必须是文件的所有者或超级用户。
hadoop fs -chmod [-R] <MODE[,MODE]... | OCTALMODE> URI [URI ...]
# 修改文件的拥有者 用户必须是超级用户。
hadoop fs -chown [-R] [OWNER][:[GROUP]] URI [URI ]

15. 文件检测

1
hadoop fs -test - [defsz]  URI

可选选项:

  • -d:如果路径是目录,返回 0。
  • -e:如果路径存在,则返回 0。
  • -f:如果路径是文件,则返回 0。
  • -s:如果路径不为空,则返回 0。
  • -r:如果路径存在且授予读权限,则返回 0。
  • -w:如果路径存在且授予写入权限,则返回 0。
  • -z:如果文件长度为零,则返回 0。
1
2
# 示例
hadoop fs -test -e filename

引用

通过js读取Excel文件

关于通过JavaScript读取Excel中的数据,可以通过

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html>
<header>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="https://unpkg.com/read-excel-file@4.x/bundle/read-excel-file.min.js"></script>
<script>
$( document ).ready(function() {
var input = document.getElementById('input')
if(input) {
input.addEventListener('change', function() {
readXlsxFile(input.files[0]).then(rows => {
console.log(rows);
})
})
}
});
</script>
</header>

<body>
<input type="file" id="input" />
<div id='result'></div>
</body>
</html>

将Word文件分割成小Word文件

关于将Word文件切分为多个小的Word文件。

我们可以通过以下的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Sub SplitWordFile()
Dim pageCount As Integer
Dim splitWordNum As Integer
Dim rngPage As Range
Dim splitDoc As Document
Dim targetFolder As String
pageCount = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
splitWordNum = pageCount / 3
currentPage = 1
targetFolder = "C:\Users\jzhout1\Downloads\CopyTest\Result\"
Set rngPage = ActiveDocument.Range
Application.ScreenUpdating = False
For Index = 1 To 3
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=currentPage
rngPage.Start = Selection.Range.Start
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=Index * splitWordNum
Selection.MoveEnd
rngPage.End = Selection.Range.End
rngPage.Copy
Set splitDoc = Documents.Add
splitDoc.Select
Selection.WholeStory
Selection.PasteAndFormat (wdFormatOriginalFormatting)
newDocName = targetFolder & Right$("000" & Index, 4) & ".docx"
splitDoc.SaveAs newDocName
splitDoc.Close
currentPage = Index * splitWordNum + 1
Next Index

Application.ScreenUpdating = True
End Sub

AWS EC2磁盘扩容

AWS EC2默认创建的时候磁盘比较小,当磁盘不够的时候,我们需要对EC2的硬盘进行扩容。

AWS EC2 控制面板修改磁盘大小

  • AWS Console进行Elastic Block Store的卷中
  • 选择需要扩容的磁盘,点击操作按钮
  • 点击修改
  • 卷类型选择通用型SSD(gp2)(可以根据自己的需求选择其他类型)
  • 大小输入16(按需输入)
  • 点击确认修改

修改卷

调整卷大小后扩展 Linux 文件系统

  • 使用df -h命令验证挂载在”/“下的根分区是否已满(100%)
    磁盘
  • 运行以下命令lsblk以收集有关附加块设备和根”/“挂载点的详细信息。
    挂载
  • 为避免块设备上没有剩余空间错误,请将临时文件系统tmpfs挂载到/tmp挂载点。这会创建一个挂载到/tmp的 10 M tmpfs
    1
    sudo mount -o size=10M,rw,nodev,nosuid -t tmpfs tmpfs /tmp
    挂载tmpfs
  • 运行growpart命令以增大根分区或分区1 的大小。将/dev/nvme0n1替换为你的根分区
    1
    sudo growpart /dev/nvme0n1 1
  • 运行lsblk命令以验证分区 1 是否已扩展到 16 GiB
    扩展磁盘
  • 扩展文件系统。执行lsblk -f确认磁盘类型
    磁盘类型
  • 如果想要扩展XFS类型的文件系统,执行下面的命令
    1
    sudo xfs_growfs -d /
    扩展XFS
  • 如果想要扩展EXT2/EXT3/EXT4文件系统,执行
    1
    sudo resize2fs /dev/nvme0n1p1
  • 扩展文件系统后,使用df -h命令验证操作系统是否可以看到额外的空间
    结果
  • 使用unmount命令卸载tmpfs文件系统
    1
    $ sudo umount /tmp

FAQ

  • FAILED: failed to dump sfdisk info for /dev/nvme0n1
    为避免块设备上没有剩余空间错误,请将临时文件系统tmpfs挂载到/tmp挂载点。这会创建一个挂载到/tmp的 10 M tmpfs
    1
    sudo mount -o size=10M,rw,nodev,nosuid -t tmpfs tmpfs /tmp
  • FAILED: failed: sfdisk –list /dev/nvme0n1
    为避免块设备上没有剩余空间错误,请将临时文件系统tmpfs挂载到/tmp挂载点。这会创建一个挂载到/tmp的 10 M tmpfs
    1
    sudo mount -o size=10M,rw,nodev,nosuid -t tmpfs tmpfs /tmp

    引用

  • How do I increase the size of my EBS volume if I receive an error that there’s no space left on my file system?

Hive安装

访问Hive获取下载安装地址

安装

  • 创建downloads文件夹并下载Hive
1
2
hadoop@M5303398:~$ mkdir downloads
hadoop@M5303398:~/downloads$ wget https://downloads.apache.org/hive/hive-3.1.2/apache-hive-3.1.2-bin.tar.gz
  • 解压apache-hive-3.1.2-bin.tar.gzhive目录
1
hadoop@M5303398:~/downloads$ tar xzf apache-hive-3.1.2-bin.tar.gz -C ~/hive
  • 配置Hive的环境变量(bashrc)
1
hadoop@M5303398:~$ sudo nano .bashrc
  • 修改环境变量
1
2
export HIVE_HOME=~/hive/apache-hive-3.1.2-bin
export PATH=$PATH:$HIVE_HOME/bin
  • 刷新.hashrc
1
hadoop@M5303398:~$ source ~/.bashrc
  • 修改hive-config.sh
1
sudo nano $HIVE_HOME/bin/hive-config.sh
  • 添加HADOOP_HOME
1
export HADOOP_HOME=/home/hdoop/hadoop-3.2.1

在HDFS中创建配置单元目录

  • 创建两个单独的目录以将数据存储在HDFS层中:
    • 临时的tmp目录将存储Hive进程的中间结果
    • warehouse仓库目录将存储与Hive相关的表。
  • 创建tmp目录,该目录将存储Hive发送到HDFS的中间数据
1
hdfs dfs -mkdir /tmp
  • 为tmp组成员添加写和执行权限
1
hdfs dfs -chmod g+w /tmp
  • 检查权限是否正确添加
1
hdfs dfs -ls /
  • 创建仓库目录warehouse
  • /user/hive/父目录中创建仓库目录
1
hdfs dfs -mkdir -p /hadoop/hive/warehouse
  • 为仓库组成员添加写入和执行权限
1
hdfs dfs -chmod g+w /hadoop/hive/warehouse
  • 检查权限是否添加正确
1
hdfs dfs -ls /hadoop/hive

配置 hive-site.xml 文件

  • 默认情况下,Apache Hive发行版包含模板配置文件。模板文件位于Hive conf目录中,并概述了默认的Hive设置。 使用以下命令找到正确的文件
1
cd $HIVE_HOME/conf
  • 使用hive-default.xml.template创建hive-site.xml文件
1
cp hive-default.xml.template hive-site.xml
  • 使用nano文本编辑器访问hive-site.xml文件
1
sudo nano hive-site.xml

FAQ

  • HiveSchemaTool:Parsing failed. Reason: Missing required option

    • 将命令替换为(修改-dbType 和 -initSchema的顺序)
    1
    $HIVE_HOME/bin/schematool -dbType derby -initSchema
  • Exception in thread “main” java.lang.NoSuchMethodError:

    • 该错误表明Hadoop和Hive版本之间很可能存在不兼容性问题
    • 在Hive lib目录中找到guava jar文件
    1
    ls $HIVE_HOME/lib
    • 在Hadoop lib目录中也找到guava jar文件
    1
    ls $HADOOP_HOME/share/hadoop/hdfs/lib
    • 列出的两个版本不兼容,并导致该错误。从Hive lib目录中删除现有的guava文件
    1
    rm $HIVE_HOME/lib/guava-19.0.jar
    • 将guava文件从Hadoop lib目录复制到Hive lib目录
    1
    cp $HADOOP_HOME/share/hadoop/hdfs/lib/guava-27.0-jre.jar $HIVE_HOME/lib/
    • 再次使用 schematool 命令启动 Derby 数据库
    1
    $HIVE_HOME/bin/schematool -dbType derby -initSchema
  • Hive throws: WstxParsingException: Illegal character entity: expansion character (code 0x8)

    • 之所以引发该错误,是因为单词fortransactional之间有一个特殊字符()。删除该字符或复制并粘贴(替换)介于<description></description>之间的字符。
    • 修改类型for&#8;transactionalfor transactional
  • Exception in thread “main” java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader

    • 确保.bashrc中修改的内容是正确的
    1
    2
    3
    #Hive Related Options
    export HIVE_HOME=~/hive/apache-hive-3.1.2-bin
    export PATH=$PATH:$HIVE_HOME/bin:$HIVE_HOME/bin
    • Hive目前不支持Java 9+,如果Java版本高于9+,需要安装并配置JAVA8

      • 根据本地包
      1
      sudo apt-get update
      • 安装JAVA 8
      1
      sudo apt-get install openjdk-8-jdk
      • 查看JAVA 8的版本
      1
      hadoop@M5303398:~$ cd /usr/lib/jvm
      • 修改hadoop-env.sh
      1
      hadoop@M5303398:~$ vi $HADOOP_HOME/etc/hadoop/hadoop-env.sh
      • 配置JAVA_HOME
      1
      JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
    • 输入hive查看

  • Exception in thread “main” java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:java.io.tmpdir%7D/$%7Bsystem:user.name%7D

    • 这是因为hive-site.xml缺少system:java.io.tmpdirsystem:user.name
    • 将下面的内容添加到hive-site.xml开头
    1
    2
    3
    4
    5
    6
    7
    8
    <property>
    <name>system:java.io.tmpdir</name>
    <value>/tmp/hive/java</value>
    </property>
    <property>
    <name>system:user.name</name>
    <value>${user.name}</value>
    </property>

引用