答:
As far as your sample data, it seems like you want to join mst_program on column uid_urusan rather than uid_program.

If so, consider:

```<code>SELECT
  u.`urusan`,
  p.`program`,
  k.`kegiatan`
FROM
  `mst_kegiatan` k
  LEFT OUTER JOIN `mst_urusan` u  ON k.`uid_urusan` = u.`uid_urusan`
  LEFT OUTER JOIN `mst_program` p ON k.`uid_urusan` = p.`uid_urusan`
</code>```

Note that I modified your query to use table aliases: this makes the query easier to read and write.

Title: Getting null when join 3 tables in mysql

Body:
so, here is my tables, i'd like to join 3 tables using JOIN query, but always getting null on some column,

mst_urusan
+------------+------------------------+
| uid_urusan | urusan                 |
+------------+------------------------+
|          1 | PANGAN                 |
|          2 | KELAUTAN DAN PERIKANAN |
|          3 | PERTANIAN              |
+------------+------------------------+

mst_program
+-------------+------------------------------------------------------------------------------+------------+
| uid_program | program                                                                      | uid_urusan |
+-------------+------------------------------------------------------------------------------+------------+
|          11 | Program Tata Kelola Pemerintahan                                             |          1 |
|          12 | Program Peningkatan Ketersediaan, Distribusi dan Penanganan Kerawanan Pangan |          1 |
|          13 | Program Peningkatan Keanekaragaman Konsumsi dan Keamanan Pangan              |          1 |
|          14 | Program Produktivitas Perikanan                                              |          2 |
|          15 | Program Produktivitas Pertanian dan Peternakan                               |          3 |
+-------------+------------------------------------------------------------------------------+------------+

mst_kegiatan
+--------------+-----------------------------------------------------------------------------+------------+-------------+
| uid_kegiatan | kegiatan                                                                    | uid_urusan | uid_program |
+--------------+-----------------------------------------------------------------------------+------------+-------------+
|            1 | Pelayanan Administrasi Perkantoran                                          |          1 |           1 |
|            2 | Peningkatan Sarana dan Prasarana Aparatur                                   |          1 |           1 |
|            3 | Peningkatan Kapasitas dan Pengelolaan Sumber Daya Aparatur Perangkat Daerah |          1 |           1 |
|            4 | Peningkatan Ketersediaan Pangan                                             |          1 |           2 |
|            5 | Koordinasi, Pemantauan dan Pemantapan Distribusi Pangan Daerah              |          1 |           2 |
|            6 | Penanganan Kerawanan Pangan                                                 |          1 |           3 |
|            7 | Diversifikasi Pengembangan Pangan                                           |          1 |           3 |
|            8 | Pengawasan Keamanan Pangan                                                  |          1 |           3 |
|            9 | Pembinaan dan Pengembangan Perikanan                                        |          2 |           4 |
|           10 | Produktivitas Pertanian Perkotaan                                           |          3 |           5 |
|           11 | Pembinaan Peternakan dan Kesehatan Hewan                                    |          3 |           5 |
|           12 | Pengembangan Pertanian dan Perikanan Terpadu                                |          3 |           5 |
+--------------+-----------------------------------------------------------------------------+------------+-------------+

i try using JOIN but i've got some null on column,

SELECT
  `mst_urusan`.`urusan`,
  `mst_program`.`program`,
  `mst_kegiatan`.`kegiatan`
FROM
  `mst_kegiatan`
  LEFT OUTER JOIN `mst_urusan` ON (`mst_kegiatan`.`uid_urusan` = `mst_urusan`.`uid_urusan`)
  LEFT OUTER JOIN `mst_program` ON (`mst_kegiatan`.`uid_program` = `mst_program`.`uid_program`)

the result is like this,

+------------------------+---------+-----------------------------------------------------------------------------+
| urusan                 | program | kegiatan                                                                    |
+------------------------+---------+-----------------------------------------------------------------------------+
| PANGAN                 | NULL    | Pelayanan Administrasi Perkantoran                                          |
| PANGAN                 | NULL    | Peningkatan Sarana dan Prasarana Aparatur                                   |
| PANGAN                 | NULL    | Peningkatan Kapasitas dan Pengelolaan Sumber Daya Aparatur Perangkat Daerah |
| PANGAN                 | NULL    | Peningkatan Ketersediaan Pangan                                             |
| PANGAN                 | NULL    | Koordinasi, Pemantauan dan Pemantapan Distribusi Pangan Daerah              |
| PANGAN                 | NULL    | Penanganan Kerawanan Pangan                                                 |
| PANGAN                 | NULL    | Diversifikasi Pengembangan Pangan                                           |
| PANGAN                 | NULL    | Pengawasan Keamanan Pangan                                                  |
| KELAUTAN DAN PERIKANAN | NULL    | Pembinaan dan Pengembangan Perikanan                                        |
| PERTANIAN              | NULL    | Produktivitas Pertanian Perkotaan                                           |
| PERTANIAN              | NULL    | Pembinaan Peternakan dan Kesehatan Hewan                                    |
| PERTANIAN              | NULL    | Pengembangan Pertanian dan Perikanan Terpadu                                |
+------------------------+---------+-----------------------------------------------------------------------------+

i'd like the result like picture bellow, any suggestion ?
 https://i.stack.imgur.com/jO1Tc.png

Body:
As far as your sample data, it seems like you want to join mst_program on column uid_urusan rather than uid_program.

If so, consider:

SELECT
  u.`urusan`,
  p.`program`,
  k.`kegiatan`
FROM
  `mst_kegiatan` k
  LEFT OUTER JOIN `mst_urusan` u  ON k.`uid_urusan` = u.`uid_urusan`
  LEFT OUTER JOIN `mst_program` p ON k.`uid_urusan` = p.`uid_urusan`

Note that I modified your query to use table aliases: this makes the query easier to read and write.


来源: stackexchange

问:
Which is more better between basic auth and token auth as security perspective
I am currently developing a RESTful API server, and I am choosing between using ID and password or using a token to authenticate a user.

Let me, explain my situation first. I need to include static authentication information to my library to communicate between a client and my server or provide it to a partnership company to communicate between their server and my server. And when I was researching other services which are in a similar situation as us, they are using token now (for example, Bugfender is using a token to specify a user).

However, what I think is that using ID and PW and using the token are the same or using ID and PW is better because there are two factors to compare it is correct or incorrect.

Is there any reason why other services are using a token?

Which one is better as a security perspective or is there a better way to do this?

答:
In case the credentials (ID and password) and the token are being transferred the same way (say: by a header in a REST request) over a TLS secured channel, the only difference lies in the entropy of the password VS entropy of the token. Since it is something for you to decide in both cases, there is no real difference from the security perspective.

NOTE: I don't count the ID as a secret, as it usually is something far easier to guess than a secret should be.

I'd go for a solution that is easier to implement and manage.

IMHO this would be HTTP basic authentication, as you usually get full support from your framework/web server with little danger of making security mistakes in authentication logic. You know, friends don't let friends write their own auth. ;)

ContentLicense: CC BY-SA 4.0


问:
Swift how to add compiler warning to function
I wrote a simple extension to a viewController and I would like to put a warning so that other developers don't call this function directly. I am not sure if there is an attribute that can serve this purpose in swift :

My code :

```<code>
internal extension UIViewController {

    // add warning so that it can appear when another developer wants to directly access this function
    func removeFromParent() {

        self.willMove(toParent: nil)
        self.view.removeFromSuperview()
        self.removeFromParent()
    }
}

</code>```

So when another develop tries to do something like :

```<code>let viewController = TestViewController()
viewController.removeFromParent()
</code>```

They should get warning that you can't use this function directly

答:
add above your function:

```<code>@available(*, deprecated, message: "use `someOtherFunction` instead")
</code>```

You could also add a commentary like this:

```<code>/// DEPRECATED: you should use `someOtherFunction` instead
</code>```

So it appears when your developer use Alt + Click

Body:
I wrote a simple extension to a viewController and I would like to put a warning so that other developers don't call this function directly. I am not sure if there is an attribute that can serve this purpose in swift :

My code :

internal extension UIViewController {

    // add warning so that it can appear when another developer wants to directly access this function
    func removeFromParent() {

        self.willMove(toParent: nil)
        self.view.removeFromSuperview()
        self.removeFromParent()
    }
}

So when another develop tries to do something like :

let viewController = TestViewController()
viewController.removeFromParent()

They should get warning that you can't use this function directly

Body:
add above your function:

@available(*, deprecated, message: "use `someOtherFunction` instead")

You could also add a commentary like this:

/// DEPRECATED: you should use `someOtherFunction` instead

So it appears when your developer use Alt + Click


问:
ng-smart-table accessing current row in custom action
I want to customize custom action icon, something like this (pseudo-code):

```<code>    settings = {
        actions: {
            custom: [
                {
                    name: 'DOWNLOAD_PDF',
                    title: `
if this.row.status === 'one'
    <i class="nb-compose" title="Download PDF"></i>
else
    <i class="nb-compose" title="PDF is not downloadable yet"></i>
endif;
`
                },
            ],
        },
        ...
    }
</code>```

Seems that ng2-smart-table only allows to output icon and no customization? Or is there any workaround?

ng2-smart-table version is 1.4.0.

答:
Seems that it finally boils down to 2 options:

Defining column type=custom with renderComponent. No custom action then

Rendering 2 icons, using rowClassFunction to set row classes and hide/show them appropriately

Body:
I want to customize custom action icon, something like this (pseudo-code):

    settings = {
        actions: {
            custom: [
                {
                    name: 'DOWNLOAD_PDF',
                    title: `
if this.row.status === 'one'

else

endif;
`
                },
            ],
        },
        ...
    }

Seems that ng2-smart-table only allows to output icon and no customization? Or is there any workaround?

ng2-smart-table version is 1.4.0.


问:
Is there third party package repository for Swift?
I came from web development/design (still are) and I started to learn Swift 5 for iOS app development. In web development, there's a lot of sites or repo where I can download pre-written codes for the web like packagist.org. My question is, is there a similar site where I can pull pre-written code in Swift so I can extend the functionality of an iOS app? Thank you!</p>

答: You can try dependency managers CocoaPods, Carthage, Swift Package Manager.</p>


问:
Regex in sqlite
i have been thinking about this a lot.

So i wanna create a table which contains a password.
The password should at least be 6 chars long a contain minimum 2 numbers.

My version was:

```<code>create table User (
  passwort varchar(80) not null check (length(passwort) >= 6 and passwort like '%[0-9]%[0-9]%')
);
</code>```

The Problem with this approach is that the password has to contain [0-9] twice instead of the actual numbers. Does anyone know how to get rid of that problem ?

Thanks in advance.

答:
How about .*?\d.*?\d.*?

This ensures that between zero or more characters (including digits), there must be 2 digits.

Title: Regex in sqlite

Body:
i have been thinking about this a lot.

So i wanna create a table which contains a password.
The password should at least be 6 chars long a contain minimum 2 numbers.

My version was:

create table User (
  passwort varchar(80) not null check (length(passwort) >= 6 and passwort like '%[0-9]%[0-9]%')
);

The Problem with this approach is that the password has to contain [0-9] twice instead of the actual numbers. Does anyone know how to get rid of that problem ?

Thanks in advance.


问:
Play Json Reads nested generic serialized Json
Consider the following JSON

```<code>{
    "a": "{\"b\": 12, \"c\": \"test\"}"
}
</code>```

I would like to define a generic reads Reads[Outer[T]] for this kind of serialized Json

```<code>import play.api.libs.json.{Json, Reads, __}

final case class Outer[T](inner: T)
final case class SpecializedInner(b: Int, c: String)

object SpecializedInner {
   implicit val reads: Reads[SpecializedInner] = Json.reads[SpecializedInner]
}

object Outer {
   implicit def reads[T](implicit readsT: Reads[T]): Reads[Outer[T]] = ???
}
</code>```

How can I achieve my goal? I tried to flatMap a string-reads for the field "outer.a" but got stuck since I am not able to produce a Reads[T] from the validated JSON

```<code>object Outer {
  implicit def reads[T](implicit readsT: Reads[T]): Reads[Outer[T]] =
    (__ \ "a").read[String].flatMap(x => Json.parse(x).validate[T])
}
</code>```

答:
You need just add map inside Outer.reads construction after validate[T] invocation.

Please, see next code for example:

```<code>object App {

  final case class Outer[T](inner: T)

  object Outer {
    implicit def reads[T](implicit innerReads: Reads[T]): Reads[Outer[T]] = { json: JsValue =>
      json.validate[String].flatMap(string => Json.parse(string).validate[T].map(Outer.apply[T]))
    }
  }

  final case class Root[T](a: Outer[T])
  object Root {
    implicit def reads[T](implicit innerReads: Reads[T]): Reads[Root[T]] = Json.reads
  }

  final case class SpecializedInner(b: Int, c: String)

  object SpecializedInner {
    implicit val reads: Reads[SpecializedInner] = Json.reads
  }

  def main(args: Array[String]): Unit = {
    val rawJson  = "{\"a\": \"{\\\"b\\\": 12, \\\"c\\\": \\\"test\\\"}\"}"
    println(Json.parse(rawJson).validate[Root[SpecializedInner]])
  }
}
</code>```

Which produced next result in my case:

```<code>JsSuccess(Root(Outer(SpecializedInner(12,test))),)
</code>```

Hope this helps!

Body:
Consider the following JSON

{
    "a": "{\"b\": 12, \"c\": \"test\"}"
}

I would like to define a generic reads Reads[Outer[T]] for this kind of serialized Json

import play.api.libs.json.{Json, Reads, __}

final case class Outer[T](inner: T)
final case class SpecializedInner(b: Int, c: String)

object SpecializedInner {
   implicit val reads: Reads[SpecializedInner] = Json.reads[SpecializedInner]
}

object Outer {
   implicit def reads[T](implicit readsT: Reads[T]): Reads[Outer[T]] = ???
}

How can I achieve my goal? I tried to flatMap a string-reads for the field "outer.a" but got stuck since I am not able to produce a Reads[T] from the validated JSON

object Outer {
  implicit def reads[T](implicit readsT: Reads[T]): Reads[Outer[T]] =
    (__ \ "a").read[String].flatMap(x => Json.parse(x).validate[T])
}

Body:
You need just add map inside Outer.reads construction after validate[T] invocation.

Please, see next code for example:

object App {

  final case class Outer[T](inner: T)

  object Outer {
    implicit def reads[T](implicit innerReads: Reads[T]): Reads[Outer[T]] = { json: JsValue =>
      json.validate[String].flatMap(string => Json.parse(string).validate[T].map(Outer.apply[T]))
    }
  }

  final case class Root[T](a: Outer[T])
  object Root {
    implicit def reads[T](implicit innerReads: Reads[T]): Reads[Root[T]] = Json.reads
  }

  final case class SpecializedInner(b: Int, c: String)

  object SpecializedInner {
    implicit val reads: Reads[SpecializedInner] = Json.reads
  }

  def main(args: Array[String]): Unit = {
    val rawJson  = "{\"a\": \"{\\\"b\\\": 12, \\\"c\\\": \\\"test\\\"}\"}"
    println(Json.parse(rawJson).validate[Root[SpecializedInner]])
  }
}

Which produced next result in my case:

JsSuccess(Root(Outer(SpecializedInner(12,test))),)

Hope this helps!


问:
CheckForDetailedUpdate throws "Cannot bind to deployment that is not installed"
I have a 10 second timer checking for updates. I have tried putting "check for updates" in the timer routine. I tried "checking for updates" in a background worker . I have tried async "checking for updates". All throw "Cannot bind to deployment that is not installed". After that, I get a new exception, "Object reference not set to an instance of an object". The exception is not necessarily on the 1st check for updates. I have had it run for hours before throwing an exception. After that, it will no longer retrieve an update. If I can't fix the issue, I would like to clear the error. Every 10 seconds out of a hat. I write to a log and/or restart the computer on the main thread.</p>

```<code>Private Sub tmrAppUpdate_Tick(sender As Object, e As EventArgs) Handles tmrAppUpdate.Tick
    If bwAutoUpdates.IsBusy Then Return                             'if updates busy...leave
    bwAutoUpdates.RunWorkerAsync()                                  'check for updates
End Sub

''' <summary>check for and get automatic updates</summary>
Private Sub bwAutoUpdates_DoWork(sender As Object, e As DoWorkEventArgs) Handles bwAutoUpdates.DoWork
    e.Result = {"ok", ""}                                           'default message

    Try
        Dim updateCheck = ApplicationDeployment.CurrentDeployment   'updates
        Dim info = updateCheck.CheckForDetailedUpdate()             'get update info

        If info.UpdateAvailable Then                                'if updates available...
            updateCheck.Update()                                    'download updates
            e.Result = {"restart", "Automatic Update ReStart: "}    'error, error message
        End If                                                      '

    Catch ex As InvalidOperationException                           'error
        e.Result = {"err", "bw ioe: " & ex.Message}                 'error, error message
    Catch ex As DeploymentDownloadException                         'error
        e.Result = {"err", "bw dde: " & ex.Message}                 'error, error message
    Catch ex As InvalidDeploymentException                          'error
        e.Result = {"err", "bw ide: " & ex.Message}                 'error, error message
    Catch ex As TrustNotGrantedException                            'error
        e.Result = {"err", "bw tnge: " & ex.Message}                'error, error message
    Catch ex As Exception                                           'error
        e.Result = {"err", "bw ax: " & ex.Message}                  'error, error message
    End Try                                                         '
End Sub

Private Sub bwAutoUpdates_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bwAutoUpdates.RunWorkerCompleted
    Dim result = CType(e.Result, Array).OfType(Of String)

    If result(1).Length > 0 Then writeLog(result(1))                'write to log!

    Select Case result(0)                                           'what are we going to do?
        Case "ok" : Return                                          'everything ok
        Case "restart" : Application.Restart()                      'restart
        Case "err"                                                  'errors
    End Select
End Sub
</code>```

答:
For the past couple of weeks I have fooled with automatic updates. It has bugs and fails if your app checks for updates often. I have included a link to the full project. You can download it, run it for a couple of days and watch it fail (usually within a couple of hours). When it fails, there seems to be no way to clear the error without a restarting. I have seen reports of this bug going back 10 years. If you find a fix, please let me know. Thanks Sandy

vb.net AutomaticUpdates

Body:
I have a 10 second timer checking for updates. I have tried putting "check for updates" in the timer routine. I tried "checking for updates" in a background worker . I have tried async "checking for updates". All throw "Cannot bind to deployment that is not installed". After that, I get a new exception, "Object reference not set to an instance of an object". The exception is not necessarily on the 1st check for updates. I have had it run for hours before throwing an exception. After that, it will no longer retrieve an update. If I can't fix the issue, I would like to clear the error. Every 10 seconds out of a hat. I write to a log and/or restart the computer on the main thread.

Private Sub tmrAppUpdate_Tick(sender As Object, e As EventArgs) Handles tmrAppUpdate.Tick
    If bwAutoUpdates.IsBusy Then Return                             'if updates busy...leave
    bwAutoUpdates.RunWorkerAsync()                                  'check for updates
End Sub

''' check for and get automatic updates
Private Sub bwAutoUpdates_DoWork(sender As Object, e As DoWorkEventArgs) Handles bwAutoUpdates.DoWork
    e.Result = {"ok", ""}                                           'default message

    Try
        Dim updateCheck = ApplicationDeployment.CurrentDeployment   'updates
        Dim info = updateCheck.CheckForDetailedUpdate()             'get update info

        If info.UpdateAvailable Then                                'if updates available...
            updateCheck.Update()                                    'download updates
            e.Result = {"restart", "Automatic Update ReStart: "}    'error, error message
        End If                                                      '

    Catch ex As InvalidOperationException                           'error
        e.Result = {"err", "bw ioe: " & ex.Message}                 'error, error message
    Catch ex As DeploymentDownloadException                         'error
        e.Result = {"err", "bw dde: " & ex.Message}                 'error, error message
    Catch ex As InvalidDeploymentException                          'error
        e.Result = {"err", "bw ide: " & ex.Message}                 'error, error message
    Catch ex As TrustNotGrantedException                            'error
        e.Result = {"err", "bw tnge: " & ex.Message}                'error, error message
    Catch ex As Exception                                           'error
        e.Result = {"err", "bw ax: " & ex.Message}                  'error, error message
    End Try                                                         '
End Sub

Private Sub bwAutoUpdates_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bwAutoUpdates.RunWorkerCompleted
    Dim result = CType(e.Result, Array).OfType(Of String)

    If result(1).Length > 0 Then writeLog(result(1))                'write to log!

    Select Case result(0)                                           'what are we going to do?
        Case "ok" : Return                                          'everything ok
        Case "restart" : Application.Restart()                      'restart
        Case "err"                                                  'errors
    End Select
End Sub


问:
Is it possible to measure time performance of the functions that are called inside jar of some library?
I am using some library in my maven based project. The jar of this library may/may not contain some other dependent libraries.

I am calling xyz function of this library. There is a requirement where I need to check time taken by each of the functions that are called inside xyz function. These functions may/ may not be publicly accessible.

Any idea how to do this ? Would aspect work in such scenario ?

答:
Yes, aspects should work. Here is the small example for spring package

```<code>private final Logger log = LoggerFactory.getLogger(this.getClass());

@Pointcut("within(@org.springframework.stereotype.Repository *)")
public void springBeanPointcut() {
}

@Around("springBeanPointcut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    if (log.isDebugEnabled()) {
        log.debug("Enter: {}.{}()",
                joinPoint.getSignature().getDeclaringTypeName(),
                joinPoint.getSignature().getName());
    }
    try {
        Object result = joinPoint.proceed();
        if (log.isDebugEnabled()) {
            log.debug("Exit: {}.{}()",
                    joinPoint.getSignature().getDeclaringTypeName(),
                    joinPoint.getSignature().getName());
        }
        return result;
    } catch (IllegalArgumentException e) {
        log.error("Error:{}.{}()",
                joinPoint.getSignature().getDeclaringTypeName(),
                joinPoint.getSignature().getName());

        throw e;
    }
}
</code>```

Configure your logger format for logging millisecond time log, or just put your logic to compute the time taken.

Body:
Yes, aspects should work. Here is the small example for spring package

private final Logger log = LoggerFactory.getLogger(this.getClass());

@Pointcut("within(@org.springframework.stereotype.Repository *)")
public void springBeanPointcut() {
}

@Around("springBeanPointcut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    if (log.isDebugEnabled()) {
        log.debug("Enter: {}.{}()",
                joinPoint.getSignature().getDeclaringTypeName(),
                joinPoint.getSignature().getName());
    }
    try {
        Object result = joinPoint.proceed();
        if (log.isDebugEnabled()) {
            log.debug("Exit: {}.{}()",
                    joinPoint.getSignature().getDeclaringTypeName(),
                    joinPoint.getSignature().getName());
        }
        return result;
    } catch (IllegalArgumentException e) {
        log.error("Error:{}.{}()",
                joinPoint.getSignature().getDeclaringTypeName(),
                joinPoint.getSignature().getName());

        throw e;
    }
}

Configure your logger format for logging millisecond time log, or just put your logic to compute the time taken.


问:
How to prevent internal field shift in views when base table gets additional fields?
Today there was an interesting issue in our MS SQL Server database view.

There are several base tables. One is sys_user an the others join in some additional data.

The (beginning of) SQL view definition looks like this:

```<code>ALTER VIEW [dbo].[V_Usage] AS
SELECT u.*,
    case when assexcl.sys_id IS NULL THEN asslookup.home_office ELSE assexcl.home_office END [Home Office],
    case when assexcl.sys_id IS NULL THEN asslookup.assigned_group ELSE assexcl.assigned_group END [Assigned Group],
    case when assexcl.sys_id IS NULL THEN asslookup.drs_hub ELSE assexcl.drs_hub END [DRS Hub],
  x.request_sys_id, x.request_pa_name, x.u_allocation request_allocation, x.u_negotiated_deadline, x.u_research_time, x.u_qa_time_drs, x.u_assignment_group, x.u_utilization_category
FROM sys_user u
    LEFT outer join (...
</code>```

The select u.* brings in all fields of the sys_user table and some other fields are also shown per user which is queried in the lower part.

After adding a field in the sys_user table we had the issues that all content in the last fields was shiftet one column to the right.

[Home Office] did contain the last field of the sys_user table,
[Assigned Group] did contain the content of [Home Office]
etc.

First I would like to understand the issue. I think it is due to internal compilation and referencing.

And second I would like to have a solution to make that not happen again. I would prefer not reference all fields directly in the view since it was intentional that all sys_user fields should be in the view.

Is there a kind of flag or db hint to keep those view correct even when base tables get fields added?

Any information appreciated.

答:
Having spent lots and lots of time (once upon a time) debugging problems cause by such queries, I strongly advise you to never use * in views.

That said, you can prevent changes to the underlying tables using SCHEMABINDING.

Binds the view to the schema of the underlying table or tables. When SCHEMABINDING is specified, the base table or tables cannot be modified in a way that would affect the view definition.

As the documentation says, this prevents changes to the other tables unless the view is recreated.  It does not automatically change the view, but it does prevent damaging changes from occurring.

You might also investigate WHY table structures are changing in -- what is presumably -- a production database.  That seems like a bad design.

Body:
Today there was an interesting issue in our MS SQL Server database view.

There are several base tables. One is sys_user an the others join in some additional data.

The (beginning of) SQL view definition looks like this:

ALTER VIEW [dbo].[V_Usage] AS
SELECT u.*,
    case when assexcl.sys_id IS NULL THEN asslookup.home_office ELSE assexcl.home_office END [Home Office],
    case when assexcl.sys_id IS NULL THEN asslookup.assigned_group ELSE assexcl.assigned_group END [Assigned Group],
    case when assexcl.sys_id IS NULL THEN asslookup.drs_hub ELSE assexcl.drs_hub END [DRS Hub],
  x.request_sys_id, x.request_pa_name, x.u_allocation request_allocation, x.u_negotiated_deadline, x.u_research_time, x.u_qa_time_drs, x.u_assignment_group, x.u_utilization_category
FROM sys_user u
    LEFT outer join (...

The select u.* brings in all fields of the sys_user table and some other fields are also shown per user which is queried in the lower part.

After adding a field in the sys_user table we had the issues that all content in the last fields was shiftet one column to the right.

[Home Office] did contain the last field of the sys_user table,
[Assigned Group] did contain the content of [Home Office]
etc.

First I would like to understand the issue. I think it is due to internal compilation and referencing.

And second I would like to have a solution to make that not happen again. I would prefer not reference all fields directly in the view since it was intentional that all sys_user fields should be in the view.

Is there a kind of flag or db hint to keep those view correct even when base tables get fields added?

Any information appreciated.


问:
ArrayList to update HTML Table
Good Evening,
I've got to update my already existing Table with the data of an ArrayList, which consists of Database data. The List is implemented like this:

```<code>ResultSet rs1 = st.executeQuery("SELECT * FROM Termine");
List<String[]> list = new ArrayList<>();
while(rs1.next()){
    String veranName = rs1.getString(1);
    String terminid = rs1.getString(2);
    list.add(new String[]{veranName, terminid});
    }
</code>```

Now i don't know how i can iterate through the List in Javascript or HTML and update my HTML Table with these Elements.

I wanted to use this:

```<code>ResultSet rs1 = st.executeQuery("SELECT * FROM Termine");
List<String[]> list = new ArrayList<>();
while(rs1.next()){
    String veranName = rs1.getString(1);
    String terminid = rs1.getString(2);
    updateTable(veranName,terminid);
    }
</code>```

With updateTable(vname, vid){document.getElementById(vid).value=vname;}
But this didnt work, because the DOC operations are in Javascript and it wasn't found (at least i think thats why). The Code belongs to a JSP Webapplication, in which i have to code something like a weekly calendar, which is displayed as a html table.

Has anyone got a Solution for my matter?
Future Thanks :)

答:
The Java code that fetches the rows from the database is performed in an Action or Controller class depending the the technology you use. You have to add the list to the session attributes if you want to use it in a JSP:

```<code>List<String[]> appointments = new ArrayList<>();
// ... fill the list
request.setAttribute("termine", appointments);
</code>```

In the JSP you retrieve the list from the request attributes and can iterate over it, eg:

```<code><%
List<String[]> appointments = (List<String[]>) request.getAttribute("termine");
for (String[] appointment : appointments) {
%>
  <div>veranName: <%= appointment[0] %></div>
  <div>terminid: <%= appointment[1] %></div>
<%
}
%>
</code>```

ContentLicense: CC BY-SA 3.0

Title: ArrayList to update HTML Table

Body:
Good Evening,
I've got to update my already existing Table with the data of an ArrayList, which consists of Database data. The List is implemented like this:

ResultSet rs1 = st.executeQuery("SELECT * FROM Termine");
List<String[]> list = new ArrayList<>();
while(rs1.next()){
    String veranName = rs1.getString(1);
    String terminid = rs1.getString(2);
    list.add(new String[]{veranName, terminid});
    }

Now i don't know how i can iterate through the List in Javascript or HTML and update my HTML Table with these Elements.

I wanted to use this:

ResultSet rs1 = st.executeQuery("SELECT * FROM Termine");
List<String[]> list = new ArrayList<>();
while(rs1.next()){
    String veranName = rs1.getString(1);
    String terminid = rs1.getString(2);
    updateTable(veranName,terminid);
    }

With updateTable(vname, vid){document.getElementById(vid).value=vname;}
But this didnt work, because the DOC operations are in Javascript and it wasn't found (at least i think thats why). The Code belongs to a JSP Webapplication, in which i have to code something like a weekly calendar, which is displayed as a html table.

Has anyone got a Solution for my matter?
Future Thanks :)

Body:
The Java code that fetches the rows from the database is performed in an Action or Controller class depending the the technology you use. You have to add the list to the session attributes if you want to use it in a JSP:

List<String[]> appointments = new ArrayList<>();
// ... fill the list
request.setAttribute("termine", appointments);

In the JSP you retrieve the list from the request attributes and can iterate over it, eg:

<%
List<String[]> appointments = (List<String[]>) request.getAttribute("termine");
for (String[] appointment : appointments) {
%>
  <div>veranName: <%= appointment[0] %></div>
  <div>terminid: <%= appointment[1] %></div>
<%
}
%>


问:
Rails 3, make exception links clickable and open in editor (textmate)
I have a Rails 3 installation running on a remote development server. Is it possible to make the exception lines clickable and open in an editor like textmate on my local maschine? I would save a lot of time if I did not have to manually open textmate, find file and line number.

NoMethodError in Arts#index

Showing /home/demo1/app/widgets/art_widgets/list/display.html.erb where line #9 raised:

Best regards.
Asbjørn Morell

答:
I also wanted textmate link(txmt://) in Rails Error Page. But until now no success.

rails-footnotes has similar feature. It insert txmt link to every page. But not in Error page.

someone on rails-talk mailing list asked same question. and said merb had that feature.

ContentLicense: CC BY-SA 2.5


问:
How can I get a c variable from cgo?
```<code>package main

/*
#include <malloc.h>
#include <windows.h>
HDC *hdcArr

BOOL CALLBACK EnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
    for (int i = 0; i < (_msize(hdcArr) / sizeof(HDC)); i++) {
        if (hdcArr[i] == NULL) {
            hdcArr[i] = hdcMonitor;
            break;
        }
    }
    return TRUE;
}
void Init() {
    int count = GetSystemMetrics(SM_CMONITORS);
    hdcArr = (HDC*)malloc(sizeof(HDC) * count);
    memset(hdcArr, 0, sizeof(HDC) * count);
}
HDC* GetHDC() {
    return *hdcArr;
}
*/
import "C"
import (
    "fmt"
    "reflect"
    "unsafe"
    ".../w32"
)
func main() {
    var hdc w32.HDC
    hdc = w32.GetDC(0)
    C.Init()
    w32.EnumDisplayMonitors(hdc, nil, reflect.ValueOf(C.EnumProc).Pointer(), 0)
    t := (*[]w32.HDC)(unsafe.Pointer(&C.hdcArr))
    cx := w32.GetDeviceCaps((*t)[0], w32.HORZRES)
    fmt.Println(cx)
}
</code>```

I wrote the source code as above.

What I want is to import cgo's HDC array into a w32.HDC array to know the width and height values of each monitor.

However, if you import t: = (* [] w32.HDC) unsafe.Pointer (& C.hdcArr)) and call cx: = w32.GetDeviceCaps ((* t) [0], w32.HORZRES) Only 0 is returned.

How can I use cgo to find the width and height of multiple monitors?

答:
package main

/*
#cgo LDFLAGS: -lgdi32
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <windows.h>
HDC *hdcArr;
int count;

BOOL CALLBACK EnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
    int i;
    for (i = 0; i < (_msize(hdcArr) / sizeof(HDC)); i++) {
        if (hdcArr[i] == NULL) {
            hdcArr[i] = CreateCompatibleDC(hdcMonitor);
            break;
        }
    }
    return TRUE;
}
void Init() {
    count = GetSystemMetrics(SM_CMONITORS);
    hdcArr = (HDC*)malloc(sizeof(HDC) * count);
    memset(hdcArr, 0, sizeof(HDC) * count);
}
*/
import "C"

import (
    "fmt"
    "reflect"
    "unsafe"

    "github.com/JamesHovious/w32"
)

func main() {
    C.Init()
    hdc := w32.GetDC(0)
    w32.EnumDisplayMonitors(hdc, nil, reflect.ValueOf(C.EnumProc).Pointer(), 0)
    w32.ReleaseDC(0, hdc)
    t := (*[256]w32.HDC)(unsafe.Pointer(C.hdcArr))[:C.count:C.count]
    for _, dc := range t {
        cx := w32.GetDeviceCaps(dc, w32.HORZRES)
        fmt.Println(cx)
        w32.DeleteDC(dc)
    }
    C.free(unsafe.Pointer(C.hdcArr))
}
```

<p>It's very important for you to understand, that a pointer to a C-Array is just a memory address without any information about size (hence why your t array was empty). This is why you have to cast it to a big array first <code>(*[256]w32.HDC)</code> and then slice it to the right size <code>[:C.count:C.count]</code></p>

<p>That's why i made <code>count</code> a global variable.</p>

<p>The other issue you ran into was that the hdc handles passed to <code>EnumProc</code> were only valid inside the callback. To make them permanent and usable outside the callback scope, you have to call <code>CreateCompatibleDC(hdcMonitor);</code>. <br />
To use this function with cgo you will have to include the lib gdi32 via <code>#cgo LDFLAGS: -lgdi32</code></p>

<p>Once you are done using these DC's you have to free them again with <code>w32.DeleteDC(dc)</code></p>

<p>Also don't forget to free your malloced array with <code>C.free(unsafe.Pointer(C.hdcArr))</code></p>

<p>My advice: Whenever you use a WinApi, carefully read the msdn documentation. It takes some time, but saves you lots of trouble</p>

<p>You can also do this entirely in golang without cgo:</p>

<pre class="lang-golang prettyprint-override"><code>package main

import (
    "fmt"
    "syscall"

    "github.com/JamesHovious/w32"
)

func EnumProc(hMonitor w32.HMONITOR, hdcMonitor w32.HDC, lprcMonitor *w32.RECT, dwData w32.LPARAM) uintptr {
    fmt.Println(w32.GetDeviceCaps(hdcMonitor, w32.HORZRES))
    return w32.TRUE
}

func main() {
    hdc := w32.GetDC(0)
    w32.EnumDisplayMonitors(hdc, nil, syscall.NewCallback(EnumProc), 0)
    w32.ReleaseDC(0, hdc)
}
</code>```

or even smoother:

func EnumProc(hMonitor w32.HMONITOR, hdcMonitor w32.HDC, lprcMonitor *w32.RECT, dwData w32.LPARAM) uintptr {
    horzres := lprcMonitor.Right - lprcMonitor.Left
    fmt.Println(horzres)
    return w32.TRUE
}

func main() {
    w32.EnumDisplayMonitors(nil, nil, syscall.NewCallback(EnumProc), 0)
}
```

Title: How can I get a c variable from cgo?

Body:
package main

/*
#include <malloc.h>
#include <windows.h>
HDC *hdcArr

BOOL CALLBACK EnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
    for (int i = 0; i < (_msize(hdcArr) / sizeof(HDC)); i++) {
        if (hdcArr[i] == NULL) {
            hdcArr[i] = hdcMonitor;
            break;
        }
    }
    return TRUE;
}
void Init() {
    int count = GetSystemMetrics(SM_CMONITORS);
    hdcArr = (HDC*)malloc(sizeof(HDC) * count);
    memset(hdcArr, 0, sizeof(HDC) * count);
}
HDC* GetHDC() {
    return *hdcArr;
}
*/
import "C"
import (
    "fmt"
    "reflect"
    "unsafe"
    ".../w32"
)
func main() {
    var hdc w32.HDC
    hdc = w32.GetDC(0)
    C.Init()
    w32.EnumDisplayMonitors(hdc, nil, reflect.ValueOf(C.EnumProc).Pointer(), 0)
    t := (*[]w32.HDC)(unsafe.Pointer(&C.hdcArr))
    cx := w32.GetDeviceCaps((*t)[0], w32.HORZRES)
    fmt.Println(cx)
}

I wrote the source code as above.

What I want is to import cgo's HDC array into a w32.HDC array to know the width and height values of each monitor.

However, if you import t: = (* [] w32.HDC) unsafe.Pointer (& C.hdcArr)) and call cx: = w32.GetDeviceCaps ((* t) [0], w32.HORZRES) Only 0 is returned.

How can I use cgo to find the width and height of multiple monitors?

Body:
package main

/*
#cgo LDFLAGS: -lgdi32
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <windows.h>
HDC *hdcArr;
int count;

BOOL CALLBACK EnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
    int i;
    for (i = 0; i < (_msize(hdcArr) / sizeof(HDC)); i++) {
        if (hdcArr[i] == NULL) {
            hdcArr[i] = CreateCompatibleDC(hdcMonitor);
            break;
        }
    }
    return TRUE;
}
void Init() {
    count = GetSystemMetrics(SM_CMONITORS);
    hdcArr = (HDC*)malloc(sizeof(HDC) * count);
    memset(hdcArr, 0, sizeof(HDC) * count);
}
*/
import "C"

import (
    "fmt"
    "reflect"
    "unsafe"

    "github.com/JamesHovious/w32"
)

func main() {
    C.Init()
    hdc := w32.GetDC(0)
    w32.EnumDisplayMonitors(hdc, nil, reflect.ValueOf(C.EnumProc).Pointer(), 0)
    w32.ReleaseDC(0, hdc)
    t := (*[256]w32.HDC)(unsafe.Pointer(C.hdcArr))[:C.count:C.count]
    for _, dc := range t {
        cx := w32.GetDeviceCaps(dc, w32.HORZRES)
        fmt.Println(cx)
        w32.DeleteDC(dc)
    }
    C.free(unsafe.Pointer(C.hdcArr))
}

It's very important for you to understand, that a pointer to a C-Array is just a memory address without any information about size (hence why your t array was empty). This is why you have to cast it to a big array first (*[256]w32.HDC) and then slice it to the right size [:C.count:C.count]

That's why i made count a global variable.

The other issue you ran into was that the hdc handles passed to EnumProc were only valid inside the callback. To make them permanent and usable outside the callback scope, you have to call CreateCompatibleDC(hdcMonitor);.

To use this function with cgo you will have to include the lib gdi32 via #cgo LDFLAGS: -lgdi32

Once you are done using these DC's you have to free them again with w32.DeleteDC(dc)

Also don't forget to free your malloced array with C.free(unsafe.Pointer(C.hdcArr))

My advice: Whenever you use a WinApi, carefully read the msdn documentation. It takes some time, but saves you lots of trouble

You can also do this entirely in golang without cgo:

package main

import (
    "fmt"
    "syscall"

    "github.com/JamesHovious/w32"
)

func EnumProc(hMonitor w32.HMONITOR, hdcMonitor w32.HDC, lprcMonitor *w32.RECT, dwData w32.LPARAM) uintptr {
    fmt.Println(w32.GetDeviceCaps(hdcMonitor, w32.HORZRES))
    return w32.TRUE
}

func main() {
    hdc := w32.GetDC(0)
    w32.EnumDisplayMonitors(hdc, nil, syscall.NewCallback(EnumProc), 0)
    w32.ReleaseDC(0, hdc)
}

or even smoother:

func EnumProc(hMonitor w32.HMONITOR, hdcMonitor w32.HDC, lprcMonitor *w32.RECT, dwData w32.LPARAM) uintptr {
    horzres := lprcMonitor.Right - lprcMonitor.Left
    fmt.Println(horzres)
    return w32.TRUE
}

func main() {
    w32.EnumDisplayMonitors(nil, nil, syscall.NewCallback(EnumProc), 0)
}


问:
Check my Kafka and Zookeeper functionality and connections
As part of my system, I need to use Kafka and Zookeeper cluster on top of Kubernetes.
I'm using Statefulset for deploying them and use headless service for Kafka's Broker to be able to talk with the Zookeeper servers.

Its seems that the clusters are running - (by typing kubectl get pods)

```<code>NAME                      READY   STATUS    RESTARTS   AGE
kafka-statefulset-0       1/1     Running   14         5d10h
kafka-statefulset-1       1/1     Running   14         5d10h
kafka-statefulset-2       1/1     Running   16         5d10h
kafka-statefulset-3       1/1     Running   14         5d10h
kafka-statefulset-4       1/1     Running   14         5d10h
zookeeper-statefulset-0   1/1     Running   5          5d10h
zookeeper-statefulset-1   1/1     Running   5          5d10h
zookeeper-statefulset-2   1/1     Running   5          5d10h
</code>```

My problem is that I do not really understand how can I check if they can communicate properly.

What I have already tried -
I tried -

```<code> kafkacat -L -b kafka-statefulset-0.kafka headless.default.svc.cluster.local:9093
</code>```

and got -

% ERROR: Failed to acquire metadata: Local: Broker transport failure

I tried -

```<code>kafkacat -b 172.17.0.10:9093 -t second_topic -P
</code>```

and got -

```<code>% ERROR: Local: Host resolution failure: kafka-statefulset-0.kafka-headless.default.svc.cluster.local:9093/0: Failed to resolve 'kafka-statefulset-0.kafka-headless.default.svc.cluster.local:9093': Name or service not known
% ERROR: Local: Host resolution failure: kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093/1: Failed to resolve 'kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093': Name or service not known
% ERROR: Local: Host resolution failure: kafka-statefulset-3.kafka-headless.default.svc.cluster.local:9093/3: Failed to resolve 'kafka-statefulset-3.kafka-headless.default.svc.cluster.local:9093': Name or service not known
% ERROR: Local: Host resolution failure: kafka-statefulset-4.kafka-headless.default.svc.cluster.local:9093/4: Failed to resolve 'kafka-statefulset-4.kafka-headless.default.svc.cluster.local:9093': Name or service not known
% ERROR: Local: Host resolution failure: kafka-statefulset-2.kafka-headless.default.svc.cluster.local:9093/2: Failed to resolve 'kafka-statefulset-2.kafka-headless.default.svc.cluster.local:9093': Name or service not known
</code>```

but when I ran -

```<code>kafkacat -L -b 172.17.0.10:9093
</code>```

I get -

```<code>Metadata for all topics (from broker -1: 172.17.0.10:9093/bootstrap):
 5 brokers:
  broker 2 at kafka-statefulset-2.kafka-headless.default.svc.cluster.local:9093
  broker 4 at kafka-statefulset-4.kafka-headless.default.svc.cluster.local:9093
  broker 1 at kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093
  broker 3 at kafka-statefulset-3.kafka-headless.default.svc.cluster.local:9093
  broker 0 at kafka-statefulset-0.kafka-headless.default.svc.cluster.local:9093
 4 topics:
  topic "second_topic" with 1 partitions:
    partition 0, leader 4, replicas: 4, isrs: 4
</code>```

As I understand right now, I have not configured the services correctly for connecting with them from outside the cluster, but I can connect to them from within the cluster.
Although I can communicate with them from within the cluster, I keep getting errors I can't understand.

For example (installing Kafkacat inside the pod's container and trying to talk with other Kafka's broker) -

```<code> > kubectl exec -it  kafka-statefulset-0 bash
 > apt-get install kafkacat
 > kafkacat -b kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093 -t TOPIC -P
 > kafkacat -L -b kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093
</code>```

I got the right metadata but with error in the end -

```<code>etadata for all topics (from broker -1: kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093/bootstrap):
 5 brokers:
  broker 2 at kafka-statefulset-2.kafka-headless.default.svc.cluster.local:9093
  broker 4 at kafka-statefulset-4.kafka-headless.default.svc.cluster.local:9093
  broker 1 at kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093
  broker 3 at kafka-statefulset-3.kafka-headless.default.svc.cluster.local:9093
  broker 0 at kafka-statefulset-0.kafka-headless.default.svc.cluster.local:9093
 5 topics:
  topic "TOPIC" with 1 partitions:
    partition 0, leader 2, replicas: 2, isrs: 2
  topic "second_topic" with 1 partitions:
    partition 0, leader 4, replicas: 4, isrs: 4
  topic "first_topic" with 1 partitions:
    partition 0, leader 2, replicas: 2, isrs: 2
  topic "nir_topic" with 1 partitions:
    partition 0, leader 0, replicas: 0, isrs: 0
  topic "first" with 1 partitions:
    partition 0, leader 3, replicas: 3, isrs: 3
%3|1581685918.022|FAIL|rdkafka#producer-0| kafka-statefulset-0.kafka-headless.default.svc.cluster.local:9093/0: Failed to connect to broker at kafka-statefulset-0.kafka-headless.default.svc.cluster.local:: Interrupted system call
%3|1581685918.022|ERROR|rdkafka#producer-0| kafka-statefulset-0.kafka-headless.default.svc.cluster.local:9093/0: Failed to connect to broker at kafka-statefulset-0.kafka-headless.default.svc.cluster.local:: Interrupted system call
%3|1581685918.022|FAIL|rdkafka#producer-0| kafka-statefulset-2.kafka-headless.default.svc.cluster.local:9093/2: Failed to connect to broker at kafka-statefulset-2.kafka-headless.default.svc.cluster.local:: Interrupted system call
%3|1581685918.022|ERROR|rdkafka#producer-0| kafka-statefulset-2.kafka-headless.default.svc.cluster.local:9093/2: Failed to connect to broker at kafka-statefulset-2.kafka-headless.default.svc.cluster.local:: Interrupted system call
%3|1581685918.023|FAIL|rdkafka#producer-0| kafka-statefulset-4.kafka-headless.default.svc.cluster.local:9093/4: Failed to connect to broker at kafka-statefulset-4.kafka-headless.default.svc.cluster.local:: Interrupted system call
%3|1581685918.023|ERROR|rdkafka#producer-0| kafka-statefulset-4.kafka-headless.default.svc.cluster.local:9093/4: Failed to connect to broker at kafka-statefulset-4.kafka-headless.default.svc.cluster.local:: Interrupted system call
</code>```

What cloud be the problem?
How can I check the communication between them and check the functionalities correctness of both of them - Kafka and Zookeeper?

Some more information about the system (I can copy all the YAML configuration if needed, but it is pretty long)-

There is a Statefulset of Kafka and Statefulset of Zookeeper.

Kafka's Broker can talk with the zookeeper through port 2181.
My configuration is -

-override zookeeper.connect=zookeeper-statefulset-0.zookeeper-headless.default.svc.cluster.local:2181 ...

I also built headless service for talking with each Kafka's Broker through port 9093

Zookeeper ports:

a. 2181 - client port

b. 2888 - server port

c. 3888 - port for leader election

5.The services which run -

```<code>NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)             AGE
kafka-headless       ClusterIP   None            <none>        9093/TCP            5d10h
kubernetes           ClusterIP   10.96.0.1       <none>        443/TCP             5d11h
zookeeper-cs         ClusterIP   10.106.99.170   <none>        2181/TCP            5d11h
zookeeper-headless   ClusterIP   None            <none>        2888/TCP,3888/TCP   5d11h
</code>```

UPDATE - Adding Kafka and Zookeeper configuration files
kafka -

```<code>apiVersion: apps/v1
kind: StatefulSet
metadata:
spec:
  selector:
    matchLabels:
      app: kafka-app
  serviceName: kafka-headless
  replicas: 5
  podManagementPolicy: Parallel
  template:
    metadata:
      labels:
        app: kafka-app
        kafkaMinReplicas: need-supervision
    spec:
      affinity:
        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
             - weight: 1
               podAffinityTerm:
                 labelSelector:
                    matchExpressions:
                      - key: "app"
                        operator: In
                        values:
                        - zookeeper-app
                 topologyKey: "kubernetes.io/hostname"
      terminationGracePeriodSeconds: 300
      containers:
      - name: k8skafka
        imagePullPolicy: Always
        image: gcr.io/google_containers/kubernetes-kafka:1.0-10.2.1
        resources:
          requests:
            memory: "1Gi"
            cpu: 500m
        ports:
        - containerPort: 9093
          name: server
        command:
        - sh
        - -c
        - "exec kafka-server-start.sh /opt/kafka/config/server.properties --override broker.id=${HOSTNAME##*-} \
          --override listeners=PLAINTEXT://:9093 \
          --override zookeeper.connect=zookeeper-statefulset-0.zookeeper-headless.default.svc.cluster.local:2181,zookeeper-statefulset-1.zookeeper-headless.default.svc.cluster.local:2181,zookeeper-statefulset-2.zookeeper-headless.default.svc.cluster.local:2181 \
          --override log.dir=/var/lib/kafka \
          --override auto.create.topics.enable=true \
          --override auto.leader.rebalance.enable=true \
          --override background.threads=10 \
          --override compression.type=producer \
          --override delete.topic.enable=false \
          --override leader.imbalance.check.interval.seconds=300 \
          --override leader.imbalance.per.broker.percentage=10 \
          --override log.flush.interval.messages=9223372036854775807 \
          --override log.flush.offset.checkpoint.interval.ms=60000 \
          --override log.flush.scheduler.interval.ms=9223372036854775807 \
          --override log.retention.bytes=-1 \
          --override log.retention.hours=168 \
          --override log.roll.hours=168 \
          --override log.roll.jitter.hours=0 \
          --override log.segment.bytes=1073741824 \
          --override log.segment.delete.delay.ms=60000 \
          --override message.max.bytes=1000012 \
          --override min.insync.replicas=1 \
          --override num.io.threads=8 \
          --override num.network.threads=3 \
          --override num.recovery.threads.per.data.dir=1 \
          --override num.replica.fetchers=1 \
          --override offset.metadata.max.bytes=4096 \
          --override offsets.commit.required.acks=-1 \
          --override offsets.commit.timeout.ms=5000 \
          --override offsets.load.buffer.size=5242880 \
          --override offsets.retention.check.interval.ms=600000 \
          --override offsets.retention.minutes=1440 \
          --override offsets.topic.compression.codec=0 \
          --override offsets.topic.num.partitions=50 \
          --override offsets.topic.replication.factor=3 \
          --override offsets.topic.segment.bytes=104857600 \
          --override queued.max.requests=500 \
          --override quota.consumer.default=9223372036854775807 \
          --override quota.producer.default=9223372036854775807 \
          --override replica.fetch.min.bytes=1 \
          --override replica.fetch.wait.max.ms=500 \
          --override replica.high.watermark.checkpoint.interval.ms=5000 \
          --override replica.lag.time.max.ms=10000 \
          --override replica.socket.receive.buffer.bytes=65536 \
          --override replica.socket.timeout.ms=30000 \
          --override request.timeout.ms=30000 \
          --override socket.receive.buffer.bytes=102400 \
          --override socket.request.max.bytes=104857600 \
          --override socket.send.buffer.bytes=102400 \
          --override unclean.leader.election.enable=true \
          --override zookeeper.session.timeout.ms=30000 \
          --override zookeeper.set.acl=false \
          --override broker.id.generation.enable=true \
          --override connections.max.idle.ms=600000 \
          --override controlled.shutdown.enable=true \
          --override controlled.shutdown.max.retries=3 \
          --override controlled.shutdown.retry.backoff.ms=5000 \
          --override controller.socket.timeout.ms=30000 \
          --override default.replication.factor=1 \
          --override fetch.purgatory.purge.interval.requests=1000 \
          --override group.max.session.timeout.ms=300000 \
          --override group.min.session.timeout.ms=6000 \
          --override inter.broker.protocol.version=0.10.2-IV0 \
          --override log.cleaner.backoff.ms=15000 \
          --override log.cleaner.dedupe.buffer.size=134217728 \
          --override log.cleaner.delete.retention.ms=86400000 \
          --override log.cleaner.enable=true \
          --override log.cleaner.io.buffer.load.factor=0.9 \
          --override log.cleaner.io.buffer.size=524288 \
          --override log.cleaner.io.max.bytes.per.second=1.7976931348623157E308 \
          --override log.cleaner.min.cleanable.ratio=0.5 \
          --override log.cleaner.min.compaction.lag.ms=0 \
          --override log.cleaner.threads=1 \
          --override log.cleanup.policy=delete \
          --override log.index.interval.bytes=4096 \
          --override log.index.size.max.bytes=10485760 \
          --override log.message.timestamp.difference.max.ms=9223372036854775807 \
          --override log.message.timestamp.type=CreateTime \
          --override log.preallocate=false \
          --override log.retention.check.interval.ms=300000 \
          --override max.connections.per.ip=2147483647 \
          --override num.partitions=1 \
          --override producer.purgatory.purge.interval.requests=1000 \
          --override replica.fetch.backoff.ms=1000 \
          --override replica.fetch.max.bytes=1048576 \
          --override replica.fetch.response.max.bytes=10485760 \
          --override reserved.broker.max.id=1000 "
        env:
        - name: KAFKA_HEAP_OPTS
          value : "-Xmx512M -Xms512M"
        - name: KAFKA_OPTS
          value: "-Dlogging.level=INFO"
        volumeMounts:
        - name: datadir
          mountPath: /var/lib/kafka
        readinessProbe:
          exec:
            command:
            - sh
            - -c
            - "/opt/kafka/bin/kafka-broker-api-versions.sh --bootstrap-server=localhost:9093"
      securityContext:
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: datadir
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi
</code>```

Zookeeper -

```<code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: zookeeper-statefulset
spec:
  selector:
    matchLabels:
      app: zookeeper-app
  serviceName: zookeeper-headless
  replicas: 5
  updateStrategy:
    type: RollingUpdate
  podManagementPolicy: OrderedReady
  template:
    metadata:
      labels:
        app: zookeeper-app
    spec:
      containers:
      - name: kubernetes-zookeeper
        imagePullPolicy: Always
        image: "k8s.gcr.io/kubernetes-zookeeper:1.0-3.4.10"
        resources:
          requests:
            memory: "1Gi"
            cpu: "0.5"

        ports:
        # Expose by zookeeper-cs to the client
        - containerPort: 2181
          name: client
        # Expose by zookeeper-headless to the other replicas of the set
        - containerPort: 2888
          name: server
        - containerPort: 3888
          name: leader-election
        command:
        - sh
        - -c
        - "start-zookeeper \
          --servers=3 \
          --data_dir=/var/lib/zookeeper/data \
          --data_log_dir=/var/lib/zookeeper/data/log \
          --conf_dir=/opt/zookeeper/conf \
          --client_port=2181 \
          --election_port=3888 \
          --server_port=2888 \
          --tick_time=2000 \
          --init_limit=10 \
          --sync_limit=5 \
          --heap=512M \
          --max_client_cnxns=60 \
          --snap_retain_count=3 \
          --purge_interval=12 \
          --max_session_timeout=40000 \
          --min_session_timeout=4000 \
          --log_level=INFO"
        readinessProbe:
          exec:
            command: ["sh", "-c", "zookeeper-ready 2181"]
          initialDelaySeconds: 60
          timeoutSeconds: 10
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - "zookeeper-ready 2181"
          initialDelaySeconds: 60
          timeoutSeconds: 10
        volumeMounts:
        - name: zookeeper-volume
          mountPath: /var/lib/zookeeper
      securityContext:
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: zookeeper-volume
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi
</code>```

答:
As You mentioned in comment:

I ran - / # nslookup headless.default.svc.cluster.local and got - Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local nslookup: can't resolve 'headless.default.svc.cluster.local

Problem is related with DNS in your environment as your environment cannot resolve headless service.
DNS component should provide Pod's DNS names.

You should receive something like:

```<code>/ # nslookup my-kafka-headless
Server:    10.122.0.10
Address 1: 10.122.0.10 kube-dns.kube-system.svc.cluster.local

Name:      my-kafka-headless
Address 1: 10.56.0.5 my-kafka-0.my-kafka-headless.default.svc.cluster.local
</code>```

Treat is as sort of prerequisite if you want to refer to Pod's backing the Statefulset in Kubernetes based on DNS name (in your case: headless.default.svc.cluster.local).

Verify if your services have set .sepec.clusterIP: None or if everything is ok with kube-dns-XXXX pod in kube-system namespace. Here you can find some information about troubleshooting your DNS issue.

Also as @cricket_007 advised you can use helm for deploying kafka. For example helm chart from this source which also contains HowTo.

Body:
As part of my system, I need to use Kafka and Zookeeper cluster on top of Kubernetes.
I'm using Statefulset for deploying them and use headless service for Kafka's Broker to be able to talk with the Zookeeper servers.

Its seems that the clusters are running - (by typing kubectl get pods)

NAME                      READY   STATUS    RESTARTS   AGE
kafka-statefulset-0       1/1     Running   14         5d10h
kafka-statefulset-1       1/1     Running   14         5d10h
kafka-statefulset-2       1/1     Running   16         5d10h
kafka-statefulset-3       1/1     Running   14         5d10h
kafka-statefulset-4       1/1     Running   14         5d10h
zookeeper-statefulset-0   1/1     Running   5          5d10h
zookeeper-statefulset-1   1/1     Running   5          5d10h
zookeeper-statefulset-2   1/1     Running   5          5d10h

My problem is that I do not really understand how can I check if they can communicate properly.

What I have already tried -
I tried -

 kafkacat -L -b kafka-statefulset-0.kafka headless.default.svc.cluster.local:9093

and got -

% ERROR: Failed to acquire metadata: Local: Broker transport failure

I tried -

kafkacat -b 172.17.0.10:9093 -t second_topic -P

and got -

% ERROR: Local: Host resolution failure: kafka-statefulset-0.kafka-headless.default.svc.cluster.local:9093/0: Failed to resolve 'kafka-statefulset-0.kafka-headless.default.svc.cluster.local:9093': Name or service not known
% ERROR: Local: Host resolution failure: kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093/1: Failed to resolve 'kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093': Name or service not known
% ERROR: Local: Host resolution failure: kafka-statefulset-3.kafka-headless.default.svc.cluster.local:9093/3: Failed to resolve 'kafka-statefulset-3.kafka-headless.default.svc.cluster.local:9093': Name or service not known
% ERROR: Local: Host resolution failure: kafka-statefulset-4.kafka-headless.default.svc.cluster.local:9093/4: Failed to resolve 'kafka-statefulset-4.kafka-headless.default.svc.cluster.local:9093': Name or service not known
% ERROR: Local: Host resolution failure: kafka-statefulset-2.kafka-headless.default.svc.cluster.local:9093/2: Failed to resolve 'kafka-statefulset-2.kafka-headless.default.svc.cluster.local:9093': Name or service not known

but when I ran -

kafkacat -L -b 172.17.0.10:9093

I get -

Metadata for all topics (from broker -1: 172.17.0.10:9093/bootstrap):
 5 brokers:
  broker 2 at kafka-statefulset-2.kafka-headless.default.svc.cluster.local:9093
  broker 4 at kafka-statefulset-4.kafka-headless.default.svc.cluster.local:9093
  broker 1 at kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093
  broker 3 at kafka-statefulset-3.kafka-headless.default.svc.cluster.local:9093
  broker 0 at kafka-statefulset-0.kafka-headless.default.svc.cluster.local:9093
 4 topics:
  topic "second_topic" with 1 partitions:
    partition 0, leader 4, replicas: 4, isrs: 4

As I understand right now, I have not configured the services correctly for connecting with them from outside the cluster, but I can connect to them from within the cluster.
Although I can communicate with them from within the cluster, I keep getting errors I can't understand.

For example (installing Kafkacat inside the pod's container and trying to talk with other Kafka's broker) -

 > kubectl exec -it  kafka-statefulset-0 bash
 > apt-get install kafkacat
 > kafkacat -b kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093 -t TOPIC -P
 > kafkacat -L -b kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093

I got the right metadata but with error in the end -

etadata for all topics (from broker -1: kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093/bootstrap):
 5 brokers:
  broker 2 at kafka-statefulset-2.kafka-headless.default.svc.cluster.local:9093
  broker 4 at kafka-statefulset-4.kafka-headless.default.svc.cluster.local:9093
  broker 1 at kafka-statefulset-1.kafka-headless.default.svc.cluster.local:9093
  broker 3 at kafka-statefulset-3.kafka-headless.default.svc.cluster.local:9093
  broker 0 at kafka-statefulset-0.kafka-headless.default.svc.cluster.local:9093
 5 topics:
  topic "TOPIC" with 1 partitions:
    partition 0, leader 2, replicas: 2, isrs: 2
  topic "second_topic" with 1 partitions:
    partition 0, leader 4, replicas: 4, isrs: 4
  topic "first_topic" with 1 partitions:
    partition 0, leader 2, replicas: 2, isrs: 2
  topic "nir_topic" with 1 partitions:
    partition 0, leader 0, replicas: 0, isrs: 0
  topic "first" with 1 partitions:
    partition 0, leader 3, replicas: 3, isrs: 3
%3|1581685918.022|FAIL|rdkafka#producer-0| kafka-statefulset-0.kafka-headless.default.svc.cluster.local:9093/0: Failed to connect to broker at kafka-statefulset-0.kafka-headless.default.svc.cluster.local:: Interrupted system call
%3|1581685918.022|ERROR|rdkafka#producer-0| kafka-statefulset-0.kafka-headless.default.svc.cluster.local:9093/0: Failed to connect to broker at kafka-statefulset-0.kafka-headless.default.svc.cluster.local:: Interrupted system call
%3|1581685918.022|FAIL|rdkafka#producer-0| kafka-statefulset-2.kafka-headless.default.svc.cluster.local:9093/2: Failed to connect to broker at kafka-statefulset-2.kafka-headless.default.svc.cluster.local:: Interrupted system call
%3|1581685918.022|ERROR|rdkafka#producer-0| kafka-statefulset-2.kafka-headless.default.svc.cluster.local:9093/2: Failed to connect to broker at kafka-statefulset-2.kafka-headless.default.svc.cluster.local:: Interrupted system call
%3|1581685918.023|FAIL|rdkafka#producer-0| kafka-statefulset-4.kafka-headless.default.svc.cluster.local:9093/4: Failed to connect to broker at kafka-statefulset-4.kafka-headless.default.svc.cluster.local:: Interrupted system call
%3|1581685918.023|ERROR|rdkafka#producer-0| kafka-statefulset-4.kafka-headless.default.svc.cluster.local:9093/4: Failed to connect to broker at kafka-statefulset-4.kafka-headless.default.svc.cluster.local:: Interrupted system call

What cloud be the problem?
How can I check the communication between them and check the functionalities correctness of both of them - Kafka and Zookeeper?

Some more information about the system (I can copy all the YAML configuration if needed, but it is pretty long)-

There is a Statefulset of Kafka and Statefulset of Zookeeper.

Kafka's Broker can talk with the zookeeper through port 2181.
My configuration is -

-override zookeeper.connect=zookeeper-statefulset-0.zookeeper-headless.default.svc.cluster.local:2181 ...

I also built headless service for talking with each Kafka's Broker through port 9093

Zookeeper ports:

a. 2181 - client port

b. 2888 - server port

c. 3888 - port for leader election

5.The services which run -

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)             AGE
kafka-headless       ClusterIP   None            <none>        9093/TCP            5d10h
kubernetes           ClusterIP   10.96.0.1       <none>        443/TCP             5d11h
zookeeper-cs         ClusterIP   10.106.99.170   <none>        2181/TCP            5d11h
zookeeper-headless   ClusterIP   None            <none>        2888/TCP,3888/TCP   5d11h

UPDATE - Adding Kafka and Zookeeper configuration files
kafka -

apiVersion: apps/v1
kind: StatefulSet
metadata:
spec:
  selector:
    matchLabels:
      app: kafka-app
  serviceName: kafka-headless
  replicas: 5
  podManagementPolicy: Parallel
  template:
    metadata:
      labels:
        app: kafka-app
        kafkaMinReplicas: need-supervision
    spec:
      affinity:
        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
             - weight: 1
               podAffinityTerm:
                 labelSelector:
                    matchExpressions:
                      - key: "app"
                        operator: In
                        values:
                        - zookeeper-app
                 topologyKey: "kubernetes.io/hostname"
      terminationGracePeriodSeconds: 300
      containers:
      - name: k8skafka
        imagePullPolicy: Always
        image: gcr.io/google_containers/kubernetes-kafka:1.0-10.2.1
        resources:
          requests:
            memory: "1Gi"
            cpu: 500m
        ports:
        - containerPort: 9093
          name: server
        command:
        - sh
        - -c
        - "exec kafka-server-start.sh /opt/kafka/config/server.properties --override broker.id=${HOSTNAME##*-} \
          --override listeners=PLAINTEXT://:9093 \
          --override zookeeper.connect=zookeeper-statefulset-0.zookeeper-headless.default.svc.cluster.local:2181,zookeeper-statefulset-1.zookeeper-headless.default.svc.cluster.local:2181,zookeeper-statefulset-2.zookeeper-headless.default.svc.cluster.local:2181 \
          --override log.dir=/var/lib/kafka \
          --override auto.create.topics.enable=true \
          --override auto.leader.rebalance.enable=true \
          --override background.threads=10 \
          --override compression.type=producer \
          --override delete.topic.enable=false \
          --override leader.imbalance.check.interval.seconds=300 \
          --override leader.imbalance.per.broker.percentage=10 \
          --override log.flush.interval.messages=9223372036854775807 \
          --override log.flush.offset.checkpoint.interval.ms=60000 \
          --override log.flush.scheduler.interval.ms=9223372036854775807 \
          --override log.retention.bytes=-1 \
          --override log.retention.hours=168 \
          --override log.roll.hours=168 \
          --override log.roll.jitter.hours=0 \
          --override log.segment.bytes=1073741824 \
          --override log.segment.delete.delay.ms=60000 \
          --override message.max.bytes=1000012 \
          --override min.insync.replicas=1 \
          --override num.io.threads=8 \
          --override num.network.threads=3 \
          --override num.recovery.threads.per.data.dir=1 \
          --override num.replica.fetchers=1 \
          --override offset.metadata.max.bytes=4096 \
          --override offsets.commit.required.acks=-1 \
          --override offsets.commit.timeout.ms=5000 \
          --override offsets.load.buffer.size=5242880 \
          --override offsets.retention.check.interval.ms=600000 \
          --override offsets.retention.minutes=1440 \
          --override offsets.topic.compression.codec=0 \
          --override offsets.topic.num.partitions=50 \
          --override offsets.topic.replication.factor=3 \
          --override offsets.topic.segment.bytes=104857600 \
          --override queued.max.requests=500 \
          --override quota.consumer.default=9223372036854775807 \
          --override quota.producer.default=9223372036854775807 \
          --override replica.fetch.min.bytes=1 \
          --override replica.fetch.wait.max.ms=500 \
          --override replica.high.watermark.checkpoint.interval.ms=5000 \
          --override replica.lag.time.max.ms=10000 \
          --override replica.socket.receive.buffer.bytes=65536 \
          --override replica.socket.timeout.ms=30000 \
          --override request.timeout.ms=30000 \
          --override socket.receive.buffer.bytes=102400 \
          --override socket.request.max.bytes=104857600 \
          --override socket.send.buffer.bytes=102400 \
          --override unclean.leader.election.enable=true \
          --override zookeeper.session.timeout.ms=30000 \
          --override zookeeper.set.acl=false \
          --override broker.id.generation.enable=true \
          --override connections.max.idle.ms=600000 \
          --override controlled.shutdown.enable=true \
          --override controlled.shutdown.max.retries=3 \
          --override controlled.shutdown.retry.backoff.ms=5000 \
          --override controller.socket.timeout.ms=30000 \
          --override default.replication.factor=1 \
          --override fetch.purgatory.purge.interval.requests=1000 \
          --override group.max.session.timeout.ms=300000 \
          --override group.min.session.timeout.ms=6000 \
          --override inter.broker.protocol.version=0.10.2-IV0 \
          --override log.cleaner.backoff.ms=15000 \
          --override log.cleaner.dedupe.buffer.size=134217728 \
          --override log.cleaner.delete.retention.ms=86400000 \
          --override log.cleaner.enable=true \
          --override log.cleaner.io.buffer.load.factor=0.9 \
          --override log.cleaner.io.buffer.size=524288 \
          --override log.cleaner.io.max.bytes.per.second=1.7976931348623157E308 \
          --override log.cleaner.min.cleanable.ratio=0.5 \
          --override log.cleaner.min.compaction.lag.ms=0 \
          --override log.cleaner.threads=1 \
          --override log.cleanup.policy=delete \
          --override log.index.interval.bytes=4096 \
          --override log.index.size.max.bytes=10485760 \
          --override log.message.timestamp.difference.max.ms=9223372036854775807 \
          --override log.message.timestamp.type=CreateTime \
          --override log.preallocate=false \
          --override log.retention.check.interval.ms=300000 \
          --override max.connections.per.ip=2147483647 \
          --override num.partitions=1 \
          --override producer.purgatory.purge.interval.requests=1000 \
          --override replica.fetch.backoff.ms=1000 \
          --override replica.fetch.max.bytes=1048576 \
          --override replica.fetch.response.max.bytes=10485760 \
          --override reserved.broker.max.id=1000 "
        env:
        - name: KAFKA_HEAP_OPTS
          value : "-Xmx512M -Xms512M"
        - name: KAFKA_OPTS
          value: "-Dlogging.level=INFO"
        volumeMounts:
        - name: datadir
          mountPath: /var/lib/kafka
        readinessProbe:
          exec:
            command:
            - sh
            - -c
            - "/opt/kafka/bin/kafka-broker-api-versions.sh --bootstrap-server=localhost:9093"
      securityContext:
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: datadir
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi

Zookeeper -

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: zookeeper-statefulset
spec:
  selector:
    matchLabels:
      app: zookeeper-app
  serviceName: zookeeper-headless
  replicas: 5
  updateStrategy:
    type: RollingUpdate
  podManagementPolicy: OrderedReady
  template:
    metadata:
      labels:
        app: zookeeper-app
    spec:
      containers:
      - name: kubernetes-zookeeper
        imagePullPolicy: Always
        image: "k8s.gcr.io/kubernetes-zookeeper:1.0-3.4.10"
        resources:
          requests:
            memory: "1Gi"
            cpu: "0.5"

        ports:
        # Expose by zookeeper-cs to the client
        - containerPort: 2181
          name: client
        # Expose by zookeeper-headless to the other replicas of the set
        - containerPort: 2888
          name: server
        - containerPort: 3888
          name: leader-election
        command:
        - sh
        - -c
        - "start-zookeeper \
          --servers=3 \
          --data_dir=/var/lib/zookeeper/data \
          --data_log_dir=/var/lib/zookeeper/data/log \
          --conf_dir=/opt/zookeeper/conf \
          --client_port=2181 \
          --election_port=3888 \
          --server_port=2888 \
          --tick_time=2000 \
          --init_limit=10 \
          --sync_limit=5 \
          --heap=512M \
          --max_client_cnxns=60 \
          --snap_retain_count=3 \
          --purge_interval=12 \
          --max_session_timeout=40000 \
          --min_session_timeout=4000 \
          --log_level=INFO"
        readinessProbe:
          exec:
            command: ["sh", "-c", "zookeeper-ready 2181"]
          initialDelaySeconds: 60
          timeoutSeconds: 10
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - "zookeeper-ready 2181"
          initialDelaySeconds: 60
          timeoutSeconds: 10
        volumeMounts:
        - name: zookeeper-volume
          mountPath: /var/lib/zookeeper
      securityContext:
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: zookeeper-volume
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

Body:
As You mentioned in comment:

I ran - / # nslookup headless.default.svc.cluster.local and got - Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local nslookup: can't resolve 'headless.default.svc.cluster.local

Problem is related with DNS in your environment as your environment cannot resolve headless service.
DNS component should provide Pod's DNS names.

You should receive something like:

/ # nslookup my-kafka-headless
Server:    10.122.0.10
Address 1: 10.122.0.10 kube-dns.kube-system.svc.cluster.local

Name:      my-kafka-headless
Address 1: 10.56.0.5 my-kafka-0.my-kafka-headless.default.svc.cluster.local

Treat is as sort of prerequisite if you want to refer to Pod's backing the Statefulset in Kubernetes based on DNS name (in your case: headless.default.svc.cluster.local).

Verify if your services have set .sepec.clusterIP: None or if everything is ok with kube-dns-XXXX pod in kube-system namespace. Here you can find some information about troubleshooting your DNS issue.

Also as @cricket_007 advised you can use helm for deploying kafka. For example helm chart from this source which also contains HowTo.


问:
memory leak - application exited with signal 9 in iphone application
I have an application which is utilizing a lot of memory by using UIImageView, creating graphics context, calling web service, drawing using quartz2d, blending images, playing video, recording and so on. But now the application is exiting after some time with a memory warning.
Is there any way to clear the ram usage like clear cache or something like that.</p>

答:
You, can do one thing, put your application on leaks analyzer, and check out the leaks, and prior you go through your code and release, the allocated objects after those use have finished, by releasing them.

Here is a good tutorial. http://mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-tutorial/


问:
Rails Custom Primary Key: SQLite3::ConstraintException: NOT NULL constraint failed
I have a custom Primary Key (PK), ASIN, in my Products table. This PK is a string.

```<code>  def change
    create_table :products, id: false do |t|
      t.references :user, null: false, foreign_key: true
      t.primary_key :asin
      t.string :image
      t.string :price
      t.string :title

      t.timestamps
    end
      change_column :products, :asin, :string
  end
</code>```

In my models for Product, I have the following:

```<code>  self.primary_key = 'asin'
  belongs_to :user

  validates_uniqueness_of :asin
  validates_presence_of :asin, :image, :title, :price, :user_id

  before_save :before_save

  def before_save
    self.asin  = self.asin.upcase!
  end
</code>```

I have looked over the code several times but while trying to hit this Route and create a Product, this is the error I receive.

```<code>"exception": "#<ActiveRecord::NotNullViolation: SQLite3::ConstraintException: NOT NULL constraint failed: products.asin>"
</code>```

And this is the JSON that I am sending to this create Route

```<code>{
    "asin": "B",
    "title": "Test",
    "image": "test",
    "user_id": 1,
    "price": "test"
}
</code>```

NOTE: I know that it is generally looked down upon to use PKs in this manor and please know I recognize this but this is the ideal setup for my project's goal. Please refrain from leaving mean-spirited comments.

EDIT: For reference, here is the Create function under the Products controller.

```<code>  skip_before_action :authorize_request, only: [:create, :show]

  def create
    Product.create!(product_params)
    json_response(product_params, :created)
  end

  def product_params
    params.permit(
        :asin,
        :image,
        :title,
        :user_id,
        :price
    )
  end
</code>```

答:
EDIT: Part of the issue was that the asin's first letter needed to be downcased before sending to the backend.

I am not totally sure what happened here to fix this. But I ran the following commands just to test things as I was sure this database setup was perfectly fine.

First, I stopped the server using control + c on mac.

I connected to rails using rails c
I connected to the products table using Product.connection
And I created a test Product just to see what was wrong Product.create!(asin: "Test_ASIN", title: "title", image: "image", price: "price", user_id: 1)

I was surprised to see that this worked since this is exactly what I sent to the backend originally, but then I went ahead and started rails s and tried to send the request again and this time, it submitted.

I hope this helps someone out there as this was obnoxiously weird.

Body:
I have a custom Primary Key (PK), ASIN, in my Products table. This PK is a string.

  def change
    create_table :products, id: false do |t|
      t.references :user, null: false, foreign_key: true
      t.primary_key :asin
      t.string :image
      t.string :price
      t.string :title

      t.timestamps
    end
      change_column :products, :asin, :string
  end

In my models for Product, I have the following:

  self.primary_key = 'asin'
  belongs_to :user

  validates_uniqueness_of :asin
  validates_presence_of :asin, :image, :title, :price, :user_id

  before_save :before_save

  def before_save
    self.asin  = self.asin.upcase!
  end

I have looked over the code several times but while trying to hit this Route and create a Product, this is the error I receive.

"exception": "#<ActiveRecord::NotNullViolation: SQLite3::ConstraintException: NOT NULL constraint failed: products.asin>"

And this is the JSON that I am sending to this create Route

{
    "asin": "B",
    "title": "Test",
    "image": "test",
    "user_id": 1,
    "price": "test"
}

NOTE: I know that it is generally looked down upon to use PKs in this manor and please know I recognize this but this is the ideal setup for my project's goal. Please refrain from leaving mean-spirited comments.

EDIT: For reference, here is the Create function under the Products controller.

  skip_before_action :authorize_request, only: [:create, :show]

  def create
    Product.create!(product_params)
    json_response(product_params, :created)
  end

  def product_params
    params.permit(
        :asin,
        :image,
        :title,
        :user_id,
        :price
    )
  end


问:
How to get probability and label prediction at the same time using sklearn
I have a multiclass classifier and I need to get the probability and label at the same time.

model.predict_proba(X) returns the probabilities for all trained classes for each data point.

model.predict(X) returns the label  for each data point.

I don't want to predict twice each data point.

答:
Simply use predict_proba and then to get the label use np.argmax:

```<code>dataset = read_csv('pollution.csv', header=0, index_col=0)
x = dataset[['pollution', 'dew', 'temp', 'press', 'wnd_spd', 'snow']].values
y = dataset[['wnd_dir']].values
from sklearn.ensemble import RandomForestClassifier

cls = RandomForestClassifier(random_state=0)
cls.fit(x, y)

z = cls.predict_proba(x)
labels = np.argmax(z, axis=1)
classes = cls.classes_
labels = [classes[i] for i in labels]
print(accuracy_score(y, labels))
</code>```

Body:
Simply use predict_proba and then to get the label use np.argmax:

dataset = read_csv('pollution.csv', header=0, index_col=0)
x = dataset[['pollution', 'dew', 'temp', 'press', 'wnd_spd', 'snow']].values
y = dataset[['wnd_dir']].values
from sklearn.ensemble import RandomForestClassifier

cls = RandomForestClassifier(random_state=0)
cls.fit(x, y)

z = cls.predict_proba(x)
labels = np.argmax(z, axis=1)
classes = cls.classes_
labels = [classes[i] for i in labels]
print(accuracy_score(y, labels))


问:
How to use my enums defined in my c# class file in MVC view page
I have this enum defined in my c# class file in a seperate folder.

```<code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TurnaroundManagement.DataServices.Context;
using TurnaroundManagement.DataServices.Entities;
using TurnaroundManagement.DataServices.Models;
using TurnaroundManagement.DataServices.Models.EntityModels;
using static TurnaroundManagement.DataServices.Models.CommonModels;

namespace TurnaroundManagement.DataServices.EntityModels
{
    public class OrdersModel
    {
        private TurnaroundManagementContext db = new TurnaroundManagementContext();
        private CommonModels objCommonModels = new CommonModels();

        public enum OrderStatus
        {
            Pending = 1,
            Processed = 2
        };
        public enum MaterialStatus
        {
            OnScheduleDate = 1,
            Delayed = 2,
            Issued = 3
        };
        public enum DeliveryStatus
        {
            Pending = 1,
            Delivered = 2
        };

        public List<Orders> GetAllOrders(int Status)
        {
            List<Orders> OrdersList = new List<Orders>();
            try
            {
                OrdersList = db.Orders.Where(x => x.Status == Status).ToList();
            }
            catch (Exception ex)
            {
                objCommonModels.LogException(ex);
                OrdersList = null;
            }
            return OrdersList;
        }

    }
}
</code>```

How do i use this in my view page ? I cant access them in my view page. i have more enums to access..
I tried using this

@using myproject.dataservices.models.myclassnamethathastheenumvariableinit;

@using static myproject.dataservices.models.myclassnamethathastheenumvariableinit;

@using myproject.dataservices.models;

Nothing above is working..

答:
You can only using namespaces and static classes. If you want to access an type, defined within an other class, as in your example. You can using the namespace, that contains the class (TurnaroundManagement.DataServices.EntityModels) in your case.

Then you need to use the complete type hirachy, to access a member of your enum. For example

```<code>var deliveryStatus = OrdersModel.DeliveryStatus.Pending
</code>```

If you´re writing code within a type of that hirachy, you can omit parts of the path.

I personally only nest type, if the nested type is not intended, to be publically used. You can move your enums safely to their own files. This also helps other developers to find the files containing a specific type faster.

Body:
I have this enum defined in my c# class file in a seperate folder.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TurnaroundManagement.DataServices.Context;
using TurnaroundManagement.DataServices.Entities;
using TurnaroundManagement.DataServices.Models;
using TurnaroundManagement.DataServices.Models.EntityModels;
using static TurnaroundManagement.DataServices.Models.CommonModels;

namespace TurnaroundManagement.DataServices.EntityModels
{
    public class OrdersModel
    {
        private TurnaroundManagementContext db = new TurnaroundManagementContext();
        private CommonModels objCommonModels = new CommonModels();

        public enum OrderStatus
        {
            Pending = 1,
            Processed = 2
        };
        public enum MaterialStatus
        {
            OnScheduleDate = 1,
            Delayed = 2,
            Issued = 3
        };
        public enum DeliveryStatus
        {
            Pending = 1,
            Delivered = 2
        };

        public List<Orders> GetAllOrders(int Status)
        {
            List<Orders> OrdersList = new List<Orders>();
            try
            {
                OrdersList = db.Orders.Where(x => x.Status == Status).ToList();
            }
            catch (Exception ex)
            {
                objCommonModels.LogException(ex);
                OrdersList = null;
            }
            return OrdersList;
        }

    }
}

How do i use this in my view page ? I cant access them in my view page. i have more enums to access..
I tried using this

@using myproject.dataservices.models.myclassnamethathastheenumvariableinit;

@using static myproject.dataservices.models.myclassnamethathastheenumvariableinit;

@using myproject.dataservices.models;

Nothing above is working..

Body:
You can only using namespaces and static classes. If you want to access an type, defined within an other class, as in your example. You can using the namespace, that contains the class (TurnaroundManagement.DataServices.EntityModels) in your case.

Then you need to use the complete type hirachy, to access a member of your enum. For example

var deliveryStatus = OrdersModel.DeliveryStatus.Pending

If you´re writing code within a type of that hirachy, you can omit parts of the path.

I personally only nest type, if the nested type is not intended, to be publically used. You can move your enums safely to their own files. This also helps other developers to find the files containing a specific type faster.


问:
How to access values of dictionary of array of dictionary of array using node js?
I have some monotonous Json response in which I am trying access element of dictionary of array of dictionary of array using node.js but hard luck and unable to do so. I have tried accessing this stackoverflow solution https://stackoverflow.com/questions/30448517/node-js-how-to-access-values-of-dictionary-within-an-array-of-a-key-in-a-dicti but in my case this is not working appropriately. My Json response is :

```<code>{
  "client": [
    {

      "code": [
        {
          "id": "001",
          "code": "100",
          "type": "New"
        },
        {
          "id": "002",
          "code": "200",
          "type": "Old"
        }
      ]
    }
  ]
}
</code>```

From this response I want to get value of each "id", "code" and "Type". Any valuable answer would be appreciated. Thank you !!!

答:
If your json was assigned to the variable x

```<code> x = {
   "client": [
     {

       "code": [
         {
           "id": "001",
           "code": "100",
           "type": "New"
         },
         {
           "id": "002",
           "code": "200",
           "type": "Old"
         }
       ]
     }
   ]
 }

console.log(x["client"][0]["code"][0]["id"])
</code>```

(I'll leave iterating through the arrays as an exercise for the reader)

Body:
I have some monotonous Json response in which I am trying access element of dictionary of array of dictionary of array using node.js but hard luck and unable to do so. I have tried accessing this stackoverflow solution https://stackoverflow.com/questions/30448517/node-js-how-to-access-values-of-dictionary-within-an-array-of-a-key-in-a-dicti but in my case this is not working appropriately. My Json response is :

{
  "client": [
    {

      "code": [
        {
          "id": "001",
          "code": "100",
          "type": "New"
        },
        {
          "id": "002",
          "code": "200",
          "type": "Old"
        }
      ]
    }
  ]
}

From this response I want to get value of each "id", "code" and "Type". Any valuable answer would be appreciated. Thank you !!!

Body:
If your json was assigned to the variable x

 x = {
   "client": [
     {

       "code": [
         {
           "id": "001",
           "code": "100",
           "type": "New"
         },
         {
           "id": "002",
           "code": "200",
           "type": "Old"
         }
       ]
     }
   ]
 }

console.log(x["client"][0]["code"][0]["id"])

(I'll leave iterating through the arrays as an exercise for the reader)


问:
How can I tell emacs not to break long lines?
How can I set Emacs to not automatically break long lines?

I find it easier to visualize indentation without its breaking long lines, to instead use horizontal scrolling when needed.

答:
Have you tried

M-x toggle-truncate-lines


问:
python struct unpack bad char
I am trying to write a small package parser, first time using python struct for it:

```<code>content = struct.unpack("BBBBBB<HBB", packet[:10])
</code>```

packet contains the following bytes:

```<code>7e03 ffff ff0f 1e00 1001 0c00 0000 0000
</code>```

I get this error: struct.error: bad char in struct format but I have no clue how I should know whats the bad char mentioned. Also on that note: does struct support grouping tuple elements, because I would rather have the unpacking done like this:

```<code>content = struct.unpack("BB(BBBB)<HBB", packet[:10])
</code>```

Because this is an address, and having it inside its own tuple in content would it make cleaner imo.

答:
< can't be in the middle of the format string, it needs to be at the front.

It specifies little-endian byte order and applies to the whole packet (but it makes no difference for single-byte values like those specified with B):

```<code>>>> packet = b'\x7e\x03\xff\xff\xff\x0f\x1e\x00\x10\x01\x0c\x00\x00\x00\x00\x00'
>>> struct.unpack("<BBBBBBHBB", packet[:10])
(126, 3, 255, 255, 255, 15, 30, 16, 1)
</code>```

To interpret four bytes as one unsigned integer, use I or L:

```<code>>>> struct.unpack("<BBIHBB", packet[:10])
(126, 3, 268435455, 30, 16, 1)
</code>```

Title: python struct unpack bad char

Body:
I am trying to write a small package parser, first time using python struct for it:

content = struct.unpack("BBBBBB<HBB", packet[:10])

packet contains the following bytes:

7e03 ffff ff0f 1e00 1001 0c00 0000 0000

I get this error: struct.error: bad char in struct format but I have no clue how I should know whats the bad char mentioned. Also on that note: does struct support grouping tuple elements, because I would rather have the unpacking done like this:

content = struct.unpack("BB(BBBB)<HBB", packet[:10])

Because this is an address, and having it inside its own tuple in content would it make cleaner imo.

Body:
< can't be in the middle of the format string, it needs to be at the front.

It specifies little-endian byte order and applies to the whole packet (but it makes no difference for single-byte values like those specified with B):

>>> packet = b'\x7e\x03\xff\xff\xff\x0f\x1e\x00\x10\x01\x0c\x00\x00\x00\x00\x00'
>>> struct.unpack("<BBBBBBHBB", packet[:10])
(126, 3, 255, 255, 255, 15, 30, 16, 1)

To interpret four bytes as one unsigned integer, use I or L:

>>> struct.unpack("<BBIHBB", packet[:10])
(126, 3, 268435455, 30, 16, 1)


问:
Text formatting error: '=' alignment not allowed in string format specifier
What does '=' alignment mean in the following error message, and why does this code cause it?

>>> "{num:03}".format(num="1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: '=' alignment not allowed in string format specifier
```

The code has a subtle problem: the input value "1" is text, not a number. But the error message doesn't appear to have anything to do with that.

Nothing in the error message indicates why “'=' alignment” is relevant,
and it does not appear in the code. So what is the significance of emitting that error message?

答:
In my case, I was trying to zero-pad a string instead of a number.

The solution was simply to convert the text to a number before applying the padding:

num_as_text = '23'
num_as_num = int(num_as_text)
padded_text = f'{num_as_num:03}'
```

Body:
What does '=' alignment mean in the following error message, and why does this code cause it?

>>> "{num:03}".format(num="1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: '=' alignment not allowed in string format specifier

The code has a subtle problem: the input value "1" is text, not a number. But the error message doesn't appear to have anything to do with that.

Nothing in the error message indicates why “'=' alignment” is relevant,
and it does not appear in the code. So what is the significance of emitting that error message?


问:
Test where ID not in (n, n1, n2)
Given a value, #some_id = x, what's the proper syntax or method to test that the id does not exist in a collection of id's?

For example...

```<code>if($some_id not in array(3, 5, 9)){
  //do something
}
</code>```

答:
```<code>$theArray = array(3, 5, 9);

if(! in_array($x, $theArray)){
    // do something
}
</code>```

http://php.net/manual/en/function.in-array.php

Title: Test where ID not in (n, n1, n2)

Body:
Given a value, #some_id = x, what's the proper syntax or method to test that the id does not exist in a collection of id's?

For example...

if($some_id not in array(3, 5, 9)){
  //do something
}

Body:
$theArray = array(3, 5, 9);

if(! in_array($x, $theArray)){
    // do something
}

http://php.net/manual/en/function.in-array.php