求助关于使用Go语言插入数据乱码的问题

Viewed 51

Version : doris-2.1.4-rc02
本地mac环境,使用docker启动的服务,下载的是 apache-doris-2.1.4-bin-arm64 版本

uname -a
Darwin local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:19:22 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T8112 arm64

FE Dockerfile

# 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.

# how to use Dockerfile.
# this is dockerfile for build doris fe image on amd64.
# when build youself image.
# 1. pull binary from official website and decompress into resource directory that the level equals with Dockerfile_be_ubuntu.
# 2. untar xxxx.tar.gz in resource directory, update the dockerfile field `apache-doris-xxx`, replace with real version.
# 3. run commad docker build -t xxx.doris.be:xx -f Dockerfile_be_ubuntu.

# we have support buildx for amd64 and arm64 architecture image build.
# get the binary from doris github and utar into resource, update the directory as apache-`version(example:2.0.1)`-bin-`architecture(amd64/arm64)` mode.

FROM ubuntu:22.04

ARG TARGETARCH

ARG DORIS_VERSION="2.1.4"

# set environment variables
ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-${TARGETARCH:-arm64} \
        PATH="/opt/apache-doris/fe/bin:${PATH}"

# apache-doris/fe from doris release tar.gz, please update the version in follows x.x.x.
COPY resource/apache-doris-2.1.4-bin-arm64/fe /opt/apache-doris/fe

RUN sed -i -e 's/^APT/# APT/' -e 's/^DPkg/# DPkg/' /etc/apt/apt.conf.d/docker-clean && swapoff -a && \
    apt-get update -y && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
    patchelf gdb binutils binutils-common mysql-client \
    curl wget less vim htop iproute2 numactl jq iotop sysstat tzdata \
    tcpdump iputils-ping dnsutils strace lsof blktrace \
    bpfcc-tools linux-headers-realtime linux-tools-realtime silversearcher-ag \
    net-tools openjdk-8-jdk && \
    rm -rf /var/lib/apt/lists/*

# RUN sysctl -w vm.max_map_count=2000000

RUN echo "vm.max_map_count = 2000000" >> /etc/sysctl.conf

ADD resource/init_fe.sh /usr/local/bin/

COPY resource/fe_*.sh /opt/apache-doris/

# when you use beijing zone, please enable the set.
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

WORKDIR /opt/apache-doris

ENTRYPOINT ["bash","init_fe.sh"]

BE Dockerfile

# 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.

# how to use Dockerfile.
# this is dockerfile for build doris fe image on amd64.
# when build youself image.
# 1. pull binary from official website and decompress into resource directory that the level equals with Dockerfile_be_ubuntu.
# 2. untar xxxx.tar.gz in resource directory, update the dockerfile field `apache-doris-xxx`, replace with real version.
# 3. run commad docker build -t xxx.doris.be:xx -f Dockerfile_be_ubuntu.

# we have support buildx for amd64 and arm64 architecture image build.
# get the binary from doris github and utar into resource, update the directory as apache-`version(example:2.0.1)`-bin-`architecture(amd64/arm64)` mode.

# choose a base image
FROM ubuntu:22.04

ARG TARGETARCH

ARG DORIS_VERSION="2.1.4"

RUN sed -i -e 's/^APT/# APT/' -e 's/^DPkg/# DPkg/' /etc/apt/apt.conf.d/docker-clean && swapoff -a && \
    apt-get update -y && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
    patchelf gdb binutils binutils-common mysql-client \
    curl wget less vim htop iproute2 numactl jq iotop sysstat tzdata \
    tcpdump iputils-ping dnsutils strace lsof blktrace \
    bpfcc-tools linux-headers-realtime linux-tools-realtime silversearcher-ag \
    net-tools openjdk-8-jdk && \
    rm -rf /var/lib/apt/lists/*

# RUN sysctl -w vm.max_map_count=2000000
RUN echo "vm.max_map_count = 2000000" >> /etc/sysctl.conf

# set environment variables
ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-${TARGETARCH:-arm64} \
        PATH="/opt/apache-doris/be/bin:${PATH}"

# apache-doris/be from doris release xxxx.tar.gz.please update the version in follows x.x.x.
ADD resource/apache-doris-2.1.4-bin-arm64/be /opt/apache-doris/be

COPY resource/be_*.sh /opt/apache-doris/

COPY resource/entry_point.sh /usr/local/bin/

COPY resource/init_be.sh /usr/local/bin/

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

WORKDIR /opt/apache-doris

ENTRYPOINT ["bash","entry_point.sh"]
SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';

image.png
image.png

建表sql:

CREATE TABLE users (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL
) ENGINE=OLAP
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 10
PROPERTIES (
    "replication_num" = "1",
    "in_memory" = "false"
);

实例代码:

package main

import (
	"fmt"
	"log"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

/*
CREATE TABLE users (
	`id` BIGINT NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL
) ENGINE=OLAP
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 10
PROPERTIES (
    "replication_num" = "1",
    "in_memory" = "false"
);
*/

type User struct {
	ID   uint   `gorm:"primaryKey"`
	Name string `gorm:"type:varchar(255);not null"`
}

func main() {
	// 数据库连接信息 charset=utf8mb4
	dsn := "username:password@tcp(127.0.0.1:9030)/dbname?charset=utf8mb4&parseTime=True&loc=Local"

	// 初始化 GORM DB
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatalf("failed to connect database: %v", err)
	}

	// 插入数据
	user := User{Name: "中文测试"}
	if err := db.Create(&user).Error; err != nil {
		log.Fatalf("failed to insert user: %v", err)
	}

	fmt.Println("Insert successful")

	// 查询所有数据
	var users []User
	if err := db.Find(&users).Error; err != nil {
		log.Fatalf("failed to query users: %v", err)
	}

	// 打印查询结果
	for _, u := range users {
		fmt.Printf("ID=%d, Name=%s\n", u.ID, u.Name)
	}
}

image.png

image.png

1 Answers

在云端服务x86的环境没有出现乱码的问题,在mac环境出现了乱码